最新在配置nginx時,意外發現location中目錄斜線有和沒有的區別,百度了找找發現沒有幾個人說的清楚,最后找到一個兄弟寫的還比較實用,再次謝過(https://blog.csdn.net/ruihaol/article/details/79526749?from=timeline https://blog.csdn.net/u010509052/article/details/105455813)。
一、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指定目錄下的;