nginx proxy_pass 裏的」/」

見配置,摘自nginx.conf 裏的server 段:html

server {
listen 80;
server_name abc.163.com ;
location / {
proxy_pass http://ent.163.com/ ;
}
location /star/ {
proxy_pass http://ent.163.com ;
}
}

裏面有兩個location,我先說第一個,/ 。其實這裏有兩種寫法,分別是:nginx

location / {
proxy_pass http://ent.163.com/ ;
}
location / {
proxy_pass http://ent.163.com ;
}

出來的效果都同樣的。代理

第二個location,/star/。一樣兩種寫法都有,都出來的結果,就不同了。server

location /star/ {
proxy_pass http://ent.163.com ;
}

當訪問 http://abc.163.com/star/ 的時候,nginx 會代理訪問到 http://ent.163.com/star/ ,並返回給咱們。htm

location /star/ {
proxy_pass http://ent.163.com/ ;
}

當訪問 http://abc.163.com/star/ 的時候,nginx 會代理訪問到 http://ent.163.com/ ,並返回給咱們。blog

這兩段配置,分別在於, proxy_pass http://ent.163.com/ ; 這個」/」,令到出來的結果徹底不一樣。io

前者,至關於告訴nginx,我這個location,是代理訪問到http://ent.163.com 這個server的,個人location是什麼,nginx 就把location 加在proxy_pass 的 server 後面,這裏是/star/,因此就至關於 http://ent.163.com/star/。若是是location /blog/ ,就是代理訪問到 http://ent.163.com/blog/。proxy_pass

後者,至關於告訴nginx,我這個location,是代理訪問到http://ent.163.com/的,http://abc.163.com/star/ == http://ent.163.com/ ,能夠這樣理解。改變location,並不能改變返回的內容,返回的內容始終是http://ent.163.com/ 。 若是是location /blog/ ,那就是 http://abc.163.com/blog/ == http://ent.163.com/ 。配置

這樣,也能夠解釋了上面那個location / 的例子,/ 嘛,加在server 的後面,仍然是 / ,因此,兩種寫法出來的結果是同樣的。co

PS: 若是是 location ~* ^/start/(.*)\.html 這種正則的location,是不能寫」/」上去的,nginx -t 也會報錯的了。由於,路徑都須要正則匹配了嘛,並非一個相對固定的locatin了,必然要代理到一個server。

相關文章
相關標籤/搜索