首頁 > 運維 > Nginx > 主體

nginx location中uri的截取如何實現

王林
發布: 2023-05-18 12:07:06
轉載
1569 人瀏覽過

說明:

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/lctest4


location /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 world

root@pts/1 $ curl http://tapi.xxxx.com/lctest2/
hello world
2

root@pts/1 $ curl http://tapi.xxxx.com/lctest3/
3
hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest4/
hello world
4
登入後複製

location alias

location /lctest5 {
  alias /data/web/;
}
location /lctest6/ {
  alias /data/web/;
}

location /lctest7 {
  alias /data/web;
}

## 403 /data/web forbidden
location /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/; }  #含"//",截断
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 url
done
登入後複製

測試結果

----------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
登入後複製

以上是nginx location中uri的截取如何實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!