Show a custom 503 page if upstream is down

﹥>﹥吖頭↗ 提交于 2019-12-28 12:08:12

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/5031818/show-a-custom-503-page-if-upstream-is-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!