我在http://127.0.0.1:4000上运行了带有gunicorn的Flask应用:

gunicorn -b 127.0.0.1:4000 webapp:app

现在,我想使用nginx作为反向代理,并将http://myserver.com/webapp转发到http://127.0.0.1:4000,以使每个http://myserver.com/webapp/subpath都转到http://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;
    }
}

我该如何设定
location /webapp {
    #go to my gunicorn app, translate URLs nicely
}

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

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

愚蠢的...但现在它的工作原理与预期的一样,我的应用程序位于“http://myserver.com/subpath”上

最佳答案

我解决了我的问题:片段http://flask.pocoo.org/snippets/35/确实有效,我太愚蠢了,无法在模板中使用绝对URL。我将其更改为url_for(),现在它就像魅力一样工作。

09-30 23:54
查看更多