是否可以更改在nginx中关闭proxy_pass网关时返回的HTTP状态代码? [英] Is it possible to change the HTTP status code returned when proxy_pass gateway is down in nginx?

查看:646
本文介绍了是否可以更改在nginx中关闭proxy_pass网关时返回的HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于搜索引擎优化的目的,我们想要更改在nginx后面的后端机器由于某种原因而关闭时返回的HTTP状态代码。

For SEO purposes, we would like to change the HTTP status code returned whenever the backend machine behind nginx goes down for some reason.

我们想要更改此到503服务不可用。除了向Google / Bing提供一个Retry-After标头,该请求应在X秒内重试。

We would like to change this to "503 Service Unavailable". As well as provide a Retry-After header to indicated to Google / Bing that the request should be retried in X number of seconds.

这是否可以通过nginx实现?

Is this possible via nginx?

我不是在谈论自定义错误页面,而是标题中返回的状态代码。

I am not talking about a custom error page, but rather the status code returned in the header.

推荐答案

我认为你必须设置一个特定的错误页面,但如果你这样做,你可以实现你想要的。试试这个:

I think you will have to set up a specific error page, however you can achieve what you're looking for if you do. Try this:

location / {
    proxy_pass http://backend;
    proxy_intercept_errors on;
    error_page 502 503 504 =503 @proxyisdown; # always reply with 503
}

location @proxyisdown {
    add_header Retry-After 500;
    index my_pretty_error_page.html; 
}

如果你以这种方式工作,你应该能够返回503(那是 = 503 error_page 指令的一部分)和retry-after标头,其好处是访问者可以很好地收到格式化哎呀,我们目前遇到问题,请在几分钟内再试页面而不是空白503你真的不知道这意味着什么页面。 :)

If you work it this way you should be able to return the 503 (that's the =503 part of the error_page directive) and retry-after headers with the benefit that your visitors will receive a nicely formatted "oops, we're currently experiencing problems, try again in a few minutes" page rather than a blank "503 you don't really know what this means" page. :)

这篇关于是否可以更改在nginx中关闭proxy_pass网关时返回的HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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