Nginx的proxy_redirect作用


Nginx的proxy_redirect作用 - 阿權的書房

Nginx的代理功能太完善了,我們看看proxy_redirect參數的作用。

案例說明:
要做一個html.aslibra.com的域名處理很多網站的html內容,當然是后端的服務器了,目錄分析
html.zcom.com/img.aslibra.com/
html.zcom.com/css.aslibra.com/
訪問的域名是該目錄下的域名,那前端nginx的配置應該類似這樣:


server {
  server_name img.aslibra.com;
  location / {
    rewrite ^(.*) /$http_host$1 break;
    proxy_set_header Host html.aslibra.com;
    proxy_pass http://cache-89;
  }
}



但這樣訪問目錄時如果沒有以“/”結尾,則服務器會返回301redirect:


[root@aslibra ~]# curl -I http://img.aslibra.com/www
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.59
Date: Tue, 21 Jul 2009 15:28:58 GMT
Connection: keep-alive
Location: http://html.aslibra.com/img.aslibra.com/www/



html.aslibra.com這個域名並非公布的域名,返回給客戶端是會自然產生錯誤的
Nginx可以很好的處理這個問題:


server {
  server_name img.aslibra.com;
  location / {
    rewrite ^(.*) /$http_host$1 break;
    proxy_set_header Host html.aslibra.com;
    proxy_pass http://cache-89;
    proxy_redirect   http://html.aslibra.com/img.aslibra.com/    /;
  }
}



加一行proxy_redirect后,正常了:


[root@aslibra ~]# curl -I http://img.aslibra.com/www
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.59
Date: Tue, 21 Jul 2009 15:23:49 GMT
Content-Type: text/html
Location: http://img.aslibra.com/www/
Connection: keep-alive
Content-Length: 185
Expires: Tue, 21 Jul 2009 16:23:49 GMT
Cache-Control: max-age=3600



就這么樣就ok啦~
不過貌似不支持變量出現在地址里,這個就郁悶了,必須指定相應域名。
對於多個域名匹配的server,redirect設置不能寫作'/'了,否則會用第一個域名作為redirect域名
可以寫幾個匹配規則:


proxy_redirect   http://html.aslibra.com/img.aslibra.com/    http://img.aslibra.com/;
proxy_redirect   http://html.aslibra.com/css.aslibra.com/    http://css.aslibra.com/;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM