Nginx——location常见配置指令,alias、root、proxy_pass 路径问题

1.【alias】

别名配置,用于访问文件系统,在匹配到location配置的URL路径后,指向【alias】配置的路径。如:

location /test/ 
{ 
alias /home/sftp/img/; 
}
location /test/aaa/
{ 
alias /home/sftp/img/; 
}
location /test/aaa/bbb/
{ 
alias /home/sftp/img/; 
}

即:请求/test/1.jpg、/test/aaa/1.jpg、/test/aaa/bbb/1.jpg(省略了协议与域名),将会返回文件/home/sftp/img/1.jpg。

注意alias 后面有没有“/” 要和location 后面“/” 保持一致,否则找不到资源文件

2.【root】

根路径配置,用于访问文件系统,在匹配到location配置的URL路径后,指向【root】配置的路径,并把location配置路径附加到其后。如:

location /test/ 
{ 
root /home/sftp/img/; 
}

即:请求/test/1.jpg(省略了协议与域名),将会返回文件/home/sftp/img/test/1.jpg,相较于alias,使用root会把/test/附加到根目录之后。

3.【proxy_pass】

反向代理配置,用于代理请求,适用于前后端负载分离或多台机器、服务器负载分离的场景,在匹配到location配置的URL路径后,转发请求到【proxy_pass】配置的URL,是否会附加location配置路径与【proxy_pass】配置的路径后是否有"/"有关,有"/"则不附加,proxy_pass 带“/”类似于alias如:

location /test/ 
{ 
proxy_pass http://127.0.0.1:8080/; 
}
location /test/aaa/
{ 
proxy_pass http://127.0.0.1:8080/; 
}
location /test/aaa/bbb/
{ 
proxy_pass http://127.0.0.1:8080/; 
}

在 tomcat的webapp/ROOT/ 放一个1.png图片

即:请求/test/1.jpg、/test/aaa/1.jpg、/test/aaa/bbb/1.jpg(省略了协议与域名),将会被nginx转发请求到http://127.0.0.1:8080/1.jpg(未附加/test/和/test子目录路径)。

 

proxy_pass 不带“/”类似于root如:

location /test
{ 
proxy_pass http://127.0.0.1:8080; 
}
location /test/aaa
{ 
proxy_pass http://127.0.0.1:8080; 
}
location /test/aaa/bbb
{ 
proxy_pass http://127.0.0.1:8080; 
}

需要在 tomcat webapp/ROOT/创建aaa/bbb 目录 之后把1.png方式aaa和bbb目录中

即:请求/test/1.jpg,/test/aaa/1.jpg,/test/aaa/bbb/1.jpg(省略了协议与域名),将会被nginx转发请求到http://127.0.0.1:8080/test/1.jpg,http://127.0.0.1:8080/test/aaa/1.jpg,http://127.0.0.1:8080/test/aaa/bbb/1.jpg(附加/test/以及子目录路径)。

 

 

以下特殊 是proxy_pass http://127.0.0.1:8080/img;  带img目录的情况

location /test 不能加“/” 如果加了,那么这个proxy_pass http://127.0.0.1:8080/img;也得加proxy_pass http://127.0.0.1:8080/img/;   否则http://127.0.0.1:8080/img1.png
{ 
proxy_pass http://127.0.0.1:8080/img;    效果一样 
proxy_pass http://127.0.0.1:8080/img/;
}

location /test/aaaa 
{ 
proxy_pass http://127.0.0.1:8080/img;   效果一样,和y一开始说proxy_pass 末尾加不加“/” 行为不一样了
proxy_pass http://127.0.0.1:8080/img/; 
}

即:请求/test/1.jpg、/test/aaa/1.jpg(省略了协议与域名),将会被nginx转发请求到http://127.0.0.1:8080/img/1.jpg(未附加/test/和子目录路径)。

 

 

 

匹配规则:

location语法

location [=|~|~*|^~] /uri/ { … }

= 开头表示精确匹配  此时和location中的root没关系了。至于location外面的root才能影响= 的配置路径

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。
 

第一:一般匹配和精准匹配

1、精准匹配和一般匹配,uri后面不带“/”匹配

如图:

请求URL:http://192.168.60.106/zg/  访问时匹配的是:/zg

 

