【问题标题】:When I visit Django /admin via a Rewritten URL I am redirected to 127.0.0.1 rather than the correct url当我通过重写的 URL 访问 Django /admin 时,我被重定向到 127.0.0.1 而不是正确的 url
【发布时间】:2011-11-20 13:37:59
【问题描述】:

我没有经常使用 Apache 和 Django,通常使用 nginx、gunicorn 和 unix 套接字。

在试验 Apache、mod_proxy 和 Django 时,我一直在尝试将所有请求传递到 gunicorn 后端,监听 127.0.0.1:8998。

我已经使用 Rewrite ... [P](见下文)实现了这一点,但是,这对于默认 URL 工作正常,当我转到 http://foo.bar.com/admin 时,我被重定向到 127.0.0.1/admin,它显然不能远程工作。这是可以通过 htaccess 文件实现的设置吗?我需要做什么/查看什么来修复它?

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule     (.*) http://localhost:8998/$1 [P]

谢谢!

【问题讨论】:

  • 你确定重写是你想要的吗?你不是在尝试反向代理吗?
  • 我可能误解了 [P] 的作用,因为我认为这是 mod_rewrite 与代理交谈的方法?另一个问题是,如果没有 root 访问权限,这可能无法实现,但我想如果有我不知道的方法,我会问。

标签: django apache mod-proxy gunicorn


【解决方案1】:

它是否与 http://localhosthttp://localhost/ 一起使用?

试试这个:

RewriteRule     ^/(.*) http://localhost:8998/$1 [P]

我最近研究了您想要达到的目标并最终得到了这个结果:

ProxyRequests Off
<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
ProxyPass        /static/ http://example.com:8080/static/
ProxyPassReverse /static/ http://example.com:8080/static/

在这种情况下,我将所有静态路径请求传递给 nginx,但它可能适用于你的 gunicorn。

当然需要开启apache2模块代理。

【讨论】:

  • 你能在 htaccess 中做到这一点吗?我认为 ProxyPass 等指令必须在服务器级别完成?
  • 我在 virtualhost conf 中执行此操作,尽管在 google 上快速搜索在 htaccess 文件中使用时看起来既积极又存在问题。
猜你喜欢
  • 2013-01-17
  • 2012-12-14
  • 2014-10-02
  • 2015-02-11
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多