說明:
location 中的 root 和 alias
root 指令只是將搜索的根設置為 root 設定的目錄,即不會截斷 uri,而是使用原始 uri 跳轉該目錄下查找文件 aias 指令則會截斷匹配的 uri,然后使用 alias 設定的路徑加上剩余的 uri 作為子路徑進行查找location 中的 proxy_pass 的 uri
如果 proxy_pass 的 url 不帶 uri
如果尾部是"/",則會截斷匹配的uri 如果尾部不是"/",則不會截斷匹配的uri如果proxy_pass的url帶uri,則會截斷匹配的uri
Examples
location中的 root
root@pts/1 $ ls -ld /data/web/lctest*|awk '{print $NF}'/data/web/lctest/data/web/lctest2/data/web/lctest3/data/web/lctest4location /lctest { root /data/web/;}location /lctest2/ { root /data/web/;}location /lctest3 { root /data/web;}location /lctest4/ { root /data/web;}
curl 測試結果如下
備注: 瀏覽器輸入的時候最后面不添加 / , 會自動補上,但是curl 不行
root@pts/1 $ curl http://tapi.xxxx.com/lctest/hello worldroot@pts/1 $ curl http://tapi.xxxx.com/lctest2/hello world2root@pts/1 $ curl http://tapi.xxxx.com/lctest3/3hello worldroot@pts/1 $ curl http://tapi.xxxx.com/lctest4/hello world4
location alias
location /lctest5 { alias /data/web/;}location /lctest6/ { alias /data/web/;}location /lctest7 { alias /data/web;}## 403 /data/web forbiddenlocation /lctest8/ { alias /data/web;}
curl 測試結果如下
curl 'http://tapi.kaishustory.com/lctest5/'curl 'http://tapi.kaishustory.com/lctest6/'curl 'http://tapi.kaishustory.com/lctest7/'結果都是 /data/web/index.html的輸出root@pts/1 $ curl 'http://tapi.kaishustory.com/lctest8/'<html><head><title>403 Forbidden</title></head><body bgcolor="white"><center><h1>403 Forbidden</h1></center><hr><center>nginx</center></body></html>
location proxy_pass
#--------proxy_pass配置---------------------location /t1/ { proxy_pass http://servers; } #正常,不截斷location /t2/ { proxy_pass http://servers/; } #正常,截斷location /t3 { proxy_pass http://servers; } #正常,不截斷location /t4 { proxy_pass http://servers/; } #正常,截斷location /t5/ { proxy_pass http://servers/test/; } #正常,截斷location /t6/ { proxy_pass http://servers/test; } #缺"/",截斷location /t7 { proxy_pass http://servers/test/; } #含"http://",截斷location /t8 { proxy_pass http://servers/test; } #正常,截斷
測試腳本
for i in $(seq 8)do url=http://tapi.xxxx.com/t$i/doc/index.html echo "-----------$url-----------" curl urldone
測試結果
----------http://tapi.xxxx.com/t1/doc/index.html------------/t1/doc/index.html----------http://tapi.xxxx.com/t2/doc/index.html------------/doc/index.html----------http://tapi.xxxx.com/t3/doc/index.html------------/t3/doc/index.html----------http://tapi.xxxx.com/t4/doc/index.html------------/doc/index.html----------http://tapi.xxxx.com/t5/doc/index.html------------/test/doc/index.html----------http://tapi.xxxx.com/t6/doc/index.html------------/testdoc/index.html----------http://tapi.xxxx.com/t7/doc/index.html------------/test//doc/index.html----------http://tapi.xxxx.com/t8/doc/index.html------------/test/doc/index.html
新聞熱點
疑難解答