2、精准匹配和一般匹配,uri前面和后面都不带“/”

如图:

请求URL:http://192.168.60.106/zg/ 访问时匹配的是:= zg

 

3、精准匹配和一般匹配,uri带"/"和不带"/"匹配

如图:

请求URL:http://192.168.60.106/zg/ 访问时匹配的是:/zg/ 顺序换也是一样

综上所述:路径相同时的精准匹配优先,必须是满足/uri/或者uri,要么uri两边/,要么uri两边都不加斜杆的情况

 

4、精准匹配 就是访问路径和location 后面的完全一样 包括(前后的"/")

server {
        listen       80;
        server_name  localhost;
        root /opt/wubo; 起作用 全局

         index 1.html; 起作用    全局
        location = /jetto {  

                root和alias此时不起作用;

                index index.html;也不起作用;
        }

正则

        location ~ /jetto/.*/\.html {  

                root和alias此时不起作用;

                index index.html;也不起作用;
        }

正则

    location  ~ /wubo/(.*\.mp4) {

            root和alias此时起作用;

            alias video/mp4/$1;
            #echo_sleep 4;
            #echo "2222";
            #alias video/mp4;
            #mp4;
            #index wubo.mp4;
        }

正则

      location ~ /jetto {  

                root和alias此时起作用;

                index index.html;也起作用;
        }

 

}

访问路径:http://ip:port/jetto    本地真实路径:/opt/wubo/jetto  如果没有上面红色部分默认是/opt/wubo/html/jetto

5、通用匹配 以最长uri匹配优先

server {
        listen       80;
        server_name  localhost;
        root /opt/wubo; 起作用 全局

         index 1.html; 起作用    全局
        location  /jetto {  

            #root alias index起作用,此时root alias的路径想对与全局的相对路径;index 会覆盖全局 index 的默认值
            #root  /opt/wubo/html;  # 本地真实路径/opt/wubo/html/jetto
            #root  html;           #本地真实路径/opt/wubo/html/jetto
            #root  /opt/wubo/jettopro;   #本地真实路径/opt/wubo/jettopro/jetto
            #root   jettopro;            #本地真实路径/opt/wubo/jettopro/jetto
            #alias  /opt/wubo/jettoapi;   #本地真实路径/opt/wubo/jettoapi
            alias   jettoapi;            #本地真实路径/opt/wubo/jettoapi 
            index index.html jwplayer.html; 
        }

}

访问路径:http://ip:port/jetto    本地真实路径:/opt/wubo/jetto/1.html  如果没有上面红色部分默认是/opt/wubo/html/jetto/index.html

第二:^~开头的非正则匹配和一般匹配

^~代表非正则匹配,非正则,不需要继续正则匹配。

^~

^~:如果这个匹配使用^〜前缀,搜索停止。这个前缀官网和网上都说得很含糊,加上这个前缀,是会停止搜索正则匹配,但是对一般匹配是不会停止的,也就是说还是可以匹配到一般匹配的。

请求url: http://ip:port/images/aa/test.jpg,匹配结果:/images/aa/

http://ip:port/api/                                     匹配结果:/jettoapi/index.html

http://ip:port/api/wubo                                     匹配结果:/jettoapi/index.html

意思就是说^~的作用对一般匹配不会停止,对正则匹配会停止正则匹配是: ~

7、^~开头的非正则匹配和正则匹配

~ 开头表示区分大小写的正则匹配

如图:

 

请求url: http://192.168.60.106/images/aa/test.jpg,匹配结果:^~/images/ 

  location /api {
            alias   jettoapi;            #/opt/wubo/jettoapi
            index 1.html;
        }

访问路径:http://ip:port/api/wubo   本地路径:/opt/wubo/jettoapi/wubo/1.html

location /api/wubo {
            alias   jettoapi;            #/opt/wubo/jettoapi
            index 1.html;
        }

访问路径:http://ip:port/api/wubo   本地路径:/opt/wubo/jettoapi/1.html

访问路径和location中的路径完全匹配的话 就不需要在本地真实路径下有对应的访问路径,如果location的路径只是访问路径中一部分,则需要在本地路径有对应访问中的路径

8、严格精准匹配和正则匹配

如图:

