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。

相关文章
相关标签/搜索