一文了解nginx的localtion匹配机制

大家好,我是早九晚十二,目前是做运维相关的工作。写博客是为了积累,希望大家一起进步!
我的主页:早九晚十二
在这里插入图片描述

匹配规则概述

nginx的location规则遵循以下规则

= 大于 ^~ 大于 ~ ~* !~ !~* 大于 /

即“精准”最大,“通用最小”规则
那么这些符号又分别代表什么呢?
不要急,马上就说。

符号含义
=代表精准匹配,写什么匹配什么,优先级最高
^~前缀匹配,没有精准匹配时,优先前缀匹配,优先级第二 。带有 ^~ 的前缀匹配成功,则立即停止其他类型匹配,普通前缀匹配(不带参数 ^~ )成功则会暂存,继续查找正则匹配
~= 和 ^~ 均未匹配成功前提下,查找正则匹配 ~ 和 ~* 。当同时有多个正则匹配时,按其在配置文件中出现的先后顺序优先匹配,命中则立即停止其他类型匹配 。当所有正则匹配均未成功时,返回暂存的普通前缀匹配(不带参数 ^~ )结果
~*同~
!~*规则同~*,但此项是代表不匹配正则
!~规则同~,但此项是代表不匹配正则
/通用匹配。优先级最低,当以上都无匹配到时,才会到通用匹配

http模块概述

了解nginx匹配规则之后,就涉及到如何配置,我们首先要先了解一下location所在的http模块。

#安装nginx
yum install nginx -y 
cat /etc/nginx/nginx.conf
#############################
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

上面是nginx安装后默认的配置文件(注释部分已清理),下面分别讲一下各个模块的含义

user:定义启动用户
worker_processes:nginx进程数,建议设置为等于CPU总核心数
error_log:错误日志路径
pid:进程文件路径
include :nginx子配置文件路径
events:全局配置,主要配置nginx性能相关参数,worker_connections 代表单个进程最大连接数(最大连接数=连接数*进程数)
http:http服务器,主要设置代理访问的一些优化参数,如缓存,上传文件限制,访问权限等。其中的server模块主要是虚拟主机的配置,用于区分不同网站,配置网站相关匹配规则,转发机制以及一些优化处理参数。在http模块中,http{}代表协议级别,server{}代表服务器级别,localtion / 代表请求级别。上面的配置就是最低优先级的通用匹配。

案例说明

我们先简单修改一下nginx配置

server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        return 300;
#使用curl测试,看到确实返回300
curl -I http://localhost
HTTP/1.1 300 
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 02:58:24 GMT
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive
  • 没有修饰符
  server {
        listen       80;
        listen       [::]:80;
        server_name  _; 
        localtion /test {
                return 300;
        }
    }
    #测试
    [root@0003 nginx]# curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 03:03:23 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Wed, 07 Sep 2022 02:53:11 GMT
Connection: keep-alive
ETag: "63180797-10"
Accept-Ranges: bytes

[root@0003 nginx]# curl -I http://localhost/test
HTTP/1.1 300 
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 03:03:26 GMT
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive
#可以发现请求http://localhost/test 是对的
  • “=”精确匹配
修改配置文件,分别返回300 400
server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        location /test {
                return 400;
        }
        location = /test {
                return 300;
        }
    }
测试发现,虽然/test在上面,但是依然返回的300,可见“=”优先级大雨“/”
[root@0003 nginx]# curl -I http://localhost/test
HTTP/1.1 300 
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 03:06:34 GMT
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive
  • “~”指定的正则表达式要区分大小写
修改nginx配置
   server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        location /test {
                return 400;
        }
        location ~ /test {
                return 500;
        }
        location = /test {
                return 300;
        }
    }
测试发现,依然返回300
[root@0003 nginx]# curl -I http://localhost/test
HTTP/1.1 300 
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 03:09:41 GMT
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive

当我们把=去除时测试发现返回500,可见=>~>/
[root@0003 nginx]# curl -I http://localhost/test
HTTP/1.1 500 Internal Server Error
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 03:10:21 GMT
Content-Type: text/html
Content-Length: 177
Connection: close
  • ^~模式匹配
    类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。
例如
server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        location ^~ /test/ {
                return 600;
        }
测试,即返回600
[root@0003 nginx]# curl -I http://localhost/test/ss
HTTP/1.1 600 
Server: nginx/1.20.1
Date: Wed, 07 Sep 2022 03:23:46 GMT
Content-Length: 0
Connection: keep-alive

总结

通过上面的小案例,我们已经验证了精确匹配时优先级最高的,但是在实际工作中,一个nginx站点会有很多匹配规则,所以对其他的匹配规则也要足够了解,避免生产故障!

码字不易,希望大家有用到的可以三连支持一波。哪里有问题的话可以指出,谢谢大家!
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早九晚十二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值