严格精准匹配,如果被严格精准匹配到了,则不会继续搜索正则匹配

如果http://192.168.60.106,这个就严格精准匹配到了 /,则不会继续匹配 ~ \.html$

如果:http://192.168.60.106/index.html,则会被/ 匹配到,但不是严格精准匹配,则会继续搜索正则匹配
 

9、正则匹配规则

都是正则uri的情况下,匹配是按照编辑顺序的

如图

请求URL:http://192.168.60.106/prefix/index.html,会优先匹配前面定义的location。

10、@开头的uri

如图:

@开头的,如果请求的 URI 存在,则本 nginx 返回对应的页面;如果不存在,则把请求代理到baidu.com 上去做个弥补,其实就是做了一个容错,把找不到的url全部转发到fallback的反向代理服务器去。

 

最后总结:

1. 先判断精准命中,如果命中,立即返回结果并结束解析过程

2. 判断普通命中,如果有多个命中,记录下来最长的命中结果

3、如果是^~开头的命中,则不会继续搜索正则命中,但是会继续搜索一般命中

4. 继续判断正则表达式的解析结果,按配置里的正则表达式顺序为准,由上到下开始匹配,一旦匹配成功立刻返回结果,并结束解析过程。

延伸分析:a. 普通命中:顺序无所谓,是因为按命中长短来确定的   b. 正则命中:顺序有所谓,因为是从前往后命中的

 

注意:当和正则一起使用寻找目录的时候,以下一定是配套的使用。A相当于定义局部资源目录,B相当于在A定义好的资源目录里面寻找资源

C是在全局定义一个资源目录

A: location /wubo {
            alias   video/mp4;           #/opt/wubo/video/mp4
        }
B:  location  ~ /wubo/\.mp4$ {
            mp4;
            #limit_conn addr 20;
            #limit_rate 2000k;  
        }

访问:http://ip:port/wubo/wubo.mp4

server: root  /opt/wubo/video/mp4;

C:location  ~ \.mp4$ {
            mp4;
            #limit_conn addr 20;
            #limit_rate 2000k;  
        }

访问:http://ip:port/wubo.mp4

 

https://blog.csdn.net/shangrila_kun/article/details/89643964

Nginx是什么

Nginx(发音同engine x)是一个异步框架的 Web服务器,也可以用作反向代理,负载平衡器 和 HTTP缓存。
也有人这么解释
nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;
同时也是一个IMAP、POP3、SMTP代理服务器;
nginx可以作为一个HTTP服务器进行网站的发布处理,
另外nginx可以作为反向代理进行负载均衡的实现。

nginx.conf的目录结构
nginx.conf的目录结构
整个conf文件分为** 全局块、events块、http块、server块、location块**。每个块有每个块的作用域,越外层的块作用域就包含内部块的作用域,如全局块作用域就包含events块、http块、server块和location块
 

                  #全局块

event{              #events块
    ...
}

http{               #http块

    server{         #server块
        ...         #server全局块

        location{   #location块
            ...
        }

        location{   #location块
            ...
        }
    }

    server{         #server块
        ...
    }
    ...             #http全局块
}

 

  • http块

http块是Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模块的配置都可以放在这模块中。作用包括:文件引入、MIME-Type定义、日志自定义、是否使用sendfile传输文件、连接超时时间、单连接请求数上限等。

  • server块

server块,虚拟主机(虚拟服务器)。作用:使得Nginx服务器可以在同一台服务器上至运行一组Nginx进程,就可以运行多个网站。

  • location块

location块是server块的一个指令。作用:基于Nginx服务器接收到的请求字符串,虚拟主机名称(ip,域名)、url匹配,对特定请求进行处理。

location 说明

  • location语法
    location [=|~|~*|^~|@] /uri/ { … } ,意思是可以以“ = ”或“ ~* ”或“ ~ ”或“ ^~ ”或“ @ ”符号为前缀,
    当然也可以没有前缀(因为 [A] 是表示可选的 A ; A|B 表示 A 和 B 选一个),紧接着是 /uri/ ,
    再接着是{…} 指令块,整个意思是对于满足这样条件的 /uri/ 适用指令块 {…} 的指令。

     

  • location的分类
location分为两类,一类为普通 location,一类为正则location。

