问题描述
当(上游)后面的网络服务器脱机时,我试图显示自定义502错误页面,但它不起作用:它将继续显示默认的502错误页面(并且我确定上游是离线的).
I am trying to display a custom 502 error page when webserver behind (upstream) is offline, but it doesn't work : it continues displaying default 502 error page (and I'm sure upstream is offline).
我的文件组织如下:
- 静态文件位于
/var/web/example.org/www/res
下(例如:/var/web/example.org/www/res/robots.txt
); - 错误页面在
/var/web/example.org/www
下(例如:/var/web/example.org/www/502.html
).这是因为我不希望打开该Web服务器时可以访问错误页面(不是来自后面的Web服务器,包括404等).
- Static files are under
/var/web/example.org/www/res
(example:/var/web/example.org/www/res/robots.txt
) ; - Error pages are under
/var/web/example.org/www
(example:/var/web/example.org/www/502.html
). This is because i don't want error pages (not coming from the webserver behind, including 404 and others) to be accessible when that webserver is on.
这是我的(匿名)配置:
Here is my (anonymised) config :
upstream exampleorg {
server 127.0.0.1:8087;
}
server {
listen 80;
server_name www.example.org;
# content
# - static
root /var/web/example.org/www/res;
# - main webserver
error_page 403 404 @exampleorg;
error_page 502 /../502.html;
location @exampleorg {
proxy_pass http://exampleorg;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
我还尝试了以下方法来定义以下内容以捕获502(在服务器块中,最后):
I have also tried the following to define the following for catching the 502 (in the server block, at the end) :
error_page 502 /502.html;
location /502.html {
root /var/web/example.org/www;
internal;
}
但是nginx继续显示默认的502页面(即使没有internal;
).我应该怎么做 ?预先感谢您的解决方案:)
But nginx continued displaying default 502 page (even without the internal;
). How should I do ? Thanks in advance for your solution(s) :)
推荐答案
经过一些试验后找到了解决方案.我认为该错误来自"error_page" 404语句,我不认为try_files
如此强大.无论如何,这是可行的最终配置,因为它对某些人可能有用:
Found the solution after some experimenting. I think the error came from the "error_page" 404 statements, and I didn't think try_files
was so powerful. Anyway, that's the final configuration that works, as it can be useful to some people :
upstream exampleorg {
server 127.0.0.1:8087;
}
server {
listen 80;
server_name www.example.org;
# content
# - static
location / {
root /var/web/example.org/www/res;
try_files $uri @exampleorg;
}
# - main webserver
location @exampleorg {
proxy_pass http://exampleorg;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# - error
error_page 502 @offline;
location @offline {
root /var/web/example.org/www;
try_files /502.html 502;
}
}
这篇关于Nginx不会使用自定义502错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!