简体   繁体   English

当我在gunicorn上运行Flask应用程序时设置app.wsgi_app = ProxyFix(app.wsgi_app)会发生什么?

[英]What is going on when I set app.wsgi_app = ProxyFix(app.wsgi_app) when running a Flask app on gunicorn?

I built a basic web app using Flask, and was able to run it from a virtual machine using its native http server. 我使用Flask构建了一个基本的Web应用程序,并且能够使用其本机http服务器从虚拟机运行它。 I quickly realized that with this set up, requests are blocking (I couldn't make concurrent requests for resources; any new request would wait until earlier requests had finished), and decided to try gunicorn to run the app to solve this problem. 我很快意识到,通过这个设置,请求被阻止(我无法对资源进行并发请求;任何新请求都要等到早期请求完成),并决定尝试使用gunicorn运行应用程序来解决此问题。 I followed the documentation , specifically running with this line: 我按照文档 ,特别是使用此行运行:

gunicorn -w 4 -b 127.0.0.1:4000 myproject:app 

However, it failed to boot doing just this, and complained that there was no WSGI app. 但是,它无法启动这样做,并抱怨没有WSGI应用程序。 Poking around the internet, I found that a number of people had posted examples including the following: 在互联网上闲聊,我发现有很多人发布了以下示例:

from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)

I added that, and it resolved my problem. 我补充说,它解决了我的问题。 I am confused though because this is apparently meant to solve a problem serving behind an HTTP proxy, but would the addition of gunicorn impose an HTTP proxy? 我很困惑,因为这显然是为了解决在HTTP代理后面服务的问题,但是增加的gunicorn会强加一个HTTP代理吗? Or was I always behind a proxy, and it just didn't matter for Flask's built-in server? 或者我总是落后于代理,而Flask的内置服务器并不重要?

Also, Werkzeug's documentation on Fixers warns "Do not use this middleware in non-proxy setups for security reasons." 另外, Werkzeug关于Fixers的文档警告说“出于安全原因,不要在非代理设置中使用此中间件”。 Considering the fix was clearly necessary, can I assume I'm on a proxy setup? 考虑到修复显然是必要的,我可以假设我正在进行代理设置吗?

You need to show the code that defines your Flask application "app". 您需要显示定义Flask应用程序“app”的代码。

Where is "app" defined? “app”在哪里定义? Are you importing it from another file? 您是从其他文件导入的吗?

Here is my attempt to reproduce the issue: 以下是我尝试重现该问题:

$ cat myproject.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "ok"

$ bin/gunicorn -w 4 -b 127.0.0.1:4000 myproject:app &
[1] 27435

2014-03-04 12:18:36 [27435] [INFO] Starting gunicorn 18.0
2014-03-04 12:18:36 [27435] [INFO] Listening at: http://127.0.0.1:4000 (27435)
2014-03-04 12:18:36 [27435] [INFO] Using worker: sync
2014-03-04 12:18:36 [27441] [INFO] Booting worker with pid: 27441
2014-03-04 12:18:36 [27442] [INFO] Booting worker with pid: 27442
2014-03-04 12:18:36 [27445] [INFO] Booting worker with pid: 27445
2014-03-04 12:18:36 [27448] [INFO] Booting worker with pid: 27448

$ curl http://127.0.0.1:4000/
ok

As you can see it works fine. 你可以看到它工作正常。 You definitely don't need ProxyFix in this case. 在这种情况下,您绝对不需要ProxyFix。

A little late to the party, but here is what the documentation says about ProxyFix. 有点迟到了,但这里是什么文件说,大约ProxyFix。

To paraphrase: Deploying your server using gunicorn behind an HTTP proxy you will need to rewrite some of the headers so that the application can work. 换句话说:在HTTP代理后面使用gunicorn部署服务器,您需要重写一些头文件,以便应用程序可以正常工作。 And Werkzeug ships with a fixer that will solve some of the common setups. Werkzeug附带一个可以解决一些常见设置的修复工具。

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

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