如何在 Nginx 中设置拦截所有请求的自定义 503 错误页面? [英] How to setup custom 503 error page in Nginx that intercepts all requests?

查看:359
本文介绍了如何在 Nginx 中设置拦截所有请求的自定义 503 错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学会了如何让 NGINX 返回 503 客户错误页面,但我不知道如何执行以下操作:

I learned how to get NGINX to return 503 customer error pages, but I cannot find out how to do the following:

示例配置文件:

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

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

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

As you can see, according to the code above, if a page called 503.html is found in my root directory, the site will return this page to the user.

但是似乎虽然当有人只是访问我的网站输入

But it seems that although the code above works when someone simply visits my site typing

它不会捕获如下请求:

使用我的代码,用户仍然可以看到个人资料页面或除 index.php 之外的任何其他页面.

With my code, the user can still see the profile page or any other pages besides index.php.

问题:

当我的根文件夹中存在 503.html 时,如何捕获对我网站中所有页面的请求并将它们转发到 503.html?

How do I trap requests to all pages in my site and forward them to 503.html whenever 503.html is present in my root folder?

推荐答案

以下配置适用于接近最新的稳定版 nginx 1.2.4.我找不到一种方法来启用维护页面而不使用 if 但显然根据 IfIsEvil 没问题 if.

The below configuration works for close to the latest stable nginx 1.2.4. I could not find a way to enable a maintenance page with out using an if but apparently according to IfIsEvil it is an ok if.

  • 启用维护触摸/srv/sites/blah/public/maintenance.enable.您可以rm要禁用的文件.
  • Error 502 将映射到 503,这是大多数人想要的.您不想给 Google 一个 502.
  • 自定义502503 页面.您的应用将生成其他错误页面.
  • To enable maintenance touch /srv/sites/blah/public/maintenance.enable. You can rm the file to disable.
  • Error 502 will be mapped to 503 which is what most people want. You don't want to give Google a 502.
  • Custom 502 and 503 pages. Your app will generate the other error pages.

网络上还有其他配置,但它们似乎不适用于最新的 nginx.

There are other configurations on the web but they didn't seem to work on the latest 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;
    }
}

这篇关于如何在 Nginx 中设置拦截所有请求的自定义 503 错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