如何在Nginx中设置自定义503错误页面,拦截所有请求?

ev7lccsx  于 2022-12-17  发布在  Nginx
关注(0)|答案(4)|浏览(321)

我学会了如何让NGINX返回503客户错误页面,但我找不到如何执行以下操作:
配置文件示例:

location / {
        root   www;
        index  index.php;
        try_files /503.html =503;
    }

    error_page 503 /503.html;
    location = /503.html {
        root   www;
    }

正如您所看到的,根据上面的代码,如果在我的根目录中找到一个名为503.html的页面,站点将把这个页面返回给用户。

但是虽然上面的代码在有人访问我的网站时可以正常工作

使用我的代码,用户仍然可以看到配置文件页面或除index.php之外的任何其他页面。
问题是:
503.html出现在我的根文件夹中时,如何捕获对站点中所有页面的请求并将其转发到503.html

7rtdyuoh

7rtdyuoh1#

下面的配置适用于接近最新稳定的nginx 1.2.4。我找不到一种方法来启用维护页面与使用if,但显然根据IfIsEvil,这是一个确定的if

  • 要启用维护touch /srv/sites/blah/public/maintenance.enable。您可以rm要禁用的文件。
  • 错误502将被Map到503,这是大多数人想要的,你不想给予谷歌一个502
  • 自定义502503页面。你的应用将生成其他错误页面。

网上还有其他配置,但它们似乎在最新的nginx上不起作用。

server {
    listen       80;
    server_name blah.com;

    access_log  /srv/sites/blah/logs/access.log;
    error_log  /srv/sites/blah/logs/error.log;

    root   /srv/sites/blah/public/;
    index  index.html;

    location / {
        if (-f $document_root/maintenance.enable) {
            return 503;
        }
        try_files /override.html @tomcat;
    }

    location = /502.html {
    }

    location @maintenance {
       rewrite ^(.*)$ /maintenance.html break;
    }

    error_page 503 @maintenance;
    error_page 502 =503 /502.html;

    location @tomcat {
         client_max_body_size 50M;

         proxy_set_header  X-Real-IP  $remote_addr;
         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header  Host $http_host;
         proxy_set_header  Referer $http_referer;
         proxy_set_header  X-Forwarded-Proto http;
         proxy_pass http://tomcat;
         proxy_redirect off;
    }
}
7vux5j2d

7vux5j2d2#

更新日期:将“if -f”更改为“try_files”。

试试这个:

server {
    listen      80;
    server_name mysite.com;
    root    /var/www/mysite.com/;

    location / {
        try_files /maintenance.html $uri $uri/ @maintenance;

        # When maintenance ends, just mv maintenance.html from $root
        ... # the rest of your config goes here
     }

    location @maintenance {
      return 503;
    }

}

更多信息:
https://serverfault.com/questions/18994/nginx-best-practices
http://wiki.nginx.org/HttpCoreModule#try_files

moiiocjp

moiiocjp3#

其他的答案都是正确的,但是要补充的是,如果您使用内部代理,您还需要在其中一个代理服务器上添加proxy_intercept_errors on;
比如说...

proxy_intercept_errors on;
    root /var/www/site.com/public;
    error_page 503 @503;
    location @503 {
       rewrite ^(.*)$ /scripts/503.html break;
    }
j5fpnvbx

j5fpnvbx4#

多年以后,我现在使用的是完全自定义的错误消息。
HTML错误页存储在网站根目录下的/http-error目录中。

######    #####     #####      ####     #####        #####       ##       ####     ######     ####  
#         #    #    #    #    #    #    #    #       #    #     #  #     #    #    #         #      
#####     #    #    #    #    #    #    #    #       #    #    #    #    #         #####      ####  
#         #####     #####     #    #    #####        #####     ######    #  ###    #              # 
#         #   #     #   #     #    #    #   #        #         #    #    #    #    #         #    # 
######    #    #    #    #     ####     #    #       #         #    #     ####     ######     ####  

# ------------------------------------------------------------------------------
# HTTP > SERVER > ERROR_PAGE :: WWW.EXAMPLE1.COM
# ------------------------------------------------------------------------------
# Optionally include these error pages as a file.
# include /etc/nginx/conf.d/www.example1.com_error_page.conf;
# ------------------------------------------------------------------------------
# Description
# Defines the URI that will be shown for the specified errors.
# 
# ------------------------------------------------------------------------------
# 
# 
# 400 Bad Request
error_page            400 @400;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 400 error must be returned in this manner for custom http error pages to be served correctly.
location @400 {
   rewrite ^(.*)$     /http-error/400-error.html break;
}
# 401 Unauthorized
error_page            401 @401;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 401 error must be returned in this manner for custom http error pages to be served correctly.
location @401 {
   rewrite ^(.*)$     /http-error/401-error.html break;
}
# 403 Forbidden
error_page            403 @403;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 403 error must be returned in this manner for custom http error pages to be served correctly.
location @403 {
   rewrite ^(.*)$     /http-error/403-error.html break;
}
# 404 Not Found
error_page            404 @404;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 404 error must be returned in this manner for custom http error pages to be served correctly.
location @404 {
   rewrite ^(.*)$     /http-error/404-error.html break;
}

# 405 Method Not Allowed
# unreachable do to nature of the error itself. here only for completeness.
# error_page            405 /http-error/405-error.html break;

# Request Timeout
error_page            408 @408;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 408 error must be returned in this manner for custom http error pages to be served correctly.
location @408 {
   rewrite ^(.*)$     /http-error/408-error.html break;
}

# 500 Internal Server Error
error_page            500 @500;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 500 error must be returned in this manner for custom http error pages to be served correctly.
location @500 {
   rewrite ^(.*)$     /http-error/500-error.html break;
}
# 502 Bad Gateway
error_page            502 @502;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 502 error must be returned in this manner for custom http error pages to be served correctly.
location @502 {
   rewrite ^(.*)$     /http-error/502-error.html break;
}
# 503 Service Unavailable
error_page            503 @503;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 503 error must be returned in this manner for custom http error pages to be served correctly.
location @503 {
   rewrite ^(.*)$     /http-error/503-error.html break;
}
# 504 Gateway Time-out
error_page            504 @504;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 504 error must be returned in this manner for custom http error pages to be served correctly.
location @504 {
   rewrite ^(.*)$     /http-error/504-error.html break;
}
# 505 HTTP Version Not Supported
error_page            505 @505;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 505 error must be returned in this manner for custom http error pages to be served correctly.
location @505 {
   rewrite ^(.*)$     /http-error/505-error.html break;
}
# 511 HTTP Version Not Supported
error_page            511 @511;

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# An http 511 error must be returned in this manner for custom http error pages to be served correctly.
location @511 {
   rewrite ^(.*)$     /http-error/511-error.html break;
}

#       ####   ####    ##   ##### #  ####  #    # 
#      #    # #    #  #  #    #   # #    # ##   # 
#      #    # #      #    #   #   # #    # # #  # 
#      #    # #      ######   #   # #    # #  # # 
#      #    # #    # #    #   #   # #    # #   ## 
######  ####   ####  #    #   #   #  ####  #    # 

# example1.com internal error pages located at...
location /http-error/ {
  # Specifies that a given location can only be used for internal requests.
  # returns a 404 Not Found http error if accessed directly.
  internal;
}

相关问题