系统相关
首页 > 系统相关> > nginx location中斜线的位置的区别

nginx location中斜线的位置的区别

作者:互联网

https://blog.csdn.net/u010509052/article/details/105455813

https://www.cnblogs.com/caibao666/p/14393870.html

 

最新在配置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
慢慢比较发现规律:

二、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指定目录下的;

标签:127.0,0.1,斜线,nginx,proxy,pass,hello,location
来源: https://www.cnblogs.com/zhao1949/p/15094350.html