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

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

问题描述

出于 SEO 的目的,每当 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 服务不可用".以及提供 Retry-After 标头以指示 Google/Bing 应在 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 always;
    index my_pretty_error_page.html; 
}

如果您以这种方式工作,您应该能够返回 503(即 error_page 指令的 =503 部分)和 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天全站免登陆