如何在NGINX中设置自定义503错误页面? [英] How can I setup a custom 503 error page in NGINX?

查看:741
本文介绍了如何在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.

  • 启用维护touch /srv/sites/blah/public/maintenance.enable.您可以rm禁用该文件.
  • 错误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天全站免登陆