在反向代理后面使用 WSGI 为 Python 应用程序提供服务
我正在使用Python和Flask开发简单的Web应用程序。它必须在反向代理后面提供。我的URL在代理之前的URL具有/custom_prefix
由NGINX删除的,因此App并未直接意识到这一点。要解决与该问题相关的问题,我使用werkzeug.middleware.proxy_fix
:
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_port=1, x_for=1, x_host=1, x_prefix=1)
当然,我不得不添加一些特定的前缀(proxifix:
proxy_set_header X-Forwarded-Prefix /custom_prefix;
proxifix)使用它们来解决URL相关问题,并感谢这些方法像URL_为生成适当的URL。
但是,我必须使用套接字文件和WSGI服务我的应用程序,因此现在我使用UWSGI服务器服务。 更改了Nginx配置
uwsgi_param HTTP_X-Forwarded-Prefix /custom_prefix;
我 。由于request.headers
包含x-forwarded-prefix
。仍然是proxyfix无法完成这项工作。
I am developing simple web app using Python and Flask. It has to be served behind reverse proxy. My URLs before proxy have /custom_prefix
which is removed by Nginx, so app is not directly aware of that. To fix issues related to that I use ProxyFix from werkzeug.middleware.proxy_fix
:
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_port=1, x_for=1, x_host=1, x_prefix=1)
Of course I had to add some specific prefixes like to Nginx config:
proxy_set_header X-Forwarded-Prefix /custom_prefix;
ProxiFix uses them to fix url related issues and thanks to that methods like url_for generate proper URLs.
However, I have to serve my application using socket file and WSGI, so now I serve using uwsgi server. I have changed Nginx config because as far as I know there are no http headers in WGSI, so they have to be recreated with proper params like:
uwsgi_param HTTP_X-Forwarded-Prefix /custom_prefix;
After these changes ProxyFix is no longer fixing prefix issue and url_for returns URLs without /custom_prefix
. Headers seem to be recreated properly, because request.headers
contains X-Forwarded-Prefix
. Still ProxyFix does not do the job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论