【发布时间】:2013-05-16 07:41:13
【问题描述】:
我有一个 Django 应用程序,最近我需要启动一个测试版。我想保持当前正在运行的应用程序不变,并在 Nginx 的帮助下将所有以“/beta”开头的请求重定向到 beta 应用程序。这是我的conf
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 360;
proxy_pass http://localhost:8000/;
}
location /beta/ {
rewrite ^/beta/(.*)$ /$1 break;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 360;
proxy_pass http://localhost:8001/;
}
这可行,但是有一个问题,当应用程序返回 301 响应时,主要是当用户需要登录才能访问某些资源时,URL 会变成旧的。
例如,如果/events 需要登录。
http://example.com/beta/events -> http://example.com/login?next=/events/
如何在不更改应用程序代码的情况下解决此问题? (Nginx 解决方案?)
【问题讨论】:
-
你不应该在 django 后端/中间件而不是 nginx 中处理它吗?
-
使用子域 beta.example.com 对我来说是一个更好的解决方案
-
我是否正确理解您使用 Django 的内置开发服务器作为代理的后端?你真的不应该这样做。例如,如果您使用的是 uwsgi,您的问题将有一个非常简单的解决方案(并且您的应用会运行得更快)
-
由于 django 不知道你的 nginx 配置,它不能建立知道
/beta/的链接。对/foo that was redirected to a request to the base app/foo 的请求之间的唯一区别是引荐来源网址。如果您发现用户来自 /beta/ 或其他来源,则必须嗅探引荐来源并在前面加上/beta/ -
@DmitryDemidenko 是的,我知道,但我不想为测试目的设置子域。无论如何,谢谢!