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

我有一个Flask应用程序在http://127.0.0.1:4000上运行gunicorn

 gunicorn -b 127.0.0.1:4000 webapp:app 

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

代理/redirect在不使用子path时很好地工作:

 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() ,现在它就像魅力一样。