天天看點

nginx location中斜線的位置的重要性

最新在配置nginx時,意外發現location中目錄斜線有和沒有的差別,百度了找找發現沒有幾個人說的清楚,最後找到一個兄弟寫的還比較實用,再次謝過(https://blog.csdn.net/ruihaol/article/details/79526749?from=timeline)。

一、nginx代理後端服務

nginx 伺服器及端口 127.0.0.1:80

後端服務:127.0.0.1:8080

測試url:http://127.0.0.1:80/day06api/api/abc

A.配置

nginx配置如下:

location /day06api/ {
   proxy_pass http://127.0.0.1:8080/;
}
           

實際通路的端口服務:http://127.0.0.1:8080/api/abc

B.配置

location /day06api {
   proxy_pass http://127.0.0.1:8080/;
}
           

實際通路的端口服務:http://127.0.0.1:8080//api/abc

C.配置

location /day06api/ {
   proxy_pass http://127.0.0.1:8080;
}
           

實際通路的端口服務:http://127.0.0.1:8080/day06api/api/abc

D.配置

location /day06api {
   proxy_pass http://127.0.0.1:8080;
}
           

實際通路的端口服務:http://127.0.0.1:8080/day06api/api/abc

E.配置

location /day06api/ {
   proxy_pass http://127.0.0.1:8080/server/;
}
           

實際通路的端口服務:http://127.0.0.1:8080/server/api/abc

F.配置

location /day06api {
   proxy_pass http://127.0.0.1:8080/server/;
}
           

實際通路的端口服務:http://127.0.0.1:8080/server//api/abc

G.配置

location /day06api/ {
   proxy_pass http://127.0.0.1:8080/server;
}
           

實際通路的端口服務:http://127.0.0.1:8080/serverapi/abc

H.配置

location /day06api {
   proxy_pass http://127.0.0.1:8080/server;
}
           

實際通路的端口服務:http://127.0.0.1:8080/server/api/abc

慢慢比較發現規律:

  • 1.proxy_pass 最後有斜線時(即端口後隻有斜線,例如A和B中的proxy_pass),location 最後有斜線時,最終組成的url:proxy_pass + location最後一個斜線以後的部分
  • 2.proxy_pass 最後有斜線時(即端口後隻有斜線,例如A和B中的proxy_pass),location 最後無斜線時,最終組成的url:proxy_pass + 斜線 + location後面的所有部分(但不包含location後面的所有部分的第一個斜線) //其實就是比1多個斜線
  • 3.proxy_pass 最後無斜線時,location 最後有斜線時,最終組成的url:proxy_pass + location + 請求url中location以後的所有部分(不包含第一個/)
  • 4.proxy_pass 最後無斜線時,location 最後無斜線時,最終組成的url:proxy_pass + location + “/” + 請求url中location以後的所有部分(不包含第一個/)
  • 5.proxy_pass 最後有斜線時(且已經包含了至少一級目錄,例如E和F中的proxy_pass),location 最後有斜線時,最終組成的url:proxy_pass + location以後的所有部分(但不包含第一個/)
  • 6.proxy_pass 最後有斜線時(且已經包含了至少一級目錄,例如E和F中的proxy_pass),location 最後無斜線時,最終組成的url:proxy_pass + “/” + location以後的所有部分(包含第一個/)
  • 7.proxy_pass 最後無斜線時(且包含了至少一級目錄,例如G和H中的proxy_pass),location 最後有斜線時,最終組成的url:proxy_pass + location以後的所有部分(不包含第一個/)
  • 8.proxy_pass 最後無斜線時(且包含了至少一級目錄,例如G和H中的proxy_pass),location 最後無斜線時,最終組成的url:proxy_pass + location以後的所有部分(包含第一個/)

    這個真的不好總結,可能總結的有誤,可以直接對應上面的例子。

二、nginx代理本地靜态資源

nginx 伺服器及端口 127.0.0.1:80

後端服務:127.0.0.1:8080

真實的資源路徑:

E:/project/hello

E:/project/hello/index.html

E:/project/hello/img/123.png

測試url:

http://127.0.0.1/hello/index.html

http://127.0.0.1/hello/img/123.png

A:
location /hello/{
	root   E:/project/;
	index  index.html;
}
           

實際請求資源路徑

E:/project/hello/index.html

E:/project/hello/img/123.png

B:
location /hello/{
	root   E:/project;
	index  index.html;
}
           

實際請求資源路徑

E:/project/hello/index.html

E:/project/hello/img/123.png

C:
location /hello{
	root   E:/project/;
	index  index.html;
}
           

實際請求資源路徑

E:/project/hello/index.html

E:/project/hello/img/123.png

D:
location /hello{
	root   E:/project;
	index  index.html;
}
           

實際請求資源路徑

E:/project/hello/index.html

E:/project/hello/img/123.png

E:
location /hello/{
	alias   E:/project/;
}
           

實際請求資源路徑

E:/project/hello/index.html 404

E:/project/hello/img/123.png 正常

F:
location /hello{
	alias   E:/project/;
}
           

實際請求資源路徑

E:/project/hello/index.html 404

E:/project/hello/img/123.png 正常

1)alias指定的目錄是準确的,即location比對通路的path目錄下的檔案直接是在alias目錄下查找的;

2)root指定的目錄是location比對通路的path目錄的上一級目錄,這個path目錄一定要是真實存在root指定目錄下的;

繼續閱讀