Nginx不使用Gunicorn显示自定义404页+烧瓶

发布于 2025-01-26 12:11:21 字数 814 浏览 4 评论 0原文

我正在尝试在我的网站上显示一个自定义404错误页面,但是似乎Nginx并未捕获404。相反,它显示了Gunicorn(Blask App)

nginx.conf的404页:

server {
listen 80 default_server;
server_name example.com www.example.com;

location / {
    proxy_pass http://flask:8080/;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
}

error_page 404 /notfound.html;
location /notfound.html {
    root /var/www/html;
}

error_page 500 502 503 504 /maintenance.html;
location /maintenance.html {
    root /var/www/html;
}
}

Blask App和Nginx都在单独的Docker容器上运行。

我有目的地将烧瓶应用程序放下以查看发生了什么事 当它重定向到504(超时)和维护。HTML出现。 - 工作正常。

但是在404错误而不是显示此自定义notfound.html上,它显示404从烧瓶应用程序中找到(404页面上没有任何nginx版本) 。

I'm trying to display a custom 404 error page on my site, but it seems that Nginx does not catch the 404. Instead, it shows a 404 page from Gunicorn(flask app)

nginx.conf:

server {
listen 80 default_server;
server_name example.com www.example.com;

location / {
    proxy_pass http://flask:8080/;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
}

error_page 404 /notfound.html;
location /notfound.html {
    root /var/www/html;
}

error_page 500 502 503 504 /maintenance.html;
location /maintenance.html {
    root /var/www/html;
}
}

Both flask app and Nginx are running on separate docker containers.

I purposefully brought the flask app down to see what happens, after a
while It redirects to 504(Timeout) and the maintenance.html appeared. - works fine.

But on a 404 error instead of displaying this custom notfound.html, it displays a 404 Not Found from Flask App (The 404 page did not had any nginx version on it).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2025-02-02 12:11:21

proxy_intercept_errors;应该解决此问题。

proxy_intercept_errors on; should solve this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文