当前位置:   article > 正文

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

nginx location alias

1.【alias】

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

  1. location /test/
  2. {
  3. alias /home/sftp/img/;
  4. }
  5. location /test/aaa/
  6. {
  7. alias /home/sftp/img/;
  8. }
  9. location /test/aaa/bbb/
  10. {
  11. alias /home/sftp/img/;
  12. }

即:请求/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配置路径附加到其后。如:

  1. location /test/
  2. {
  3. root /home/sftp/img/;
  4. }

即:请求/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如:

  1. location /test/
  2. {
  3. proxy_pass http://127.0.0.1:8080/;
  4. }
  5. location /test/aaa/
  6. {
  7. proxy_pass http://127.0.0.1:8080/;
  8. }
  9. location /test/aaa/bbb/
  10. {
  11. proxy_pass http://127.0.0.1:8080/;
  12. }

在 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如:

  1. location /test
  2. {
  3. proxy_pass http://127.0.0.1:8080;
  4. }
  5. location /test/aaa
  6. {
  7. proxy_pass http://127.0.0.1:8080;
  8. }
  9. location /test/aaa/bbb
  10. {
  11. proxy_pass http://127.0.0.1:8080;
  12. }

需要在 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目录的情况

  1. 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
  2. {
  3. proxy_pass http://127.0.0.1:8080/img; 效果一样
  4. proxy_pass http://127.0.0.1:8080/img/;
  5. }
  6. location /test/aaaa
  7. {
  8. proxy_pass http://127.0.0.1:8080/img; 效果一样,和y一开始说proxy_pass 末尾加不加“/” 行为不一样了
  9. proxy_pass http://127.0.0.1:8080/img/;
  10. }

即:请求/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是什么

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

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

  1. #全局块
  2. event{ #events块
  3. ...
  4. }
  5. http{ #http块
  6. server{ #server块
  7. ... #server全局块
  8. location{ #location块
  9. ...
  10. }
  11. location{ #location块
  12. ...
  13. }
  14. }
  15. server{ #server块
  16. ...
  17. }
  18. ... #http全局块
  19. }

 

  • http块

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

  • server块

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

  • location块

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

location 说明

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

     

  • location的分类
  1. location分为两类,一类为普通 location,一类为正则location
  2. 1普通location
  3. “普通 location ”是以“ = ”或“ ^~ ”为前缀或者没有任何前缀的 /uri/
  4. 2正则location
  5. “正则 location ”是以“ ~ ”或“ ~* ”为前缀的 /uri/

 

  • 多个location场景下的location匹配

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

 

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

  1. server {
  2. listen 9090;
  3. server_name localhost;
  4. location / {
  5. root html;
  6. index index.html index.htm;
  7. deny all;
  8. }
  9. location ~ \.html$ {
  10. allow all;
  11. }
  12. }

 

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

测试结果
 

  1. [root@web108 ~]# curl http://localhost:9090/
  2. <html>
  3. <head><title>403 Forbidden</title></head>
  4. <body bgcolor=”white”>
  5. <center><h1>403 Forbidden</h1></center>
  6. <hr><center>nginx/1.1.0</center>
  7. </body>
  8. </html>
  9. [root@web108 ~]# curl http://localhost:9090/index.html
  10. <html>
  11. <head>
  12. <title>Welcome to nginx!</title>
  13. </head>
  14. <body bgcolor=”white” text=”black”>
  15. <center><h1>Welcome to nginx!</h1></center>
  16. </body>
  17. </html>
  18. [root@web108 ~]# curl http://localhost:9090/index_notfound.html
  19. <html>
  20. <head><title>404 Not Found</title></head>
  21. <body bgcolor=”white”>
  22. <center><h1>404 Not Found</h1></center>
  23. <hr><center>nginx/1.1.0</center>
  24. </body>
  25. </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. //在例子1的基础上增加精确匹配
  2. server {
  3. listen 9090;
  4. server_name localhost;
  5. location /exact/match.html {
  6. allow all;
  7. }
  8. location / {
  9. root html;
  10. index index.html index.htm;
  11. deny all;
  12. }
  13. location ~ \.html$ {
  14. allow all;
  15. }
  16. }
  17. ————————————————
  1. [root@web108 ~]# curl http://localhost:9090/exact/match.html
  2. <html>
  3. <head><title>404 Not Found</title></head>
  4. <body bgcolor=”white”>
  5. <center><h1>404 Not Found</h1></center>
  6. <hr><center>nginx/1.1.0</center>
  7. </body>
  8. </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

  1. server {
  2. listen 9090;
  3. server_name localhost;
  4. location /exact/match.html {
  5. allow all;
  6. }
  7. location ^~ / {
  8. root html;
  9. index index.html index.htm;
  10. deny all;
  11. }
  12. location ~ \.html$ {
  13. allow all;
  14. }
  15. }

测试

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

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

  1. server {
  2. listen 9090;
  3. server_name localhost;
  4. location ~ \.html$ {
  5. allow all;
  6. }
  7. location ~ ^/prefix/.*\.html$ {
  8. deny all;
  9. }
  10. }
  11. server {
  12. listen 9090;
  13. server_name localhost;
  14. location ~ ^/prefix/.*\.html$ {
  15. deny all;
  16. }
  17. location ~ \.html$ {
  18. allow all;
  19. }
  20. }
  21. ————————————————

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/254440
推荐阅读
相关标签
  

闽ICP备14008679号