如果上游关闭,则显示自定义 503 页面 [英] Show a custom 503 page if upstream is down

查看:41
本文介绍了如果上游关闭,则显示自定义 503 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nginx 作为 apache 服务器的前端.配置文件如下所示:

上游 apache {服务器本地主机:8000;}服务器 {听80;error_page 503/www/static/503.html;#这里需要一些魔法#位置/静态/{根/www/静态/;}地点/{proxy_path http://apache/;}}

现在,当 apache 关闭时,我会收到一个由 nginx 生成的普通 502 页面.如何让它为我的自定义错误页面提供服务并返回在这种情况下更相关的状态代码 503?

解决方案

类似这样的事情

上游 apache {服务器本地主机:8000;}服务器 {听80;error_page 502 503/www/static/503.html;位置/静态/{根/www/静态/;}地点/{proxy_path http://apache/;}}

您可以将标准错误代码附加在一起以显示多个类型的错误的单个页面.
例如:

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

如需更多参考,您可以参考error_page 手册

<小时>

error_page 手册上说

<块引用>

此外,可以更改另一个答案的代码,对于例子:

error_page 404 =200/.empty.gif;

<小时>

另一种选择
要使其返回不同的错误代码,您可以使用 return 关键字
例如:

# 检查条件如果(条件){返回503;}

另见
nginx:创建 HTTP 503 维护自定义页面>

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?

解决方案

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

这篇关于如果上游关闭,则显示自定义 503 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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