2016-01-24 58 views
0

我有一個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-subdomainshttps://django-subdomains.readthedocs.org

+1

你應該使用'break'而不是'last' –

回答

1

這可能是你的獎金問題的解決方案(即如何阻止從www.example.com /富/條): https://stackoverflow.com/a/4677893/2319697

基本上只是返回404如果模式/位置匹配

+0

謝謝,至少解決了我的獎金問題:) –