将在gunicorn上运行的Flask应用程序代理到nginx中的子路径

Mar*_*sse 10 proxy nginx flask gunicorn

我有一个带有gunicorn的Flask应用程序http://127.0.0.1:4000:

gunicorn -b 127.0.0.1:4000 webapp:app
Run Code Online (Sandbox Code Playgroud)

现在我想使用nginx作为反向代理,并转发http://myserver.com/webapphttp://127.0.0.1:4000的方式,每一个http://myserver.com/webapp/subpathhttp://127.0.0.1:4000/subpath.

不使用子路径时,代理/重定向可以很好地工作:

upstream app {
    server 127.0.0.1:4000 fail_timeout=0;
}

server {
    listen 80 default;
    client_max_body_size 4G;
    server_name _;

    location / {
       proxy_pass http://app;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $http_host;       
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎么设置

location /webapp {
    #go to my gunicorn app, translate URLs nicely
}
Run Code Online (Sandbox Code Playgroud)

来自Flask开发人员的这个提示不起作用:http://flask.pocoo.org/snippets/35/

已解决:该片段http://flask.pocoo.org/snippets/35/有效!我的模板中有一些绝对URL(例如/task/delete),并且必须将所有内容更改为url_for().

愚蠢...但现在它的工作方式与预期的一样,我的应用程序位于' http://myserver.com/subpath '

Mar*_*sse 13

我解决了我的问题:片段http://flask.pocoo.org/snippets/35/确实有效,我在模板中拥有绝对URL非常愚蠢.我把url_for()它改成了,现在它就像魅力一样.

  • 该网址似乎不再有效。您能提供解决方案吗?我面临着同样的问题。 (4认同)