Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

redirect - How to setup custom 503 error page in Nginx that intercepts all requests?

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

Sample config file:

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

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

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

it does not trap requests like:

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

The question:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

  • 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...