尽管 ProxyPassReverse,gunicorn 通过 mod_proxy 正在重定向到项目范围之外

发布于 2024-11-16 11:45:00 字数 844 浏览 11 评论 0原文

我有一个 WSGI 应用程序(一个 Django 项目)在 127.0.0.1:18731 上的 Gunicorn 下运行,我使用 Apache 和 mod_proxy 来重定向来自 http://example.com/my- 的请求项目/*http://127.0.0.1:18731/*。静态文件存储在 /my-project/ 外部。如果 Django 应用程序不需要重定向任何内容,那么这工作得很好,但如果它尝试重定向请求(例如,向 http://example.com/my-project/foo 添加尾部斜杠) code>),它最终会从 URL 中删除 /my-project/,留下无效的 URL http://example.com/foo/

我的 mod_proxy 配置如下:

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
ProxyRequests On
ProxyPass /my-project/ http://127.0.0.1:18731/ retry=0
ProxyPassReverse /my-project/ http://127.0.0.1:18731/ retry=0
ProxyPreserveHost On
ProxyErrorOverride Off

为了可移植性,我不想强​​制 Django 在其所有 URL 中添加 /my-project/ 前缀。 Apache 显然应该使用 ProxyPassReverse 行自行处理前缀。我做错了什么?

I have a WSGI-app (a Django project) running under gunicorn on 127.0.0.1:18731 and I use Apache with mod_proxy to redirect requests from http://example.com/my-project/* to http://127.0.0.1:18731/*. Static files are stored outside of /my-project/. If the Django app does not need to redirect anything, this works just fine, but if it tries to redirect a request (e.g. to add a trailing slash to http://example.com/my-project/foo), it ends up removing /my-project/ from the URL, leaving me with the invalid URL http://example.com/foo/.

My mod_proxy configuration is as follows:

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
ProxyRequests On
ProxyPass /my-project/ http://127.0.0.1:18731/ retry=0
ProxyPassReverse /my-project/ http://127.0.0.1:18731/ retry=0
ProxyPreserveHost On
ProxyErrorOverride Off

I do not want to force Django to prefix /my-project/ to all of its URLs, in the interest of portability. Apache should apparently be handling the prefix on its own with the ProxyPassReverse line. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离线来电— 2024-11-23 11:45:00

我有这个问题。

ProxyPreserveHost On

<Location "/my-project/">
    ProxyPass http://127.0.0.1:18173/my-project/
    ProxyPassReverse http://127.0.0.1:18173/my-project/
    RequestHeader set SCRIPT_NAME /my-project
    RequestHeader set X-FORWARDED-PROTOCOL ssl
    RequestHeader set X-FORWARDED-SSL on
</Location>

为了使 WSGI 应用程序能够构建绝对 URL,我们需要:

  • ProxyPreserveHost On,以便传递 Host: 标头,并且应用程序知道客户端看到我们的主机名。
  • 添加 SCRIPT_NAME 标头,以便应用知道其根目录在哪里。
  • 根据需要设置 X-FORWARDED- 标头。我也在使用 ssl,因此我必须告诉应用程序它应该使用 https 方案。

我使用 指令,因为我在这个虚拟主机上做了很多事情。但是您可以通过将 path 参数传递给 ProxyPassProxyPassReverse 指令。

注意: ProxyRequests 应该是 < strong>关闭,除非您也想要转发代理。如果您正在阅读本文,您可能只需要一个反向代理。

Django 特定说明:settings.LOGIN_URL 按原样使用,因此您需要自己在其前面添加 SCRIPT_NAME

I had this problem to.

ProxyPreserveHost On

<Location "/my-project/">
    ProxyPass http://127.0.0.1:18173/my-project/
    ProxyPassReverse http://127.0.0.1:18173/my-project/
    RequestHeader set SCRIPT_NAME /my-project
    RequestHeader set X-FORWARDED-PROTOCOL ssl
    RequestHeader set X-FORWARDED-SSL on
</Location>

To enable the WSGI app to construct an absolute url we need:

  • ProxyPreserveHost On, so the Host: header gets passed and the app knows the Hostname the client sees us at.
  • Add a SCRIPT_NAME header so the app knows where its root is.
  • Set X-FORWARDED- headers as needed. I'm using ssl too, so I have to tell the app it should use the https scheme.

I use <Location> directives, because I do a lot more stuff on this vhost. But you can easily rewrite this by passing path arguments to the ProxyPass and ProxyPassReverse directives.

NB: ProxyRequests should be Off, unless you want a forward proxy too. If you're reading this you probably only want a reversed proxy.

Django specific note: settings.LOGIN_URL is used as is, so you'll need to prepend the SCRIPT_NAME to it yourself.

埋葬我深情 2024-11-23 11:45:00

你尝试过这个吗?我也将 my-project 添加到您正在代理的 url 中。

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
ProxyRequests On
ProxyPass /my-project/ http://127.0.0.1:18731/my-project/ retry=0
ProxyPassReverse /my-project/ http://127.0.0.1:18731/my-project/ retry=0
ProxyPreserveHost On
ProxyErrorOverride Off

我通常使用 nginx 来做这类事情,所以我不确定这是否有效。

更新:上面的方法不起作用,所以尝试其他方法。

尝试这样的事情,看看是否有帮助。它的设置有点不同。它代理除通过别名提供的媒体之外的所有内容。这根本不需要 /my-project/ 。

<VirtualHost *:80>
ServerName example.com
UseCanonicalName On
ServerAdmin webmaster@localhost

LogLevel warn
CustomLog /var/log/apache2/example.com/access.log combined
ErrorLog /var/log/apache2/example.com/error.log
ServerSignature On

Alias /media/ /home/example/example.com/pysrc/project/media/

ProxyPass /media/ !
ProxyPass / http://127.0.0.1:18731/
ProxyPassReverse / http://127.0.0.1:18731/
ProxyPreserveHost On
ProxyErrorOverride Off
</VirtualHost>

Did you try this? I added my-project to the url you are proxying too.

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
ProxyRequests On
ProxyPass /my-project/ http://127.0.0.1:18731/my-project/ retry=0
ProxyPassReverse /my-project/ http://127.0.0.1:18731/my-project/ retry=0
ProxyPreserveHost On
ProxyErrorOverride Off

I normally use nginx for this sort of thing, so I'm not sure if that will work or not.

Update: the above didn't work so trying something else.

Try something like this and see if that helps. It is setup a little different. It proxies everything except media which is served via an alias. This remove the need to have /my-project/ at all.

<VirtualHost *:80>
ServerName example.com
UseCanonicalName On
ServerAdmin webmaster@localhost

LogLevel warn
CustomLog /var/log/apache2/example.com/access.log combined
ErrorLog /var/log/apache2/example.com/error.log
ServerSignature On

Alias /media/ /home/example/example.com/pysrc/project/media/

ProxyPass /media/ !
ProxyPass / http://127.0.0.1:18731/
ProxyPassReverse / http://127.0.0.1:18731/
ProxyPreserveHost On
ProxyErrorOverride Off
</VirtualHost>
云雾 2024-11-23 11:45:00

我通过将 ProxyPassReverse 配置设置为实际域名解决了最初的问题:

ProxyPass /my-project/ http://127.0.0.1:18731/
ProxyPassReverse /my-project/ http://mydomain.com/

提示: Apache ProxyPassReverse 值

I have solved the original problem by setting the ProxyPassReverse config to the actual domain name:

ProxyPass /my-project/ http://127.0.0.1:18731/
ProxyPassReverse /my-project/ http://mydomain.com/

Hint: Apache ProxyPassReverse values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文