我正在尝试为 503 添加自定义错误页面。我将这些行添加到 nginx.conf 文件中的服务器 conf:
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/username/sites/myProject/current/errorPages;
internal;
}
它在 uwsgi 关闭时显示自定义页面,但是这不显示任何图像。我尝试了许多我能想到的不同配置,但没有运气。如何显示图像文件并为自定义错误页面启用 css?
我将自定义错误页面放入 /home/username/sites/myProject/current/errorPages
文件结构为:
errorPages/50x.html
errorPages/50x_files/50x.css
errorPages/50x_files/50x.js
errorPages/50x_files/image.png
最佳答案
我只是遇到了同样的问题,有效的方法是像这样设置 nginx conf:
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/username/sites/myProject/current/errorPages;
}
location = /image.png {
root /home/username/sites/myProject/current/errorPages/50x_files;
}
然后将图像简单地引用为 src="image.png"。这同样适用于您的 css 和 js!
编辑:我找到了一种方法让它适用于一堆文件:
error_page 500 502 503 504 /errorPages/50x.html;
location /errorPages/ {
root /home/username/sites/myProject/current/;
}
这样,errorPages 文件夹中的所有文件都将可用(例如 src="/errorPages/image.png"),因为 nginx 将尝试匹配所有“/errorPages/...”。有必要删除“location”之后的“=”(因为它不再是完全匹配)和“internal;”在其中(因为其他资源将从 html 中调用,而不是在 nginx 内部调用)。
关于html - 带有 css 和图像文件的 nginx 自定义错误页面 502,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610979/