【发布时间】:2020-12-22 13:50:45
【问题描述】:
我正在尝试让我的非常简单的 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返回计划nginx的404页面:
我还尝试将 error_page 指令声明为它们各自子文件夹的位置,在我的情况下,错误的实际 index.html 所在的子文件夹是:/404-error/index.html 和 /5xx-errors/index.html,而不使用 @fallbackXXX 定义,效果是一样的。
我错过了什么? nginx 是否不尊重错误页面位于子文件夹或其他内容中?
【问题讨论】:
-
我不认为
location @fallback404实际上做了很多。您应该将try_files /index.html =404;添加到该位置以强制 Nginx 找到您的文件。 -
这有资格作为答案,它有效,想发布它。所以我可以接受吗?
标签: nginx