Flask url_for忽略端口

时间:2015-10-14 10:39:07

标签: python flask

我在端口5000上运行了一个Flask应用程序。我的服务器管理员已配置nginx将其转发到端口5001(对不起,如果我的术语错误:Flask应用程序在端口5000上运行,但该应用程序可在http://the_url:5001)。

在浏览器中直接访问的所有路由都可以正常工作,但使用url_for()的任何重定向似乎都会导致从网上遗漏端口 - 即redirect(url_for('index'))重定向到http://the_url/而不是{{ 1}}(http://the_url:5001/触发函数@app.route("/"))。

如何确保Flask在重定向时添加正确的端口?如果我将默认端口更改为5001,nginx配置将无法正常工作,因为它期望应用程序在端口5000上运行?

2 个答案:

答案 0 :(得分:8)

将此添加到您的Nginx配置: proxy_set_header Host $http_host; 要么 proxy_set_header Host $host:5001

read here

答案 1 :(得分:0)

尝试将这样的内容添加到nginx配置中。

location / {
            proxy_pass http://localhost:5000;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_set_header X-NginX-Proxy true;
        }

这样你就可以在localhost:5000上运行你的烧瓶应用程序。如果您正确设置server { listen 80; }配置(在配置顶部),用户应该能够访问端口80,它将重定向到内部5000端口。我url_for()的所有工作都很完美。

相关问题