Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

apache - Show a custom 503 page if upstream is down

I am using nginx as a frontend to an apache server. The config file looks like:

upstream apache {
    server localhost:8000;
}

server {
    listen 80;
    error_page 503 /www/static/503.html;

    # need some magic here #

    location /static/ {
        root /www/static/;
    }

    location / {
        proxy_path http://apache/;
    }
}

For now, when apache is down, I receive a plain 502 page generated by nginx. How to make it serve my custom error page and return status code 503 which is more relevant in this situation?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Something like this

upstream apache {
    server localhost:8000;
}

server {
    listen 80;
    error_page 502 503 /www/static/503.html;

    location /static/ {
        root /www/static/;
    }

    location / {
        proxy_path http://apache/;
    }
}

You can append standard error codes together to display a single page for several types of errors.
For example:

error_page 502 503 /www/static/503.html;

For more reference you can refer the error_page manual


On the error_page manual it says

Furthermore, it is possible to change the code of answer to another, for example:

error_page 404 =200 /.empty.gif;

Another option
To make it return a different error code you can make use of a return keyword
For example:

# check for a condition
  if (condition) {
     return 503;
  }

Also See
nginx: Create HTTP 503 Maintenance Custom Page


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...