在nginx中配置proxy_pass時,如果是按照^~匹配路徑時,要注意proxy_pass后的url最后的/。當加上了/,相當于是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分也給代理走。
比如下面設置:
location ^~ /wangshibo/{proxy_cache js_cache;proxy_set_header Host js.test.com;proxy_pass http://js.test.com/;}
如上面的配置,如果請求的url是http://servername/wangshibo/test.html會被代理成http://js.test.com/test.html
而如果這么配置
location ^~ /wangshibo/{proxy_cache js_cache;proxy_set_header Host js.test.com;proxy_pass http://js.test.com;}
則請求的url是http://servername/wangshibo/test.html會被代理到http://js.test.com/wangshibo/test.html
當然,可以用如下的rewrite來實現/的功能
location ^~ /wangshibo/{proxy_cache js_cache;proxy_set_header Host js.test.com;rewrite /wangshibo/(.+)$ /$1 break;proxy_pass http://js.test.com;}
列舉下面一例
1)第一種配置
[root@BJLX_16_202_V vhosts]# cat ssl-wangshibo.confupstream at { server 192.168.1.202:8080 max_fails=3 fail_timeout=30s;} server { listen 443; server_name www.wangshibo.com; ssl on; ### SSL log files ### access_log logs/wangshibo_access.log; error_log logs/wangshibo_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; location /attendance/ { proxy_pass http://at; //不需要加上"/" proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } }
訪問https://www.wangshibo.com/attendance/和http://192.168.1.202:8080/attendance結果是一致的。
2)第二種配置
[root@BJLX_16_202_V vhosts]# cat ssl-wangshibo.confupstream at { server 192.168.1.202:8080 max_fails=3 fail_timeout=30s;} server { listen 443; server_name www.wangshibo.com; ssl on; ### SSL log files ### access_log logs/wangshibo_access.log; error_log logs/wangshibo_error.log; ### SSL cert files ### ssl_certificate ssl/wang.cer; ssl_certificate_key ssl/wang.key; location / { proxy_pass http://at/attendance/; //一定要加上"/" proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; } }
訪問https://www.wangshibo.com和http://192.168.1.202:8080/attendance結果是一致的。
新聞熱點
疑難解答