1普通location
“普通 location ”是以“ = ”或“ ^~ ”为前缀或者没有任何前缀的 /uri/ 
2正则location
“正则 location ”是以“ ~ ”或“ ~* ”为前缀的 /uri/

 

  • 多个location场景下的location匹配

Nginx 的 location 匹配规则是:“正则 location ”让步 “普通 location”的严格精确匹配结果;但覆盖 “普通 location ”的最大前缀匹配结果。

 

例子1 先普通location,再正则location匹配

server {
      listen       9090;
      server_name  localhost;
          location / {
          root   html;
          index  index.html index.htm;
          deny all;
      }
      location ~ \.html$ {
          allow all;
      }
}

 

location / {… deny all;} 普通 location 以“ / ”开始的 URI 请求(注意任何 HTTP 请求都必然以“/ ”开始,所以“ / ”的意思是所有的请求都能被匹配上),都拒绝访问; location ~.html$ {allow all;} 正则 location以 .html 结尾的 URI 请求,都允许访问。

测试结果
 

[root@web108 ~]# curl http://localhost:9090/
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor=”white”>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.1.0</center>
</body>
</html>

[root@web108 ~]# curl http://localhost:9090/index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor=”white” text=”black”>
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>

[root@web108 ~]# curl http://localhost:9090/index_notfound.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor=”white”>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.1.0</center>
</body>
</html>


curl http://localhost:9090/ 的结果是“ 403 Forbidden ”,说明被匹配到“ location / {…deny all;} ”了,原因很简单HTTP 请求 GET / 被“严格精确”匹配到了普通 location / {} ,则会停止搜索正则 location ;
curl http://localhost:9090/index.html 结果是“ Welcome to nginx! ”,说明没有被“ location / {…deny all;} ”匹配,否则会 403 Forbidden ,但 /index.html 的确也是以“ / ”开头的,只不过此时的普通 location / 的匹配结果是“最大前缀”匹配,所以 Nginx 会继续搜索正则 location , location ~ .html$ 表达了以 .html 结尾的都 allow all; 于是接着就访问到了实际存在的 index.html 页面。
curl http://localhost:9090/index_notfound.html 同样的道理先匹配 location / {} ,但属于“普通 location 的最大前缀匹配”,于是后面被“正则 location ” location ~ .html$ {} 覆盖了,最终 allow all ; 但的确目录下不存在index_notfound.html 页面,于是 404 Not Found 。
如果此时我们访问 http://localhost:9090/index.txt 会是什么结果呢?显然是 deny all ;因为先匹配上了 location / {…deny all;} 尽管属于“普通 location ”的最大前缀匹配结果,继续搜索正则 location ,但是 /index.txt 不是以 .html结尾的,正则 location 失败,最终采纳普通 location 的最大前缀匹配结果,于是 deny all 了。

 


例子2 普通 location 的“隐式”严格匹配

 

//在例子1的基础上增加精确匹配
server {
       listen       9090;
       server_name  localhost;
        location /exact/match.html {
           allow all;
       }
           location / {
           root   html;
           index  index.html index.htm;
           deny all;
       }
       location ~ \.html$ {
           allow all;
       }
}
————————————————
[root@web108 ~]# curl http://localhost:9090/exact/match.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor=”white”>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.1.0</center>
</body>
</html>

结果进一步验证了“普通 location ”的“严格精确”匹配会终止对正则 location 的搜索。这里我们小结下“普通 location”与“正则 location ”的匹配规则:先匹配普通 location ,再匹配正则 location ,但是如果普通 location 的匹配结果恰好是“严格精确( exact match )”的,则 nginx 不再尝试后面的正则 location ;如果普通 location 的匹配结果是“最大前缀”,则正则 location 的匹配覆盖普通 location 的匹配。也就是前面说的“正则 location 让步普通location 的严格精确匹配结果,但覆盖普通 location 的最大前缀匹配结果”。
————————————————

普通 location 的“显式”严格匹配和“ ^~ ” 前缀
上面我们演示的普通 location 都是不加任何前缀的,其实普通 location 也可以加前缀:“ ^~ ”和“ = ”。其中“ ^~”的意思是“非正则,不需要继续正则匹配”,也就是通常我们的普通 location ,还会继续搜索正则 location (恰好严格精确匹配除外),但是 nginx 很人性化允许配置人员告诉 nginx 某条普通 location ,无论最大前缀匹配,还是严格精确匹配都终止继续搜索正则 location ;而“ = ”则表达的是普通 location 不允许“最大前缀”匹配结果,必须严格等于,严格精确匹配。
————————————————
例子3

