简体   繁体   English

与gunricorn + nginx的Flask重定向(url_for)错误

[英]Flask redirect(url_for) error with gunricorn + nginx

I am having a problem with the redirect(url_for) function in my flask app. 我的烧瓶应用程序中的重定向(url_for)函数出现问题。

Any redirect(url_for("index")) line redirects the application from domain.com/app to ip-addr/app , where the ip-addr is my own client machines ip, not the server's. 任何重定向(url_for(“index”))行将应用程序从domain.com/app重定向到ip-addr / app ,其中ip-addr是我自己的客户端机器ip,而不是服务器的。

This has gotten me very confused, and I don't know where exactly the issue occurs, as it only happens on the server and not on any local testing. 这让我很困惑,我不知道问题究竟发生在哪里,因为它只发生在服务器而不是任何本地测试。

Details: 细节:

I am using the reverse proxy setup found here http://flask.pocoo.org/snippets/35/ . 我正在使用http://flask.pocoo.org/snippets/35/中的反向代理设置。 My nginx config is setup like so 我的nginx配置是这样设置的

location /app {
                proxy_set_header X-Script-Name /app;
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-Host $proxy_add_x_forwarded_for;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_connect_timeout 60;
                proxy_read_timeout 60;
                proxy_pass http://localhost:8000/;
        }

I have gunicorn running my flask app as a upstart task. 我有一个gunicorn运行我的烧瓶应用程序作为一个暴发户的任务。 Any hints? 任何提示?

EDIT: 编辑:

So I dug around a bit and found that the this git report had similar issues, https://github.com/omab/django-social-auth/issues/157 . 所以我挖了一下,发现这个git报告有类似的问题, https://github.com/omab/django-social-auth/issues/157

Nginx - Gunicorn serving to Nginx via localhost (127.0.0.1:1234). Nginx - Gunicorn通过localhost(127.0.0.1:1234)为Nginx服务。 Unfortunately, when I authenticate with social platforms, the redirect URL social-auth is sending them to is 127.0.0.1:1234/twitter/complete, which obviously can't be resolved by the client's browser. 不幸的是,当我使用社交平台进行身份验证时,重定向URL social-auth将它们发送到127.0.0.1:1234/twitter/complete,这显然无法通过客户端的浏览器解析。

It seems my Flask app is not getting the memo to update its redirect routes. 看来我的Flask应用程序没有得到备忘录来更新其重定向路由。

I found a solution. 我找到了解决方案。 I had to use redirect(url_for(location, _external=True)) for all my redirects. 我必须为我的所有重定向使用redirect(url_for(location, _external=True))

It seems url_for(x, _external=True) will take all variables in my nginx proxy_set_header to construct the url, while the url_for(x) is not doing that. 似乎url_for(x, _external=True)将使用我的nginx proxy_set_header中的所有变量来构造url,而url_for(x)不会这样做。

This works for both server and local development. 这适用于服务器和本地开发。

Adding include proxy_params fixed it for me. 添加include proxy_params为我修复了它。

location / {
    proxy_pass http://...;
    include proxy_params;
}

Had the same problem. 有同样的问题。 Your solution is makes it necessary for the app to know which domain it's running by setting it on a config var for each environment. 您的解决方案是让应用程序通过在每个环境的config var上设置它来了解它正在运行的域。 I found HERE that the solution is actually to rewrite the headers on nginx proxy_pass like so: 我发现这里的解决方案实际上是在nginx proxy_pass上重写头文件,如下所示:

location /api {
       # Define the location of the proxy server to send the request to
       proxy_pass http://web:8000;

       # Redefine the header fields that NGINX sends to the upstream server
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       # Define the maximum file size on file uploads
       client_max_body_size 5M;
   }

I've lost 2 nights in this, I finally solved it using this solution: http://blog.macuyiko.com/post/2016/fixing-flask-url_for-when-behind-mod_proxy.html , my proxy settings: 我已经失去了2个晚上,我终于使用这个解决方案解决了它: http//blog.macuyiko.com/post/2016/fixing-flask-url_for-when-behind-mod_proxy.html ,我的代理设置:

ProxyPass /crm http://localhost:5013/
ProxyPassReverse /crm http://localhost:5013/

And the ReverseProxy class worked perfectly, the trick was the "script_name" that it's not originally included in the snippet from here: http://flask.pocoo.org/snippets/35/ I guess the snipped from flask page works with ngnix and this is for apache2 proxy. 并且ReverseProxy类工作得很好,诀窍是“script_name”,它最初没有包含在这里的代码片段中: http ://flask.pocoo.org/snippets/35/我想从烧瓶页面剪切可以使用ngnix和这是针对apache2代理的。 Hope this helps if someone comes here again with the same issue (future me included) 希望如果有人再次来到这里同样的问题(包括未来我),这会有所帮助

I had similar issue. 我有类似的问题。 My server was crashing at @return redirect(url_for('login')), when I run it using gunicorn. 当我使用gunicorn运行它时,我的服务器在@return redirect(url_for('login'))崩溃了。 For the solution go to Flask app running with gunicorn and nginx is crashing at redirecting point 对于解决方案,请访问使用gunicorn运行的Flask应用程序,并且nginx在重定向点时崩溃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM