尽管 ProxyPassReverse,gunicorn 通过 mod_proxy 正在重定向到项目范围之外
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有这个问题。
为了使 WSGI 应用程序能够构建绝对 URL,我们需要:
ProxyPreserveHost On
,以便传递Host:
标头,并且应用程序知道客户端看到我们的主机名。SCRIPT_NAME
标头,以便应用知道其根目录在哪里。X-FORWARDED-
标头。我也在使用 ssl,因此我必须告诉应用程序它应该使用https
方案。我使用
指令,因为我在这个虚拟主机上做了很多事情。但是您可以通过将path
参数传递给ProxyPass
和ProxyPassReverse
指令。注意:
ProxyRequests
应该是 < strong>关闭,除非您也想要转发代理。如果您正在阅读本文,您可能只需要一个反向代理。Django 特定说明:
settings.LOGIN_URL
按原样使用,因此您需要自己在其前面添加SCRIPT_NAME
。I had this problem to.
To enable the WSGI app to construct an absolute url we need:
ProxyPreserveHost On
, so theHost:
header gets passed and the app knows the Hostname the client sees us at.SCRIPT_NAME
header so the app knows where its root is.X-FORWARDED-
headers as needed. I'm using ssl too, so I have to tell the app it should use thehttps
scheme.I use
<Location>
directives, because I do a lot more stuff on this vhost. But you can easily rewrite this by passingpath
arguments to theProxyPass
andProxyPassReverse
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 theSCRIPT_NAME
to it yourself.你尝试过这个吗?我也将 my-project 添加到您正在代理的 url 中。
我通常使用 nginx 来做这类事情,所以我不确定这是否有效。
更新:上面的方法不起作用,所以尝试其他方法。
尝试这样的事情,看看是否有帮助。它的设置有点不同。它代理除通过别名提供的媒体之外的所有内容。这根本不需要 /my-project/ 。
Did you try this? I added my-project to the url you are proxying too.
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.
我通过将 ProxyPassReverse 配置设置为实际域名解决了最初的问题:
提示: Apache ProxyPassReverse 值
I have solved the original problem by setting the ProxyPassReverse config to the actual domain name:
Hint: Apache ProxyPassReverse values