server {
       listen       9090;
       server_name  localhost;
          location /exact/match.html {
           allow all;
       }
           location ^~ / {
           root   html;
           index  index.html index.htm;
           deny all;
       }
       location ~ \.html$ {
           allow all;
       }
}

测试

curl http://localhost:9090/	403 Forbidden	403 Forbidden
curl http://localhost:9090/index.html	Welcome to nginx!	403 Forbidden
curl http://localhost:9090/index_notfound.html	404 Not Found	403 Forbidden
curl http://localhost:9090/exact/match.html	404 Not Found	404 Not Found
————————————————

正则 location 与编辑顺序
location 的指令与编辑顺序无关,这句话不全对。对于普通 location 指令,匹配规则是:最大前缀匹配(与顺序无关),如果恰好是严格精确匹配结果或者加有前缀“ ^~ ”或“ = ”(符号“ = ”只能严格匹配,不能前缀匹配),则停止搜索正则 location ;但对于正则 location 的匹配规则是:按编辑顺序逐个匹配(与顺序有关),只要匹配上,就立即停止后面的搜索。
 

server {
       listen       9090;
       server_name  localhost;
       location ~ \.html$ {
           allow all; 
       }  
       location ~ ^/prefix/.*\.html$ {
           deny all;  
       }  
}
server {
       listen       9090;
       server_name  localhost;
       location ~ ^/prefix/.*\.html$ {
           deny all;  
       }  
        location ~ \.html$ {
           allow all; 
       } 
}
————————————————

 

curl http://localhost:9090/regextest.html	404 Not Found	404 Not Found
curl http://localhost:9090/prefix/regextest.html	404 Not Found	403 Forbidden
Location ~ ^/prefix/.*\.html$ {deny all;} 表示正则 location 对于以 /prefix/ 开头,
 .html 结尾的所有 URI 请求,都拒绝访问;   location ~\.html${allow all;} 
 表示正则 location 对于以 .html 结尾的 URI 请求,都允许访问。 实际上,
 prefix 的是 ~\.html$ 的子集。在“配置 3.1 ”下,两个请求都匹配上 location ~\.html$ {allow all;} ,
 并且停止后面的搜索,于是都允许访问, 404 Not Found ;在“配置 3.2 ”下, /regextest.html 无法匹配 prefix ,
 于是继续搜索 ~\.html$ ,允许访问,于是 404 Not Found ;然而 /prefix/regextest.html 匹配到 prefix ,
 于是 deny all , 403 Forbidden 。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx是一款流行的高性能Web服务器和反向代理服务器。LocationRootAlias和正则都是Nginx中非常常见的关键字。 Location指令用于配置站点的URL路径。例如,一个location /images的指令就能匹配站点中/images路径的请求。还可以使用正则表达式来匹配复杂的URL。Location指令中的一些常见选项包括try_files、proxy_pass、fastcgi_pass等。使用这些选项,我们可以根据需求配置站点的页面。 Root指令用于设置站点的根目录。例如,root /var/www/html可以将站点文件的默认存放路径设置为/var/www/html。对于一些需要访问静态文件的站点,设置Root指令可以更方便地读取静态文件,提高访问效率。 Alias指令也用于设置文件路径。不同于Root指令Alias指令能够为特定路径设置不同的读取路径,而非设定整个站点的根目录。例如,alias /images/ /data/images/,访问/images/路径时,Nginx会自动映射到/data/images/路径。 正则表达式可以让我们更灵活地配置站点。在Nginx中,用~或~*修饰location指令的URI参数,就可以开启正则表达式的匹配模式。在指令中使用正则表达式,可以用来匹配更多的路径,而不单单是固定的路径。例如,location ~ \.(gif|jpg|jpeg)$ { … }表示nginx会匹配以.gif、.jpg、.jpeg结尾的URI。 总之,通过熟练掌握NginxLocationRootAlias和正则等指令,能够更好地配置和优化站点的性能和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值