溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

nginx location中uri的截取如何實現

發布時間:2022-05-21 10:58:47 來源:億速云 閱讀:1536 作者:iii 欄目:大數據

這篇文章主要介紹“nginx location中uri的截取如何實現”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“nginx location中uri的截取如何實現”文章能幫助大家解決問題。

說明:

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的截取如何實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女