X-Forwarded-Proto和Flask [英] X-Forwarded-Proto and Flask
问题描述
我在这个问题和答案中描述了完全相同的问题。这个问题的答案是一个很好的工作,但我不明白的根本问题。在负载平衡器上终止SSL并在负载平衡器和Web /应用程序服务器之间使用HTTP是非常普遍的。什么堆栈不尊重X-Forwarded-Proto?这是否是werkzeug?烧瓶?在我的情况下,我使用AWS ELB(它设置X-Forwarded-Proto)=> Nginx(沿着X-Forwarded-Proto转发到uwsgi )。但是在python应用程序中,我必须按照上面引用的问题中的描述子类Flask Request。
由于这是一种常见的部署方案,似乎应该有更好的解决方案。我错过了什么? ProxyFix()
中间件组件。请参阅Flask 代理设置文档。
没有必要继承任何东西;只需将这个中间件组件添加到WSGI堆栈中即可:
from werkzeug.contrib.fixers import flask ProxyFix
from flask import
$ b app = Flask(__ name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
如果你安装了Flask,那么你也可以使用Werkzeug。
$ b
这个组件从X-Forwarded-Proto头部设置WSGI方案。请阅读我上面链接到的关于信任标头的Flask文档,以及如何根据您的具体情况定制中间件。
I have precisely the same problem described in this SO question and answer. The answer to that question is a nice work around but I don't understand the fundamental problem. Terminating SSL at the load balancer and using HTTP between the load balancer and web/app servers is very common. What piece of the stack is not respecting the X-Forwarded-Proto? Is it werkzeug? Flask? uwsgi?
In my case I'm using an AWS ELB (which sets X-Forwarded-Proto) => Nginx (which forwards along X-Forwarded-Proto to uwsgi). But in the python app I have to subclass Flask Request as described in the question I referenced above.
Since this is such a common deployment scenario, it seems that there should be a better solution. What am I missing?
You are missing the ProxyFix()
middleware component. See the Flask Proxy Setups documentation.
There is no need to subclass anything; simply add this middleware component to your WSGI stack:
from werkzeug.contrib.fixers import ProxyFix
from flask import Flask
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
If you have Flask installed, you have Werkzeug too.
This component sets the WSGI scheme from the X-Forwarded-Proto header. Do read the Flask documentation I linked you to above about trusting headers and about customising the middleware to your specific situation.
这篇关于X-Forwarded-Proto和Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!