在反向代理后面使用 WSGI 为 Python 应用程序提供服务

发布于 2025-01-17 11:36:45 字数 711 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文