nginx反向代理自定义502、504错误页不生效

技术 2023-10-30

nginx反向代理请求失败时,总是返回默认的502/504页面,在server字段下配置error_page属性仍然不生效。

在查阅资料后发现,似乎是因为nginx在location中找不到自定义的502、504错误页导致的。解决方法也很简单,手动指定错误页面的文件根目录即可。具体可参考下文示例配置。

server {
    listen       80;
    server_name  example.com;
    
    error_page 502 /502.html;
    error_page 504 /504.html;

    location / {
        proxy_pass http://p.example.com;
    }

    location = /502.html {
        root /usr/share/nginx/html;
        internal;
    }

    location = /504.html {
        root /usr/share/nginx/html;
        internal;
    }
}
nginx