【问题标题】:How to setup custom 503 error page in Nginx that intercepts all requests?如何在 Nginx 中设置拦截所有请求的自定义 503 错误页面?
【发布时间】:2011-08-01 19:24:53
【问题描述】:

我学会了如何让 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

【问题讨论】:

    标签: nginx redirect nginx-config http-status-code-503


    【解决方案1】:

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

    • 启用维护touch /srv/sites/blah/public/maintenance.enable。您可以rm 禁用该文件。
    • 错误502 将映射到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;
        }
    }
    

    【讨论】:

    • 谢谢你。我也找不到不使用if 的方法。很高兴看到这是可以接受的用途!
    • 我发现如果用户执行 POST 而不是 GET,它可能会失败 - 在这种情况下,他们会收到 405 空响应而不是 503。不确定,如何解决。尝试了我能找到的一切。
    【解决方案2】:

    更新:将“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

    【讨论】:

    • try_files 是最佳实践。此外,它并没有丢失。只是不完整。
    • @Vini 不丢失和不完整之间有什么区别,对我来说是一回事。我更新了示例以包含 try_files 而不是 if -f。希望对您有所帮助。
    • 谢谢肯。顺便问一下,$uri 是做什么的?我连续看到它两次。
    • 这为 /maintenance.html 提供状态代码 200。如何以 503 的正确状态提供页面?
    • 如果你有一个文件/maintenance.html,这将永远不会返回503 http 代码,因为try_files 指令会停在那里。
    【解决方案3】:

    其他答案都是正确的,但只是补充一点,如果您使用内部代理,您还需要在您的代理服务器之一上添加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;
        }
    

    【讨论】:

      【解决方案4】:

      多年后,这是我目前用于完全自定义错误消息的内容。

      HTML 错误页面存储在您网站根目录的 /http-error 目录中。

      我在www.xmpl.link 创建了 NGINX PHP-FPM 快速设置指南,您可以在其中学习如何启动服务器、下载准备使用的错误页面模板等等。

      ######    #####     #####      ####     #####        #####       ##       ####     ######     ####  
      #         #    #    #    #    #    #    #    #       #    #     #  #     #    #    #         #      
      #####     #    #    #    #    #    #    #    #       #    #    #    #    #         #####      ####  
      #         #####     #####     #    #    #####        #####     ######    #  ###    #              # 
      #         #   #     #   #     #    #    #   #        #         #    #    #    #    #         #    # 
      ######    #    #    #    #     ####     #    #       #         #    #     ####     ######     ####  
      
      # ------------------------------------------------------------------------------
      # 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-25
        • 1970-01-01
        • 2014-02-23
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 2013-06-04
        相关资源
        最近更新 更多