Nginx 反向代理错误页面

Lor*_*yna 8 nginx

我使用 nginx 作为一台机器的反向代理。当后端机器出现故障时,我希望有一个错误页面。

这是我的配置文件:

server {
    listen   80;
    access_log  /var/log/nginx/access.log;
    root /var/www/nginx;
    error_page 403 404 500 502 503 504 /error.html;
    location / {
            proxy_pass      http://192.168.1.78/;
            include         /etc/nginx/proxy.conf;
    }
Run Code Online (Sandbox Code Playgroud)

Mic*_*ton 8

啊哈,我看到问题了。您没有为 nginx 提供实际提供静态文件(例如 )的方法/error.html,因此它试图将它们从上游传递到您的后端。

快速解决方法是:

location /error.html {
    internal;
}
Run Code Online (Sandbox Code Playgroud)

这将导致 nginx自行处理/error.html。然后它将尝试从定义的文档中提供文件root

顺便说一句,您可能希望对 4xx 错误和 5xx 错误使用不同的错误页面。“未找到”或任何您不希望人们(或搜索引擎!)查看后端是否暂时关闭的内容。