Flask应用程序在通过nginx进行代理时提供无处不在的404 [英] Flask app gives ubiquitous 404 when proxied through nginx

查看:477
本文介绍了Flask应用程序在通过nginx进行代理时提供无处不在的404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个烧瓶的应用程序通过主管守护进程。我想proxy_pass本地主机上的子文件夹的烧瓶应用程序。烧瓶应用程序直接运行时运行正常,但通过代理调用时会出现404错误。这里是nginx的配置文件:

  upstream apiserver {
server 127.0.0.1:5000;
}

位置/ api {
重写/api/(.*)/ $ 1 break;
proxy_pass_header服务器;
proxy_set_header主机$ http_host;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Scheme $ scheme;
proxy_pass http:// apiserver;
proxy_next_upstream错误超时http_502;
proxy_buffering off;




$ b

例如,当我去 http://127.0.0.1:5000/me ,我从应用程序得到一个有效的响应。但是,当我去 http://127.0.0.1/api/me 我从烧瓶应用程序(而不是nginx)404。另外,如果这一点很重要的话,那么 SERVER_NAME 变量被设置为 127.0.0.1:5000

b
$ b

我非常感谢任何建议;我很难过!如果还有其他东西需要添加,请告诉我们!

解决方案

由于Flask正在处理请求,因此您可以添加一点点的信息到404错误,以帮助您了解什么是通过应用程序,并给你一些真正的反馈,你的nginx配置更改的原因造成的影响。

  from flask进口请求

@ app.errorhandler(404)
def page_not_found(错误):
return'此路线不存在{}' .format(request.url),404

所以当你得到一个404页面时,你正是Flask正在处理的事情,这应该可以帮助你很快地缩小你的问题。


I've got a flask app daemonized via supervisor. I want to proxy_pass a subfolder on the localhost to the flask app. The flask app runs correctly when run directly, however it gives 404 errors when called through the proxy. Here is the config file for nginx:

upstream apiserver {
    server 127.0.0.1:5000;
}

location /api {
            rewrite /api/(.*) /$1 break;
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://apiserver;
            proxy_next_upstream error timeout http_502;
            proxy_buffering off;

}

For instance, when I go to http://127.0.0.1:5000/me, I get a valid response from the app. However when I go to http://127.0.0.1/api/me I get a 404 from the flask app (not nginx). Also, the flask SERVER_NAME variable is set to 127.0.0.1:5000, if that's important.

I'd really appreciate any suggestions; I'm pretty stumped! If there's anything else I need to add, let me know!

解决方案

Since Flask is handling the request, you could just add a little bit of information to the 404 error to help you understand what's passing through to the application and give you some real feedback about what effect your nginx configuration changes cause.

from flask import request

@app.errorhandler(404)
def page_not_found(error):
    return 'This route does not exist {}'.format(request.url), 404

So when you get a 404 page, it will helpfully tell you exactly what Flask was handling, which should help you to very quickly narrow down your problem.

这篇关于Flask应用程序在通过nginx进行代理时提供无处不在的404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