我试图使我的非常简单的nginx服务静态文件回退,或者以其他方式显示错误页面,如果是404或50x错误,如下所示:
server {
listen 80;
server_name frontend;
add_header X-Frame-Options "SAMEORIGIN";
error_page 404 = @fallback404;
location @fallback404 {
index index.html;
root /usr/share/nginx/html/404-error;
internal;
}
error_page 500 502 503 504 @fallback5xx;
location @fallback5xx {
index index.html;
root /usr/share/nginx/html/5xx-errors;
}
location /testing500 {
fastcgi_pass unix:/does/not/exist;
}
location / {
autoindex off;
index index.html;
root /usr/share/nginx/html;
try_files $uri $uri/ =404;
}
}甚至/testing/500也会返回plan的404页:

我还试图将error_page指令声明为它们各自子文件夹的位置,其中实际存在有错误的index.html,在我的示例中:/404-error/index.html和/5xx-errors/index.html没有使用@fallbackXXX定义,效果是一样的。
我错过了什么?是否nginx不尊重错误页在子文件夹或其他什么?
发布于 2020-12-22 15:44:25
您需要向try_files块添加一个location @fallback404语句,以选择要返回的正确URI。
例如:
error_page 404 = @fallback404;
location @fallback404 {
root /usr/share/nginx/html/404-error;
try_files /index.html =404;
internal;
}但是,一个更简单的解决方案是在error_page语句本身中指定URI。
例如:
error_page 404 /404-error/index.html;
location = /404-error/index.html {
root /usr/share/nginx/html;
internal;
}https://stackoverflow.com/questions/65409861
复制相似问题