我对想要实现的特定设置遇到了一些问题:
概括:
- Nginx 充当反向代理
- 后端运行于 http://localhost:8080/
- Nginx 可能会返回 HTTP 403(
$block_access = true
),在这种情况下,我想显示自定义错误页面(/errors/403.html
) - 后端可能会返回 HTTP 404,在这种情况下,我还想显示自定义错误页面(
/errors/404.html
)
问题:
- 自定义 HTTP 403 页面不起作用,而是使用默认的 Nginx 403 页面
- 后端的自定义 HTTP 404 页面有效
- 如果我从位置块中删除
proxy_intercept_errors
,自定义 403 页面适用于 Nginx,但代理的自定义 404 页面显然不再起作用
配置:
server {
listen 443 ssl http2;
server_name mydomain.com;
# Reverse proxy configuration
location / {
# Let Nginx handle basic blocking behavior
if ($block_access) {
return 403;
}
# Proxy request to backend
proxy_pass http://localhost:8080/;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection $http_connection;
# Only intercept 404 errors from the backend
proxy_intercept_errors on;
error_page 404 /errors/404.html;
# Serve custom 404 error page
location = /errors/404.html {
root /var/www/html;
internal;
}
}
# Serve custom 403 error page for blocked access ($block_access = true)
error_page 403 /errors/403.html;
location = /errors/403.html {
root /var/www/html;
internal;
}
location /errors/ {
root /var/www/html;
try_files $uri $uri/ =404;
}
}
我或多或少在寻找解决方案时完全不知所措......