出于搜索引擎优化的目的,我们希望在nginx背后的后端机器由于某种原因出现故障时,更改返回的HTTP状态码。
我们想将此更改为"503服务不可用"。同时提供一个Retry-After报头,向Google/Bing表明请求应该在X秒后重试。
这是可能通过nginx吗?
我不是在谈论自定义错误页面,而是在标题中返回的状态码。
我认为你将必须设置一个特定的错误页面,但是你可以实现你正在寻找的,如果你这样做。试试这个:
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
部分)和重试标头,这样你的访问者会收到一个格式很好的"哎呀,我们目前遇到问题,几分钟后再试一次"页面,而不是一个空白的"503你真的不知道这意味着什么"页面。:)
将错误页面命名为/500.html
和:
error_page 400 404 500 502 504 =503 /500.html;
# Optional if your public root is set above and the same for error pages,
# I sometimes please them outside the app, which is why I'm including it.
location /500.html {
root /path/to/public;
}
应该也能工作,对我来说似乎更简单一些。注意:它也不支持Header