我有一個django應用程序,它爲iframe中的一些用戶內容提供服務。爲了避免會話劫持,我想從一個不同的子域提供這個服務。用nginx將`example.com/foo/bar`重寫爲`foo.example.com/bar`。重定向循環
我目前擁有的站點位於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年1月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;
無濟於事。
沒有重定向它主機正確,但我想避免讓網址爲foo.example.com/foo/bar/ID
。
有沒有不同的方法來處理這個會更好?或者一種方法來阻止重定向?
PS:紅利問題?接下來會阻止subdomain.example.com/foo/bar,從而迫使通過foo.example.com/bar訪問
編輯:我解決了這個問題,通過刪除重寫和照顧django中的子域使用django-subdomains
(https://django-subdomains.readthedocs.org)
你應該使用'break'而不是'last' –