【发布时间】:2016-01-24 19:23:19
【问题描述】:
我有一个 django 应用程序,它在 iframe 中提供一些用户内容。为避免会话劫持,我想从不同的子域提供此服务。
我目前将网站托管在subdomain.example.com,特定 iframe 内容位于subdomain.example.com/foo/bar/ID。我现在正在尝试设置 nginx 以使该特定内容可以通过 foo.example.com/bar/ID 访问。
我的 nginx 设置:
server {
listen 80;
server_name subdomain.example.com;
location / {
include proxy_params;
#Served with gunicorn:
proxy_pass http://unix:/home/example/example.sock;
}
}
server {
listen 80;
server_name foo.example.com;
location / {
include proxy_params;
#This is key:
rewrite ^/(.*?) /foo$request_uri last;
#Served with gunicorn:
proxy_pass http://unix:/home/example/example.sock;
}
}
这给了我在 nginx error.log 中的以下错误:
2016/01/24 14:10:41 [错误] 24286#0: *17 在处理“/foo/bar/66”时重写或内部重定向周期,客户端:xx.xx.xx.xx,服务器: foo.example.com,请求:“GET /bar/66 HTTP/1.1”,主机:“foo.example.com”
这是有道理的;重定向一次又一次地被重定向,因为它匹配重写正则表达式。
我尝试添加
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
无济于事。
如果没有重定向,它确实可以正确托管,但我想避免将 url 设为 foo.example.com/foo/bar/ID。
有没有更好的方法来解决这个问题?或者有办法阻止重定向?
PS:奖金问题?接下来将阻止 subdomain.example.com/foo/bar 并因此强制通过 foo.example.com/bar 访问
编辑:我通过删除重写解决了这个问题,并使用 django-subdomains (https://django-subdomains.readthedocs.org) 处理 django 中的子域
【问题讨论】:
-
你应该使用
break而不是last