千家信息网

如何使用Nginx echo模块测试location匹配优先级

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容介绍了"如何使用Nginx echo模块测试location匹配优先级"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家
千家信息网最后更新 2026年11月01日如何使用Nginx echo模块测试location匹配优先级

本篇内容介绍了"如何使用Nginx echo模块测试location匹配优先级"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Nginx-echo模块让用户可以直接在nginx中直接输出字符串,方便用户在做nginx配置时进行调试。文档地址: https://www.nginx.com/resources/wiki/modules/echo/ , github地址: https://github.com/openresty/echo-nginx-module

1, 安装

这里以ubuntu 16.04的nginx version: nginx/1.10.3 (Ubuntu) 版本作为例子,过程非常简单:

cd /etc/nginxsudo apt-get install nginx-extras

然后可以看到安装过程

Reading package lists... DoneBuilding dependency tree       Reading state information... DoneThe following package was automatically installed and is no longer required:...

知道提醒是否变更配置

Configuration file '/etc/nginx/nginx.conf' ==> Modified (by you or by a script) since installation. ==> Package distributor has shipped an updated version.   What would you like to do about it ?  Your options are:    Y or I  : install the package maintainer's version    N or O  : keep your currently-installed version      D     : show the differences between the versions      Z     : start a shell to examine the situation The default action is to keep your current version.*** nginx.conf (Y/I/N/O/D/Z) [default=N] ?
Configuration file '/etc/nginx/sites-available/default' ==> Modified (by you or by a script) since installation. ==> Package distributor has shipped an updated version.   What would you like to do about it ?  Your options are:    Y or I  : install the package maintainer's version    N or O  : keep your currently-installed version      D     : show the differences between the versions      Z     : start a shell to examine the situation The default action is to keep your current version.*** default (Y/I/N/O/D/Z) [default=N] ? D

我这里都是先D,查看差异之后,再进行的选择,第一个选的是Y,第二个选的是N

然后安装程序继续执行,直到结束

Installing new version of config file /etc/nginx/snippets/fastcgi-php.conf ...Removing obsolete conffile /etc/init/nginx.conf ...Setting up libnginx-mod-http-auth-pam (1.17.4-1+ubuntu16.04.1+deb.sury.org+3) ......Processing triggers for libc-bin (2.23-0ubuntu11) ...

到此为止,echo模块安装完成了。确认一下

nginx -V //查看nginx版本和模块nginx version: nginx/1.17.4built with OpenSSL 1.1.1d  10 Sep 2019 (running with OpenSSL 1.1.1c  28 May 2019)TLS SNI support enabledconfigure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-headers-more-filter --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-auth-pam --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-cache-purge --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-dav-ext --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-ndk #### --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-echo  ####--add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-fancyindex --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/nchan --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-lua --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/rtmp --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-uploadprogress --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-upstream-fair --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-subs-filter --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/ssl-ct --add-dynamic-module=/build/nginx-XRuY5x/nginx-1.17.4/debian/modules/http-geoip2

可以看到配置参数都是以 -- 开头,有 --with, --add, --http等。

2,语法

可使用 echo, echo_sleep暂停器,echo_reset_time定时器, echo_flush清空,echo_before, echo_after前置后置, echo_duplicate重复等。

3, 试验

3.0 在试验前,我们先把nginx启动,解决相应的问题。比如,当前的nginx以root用户在执行,但是配置文件却是 www-data,存在权限问题。

ps -aux | grep nginxroot      59733  0.0  0.4 225624 17604 ?        Ss   10:47   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;root      61083  0.0  0.2 225624  9904 ?        S    11:09   0:00 nginx: worker processroot      61084  0.0  0.2 225624  9904 ?        S    11:09   0:00 nginx: worker processroot      61085  0.0  0.2 225624  9908 ?        S    11:09   0:00 nginx: worker processroot      61086  0.0  0.2 225624  9908 ?        S    11:09   0:00 nginx: worker processc80k2     61126  0.0  0.0  21296   880 pts/26   S+   11:10   0:00 grep --color=auto nginx

将/etc/nginx/nginx.conf 文件中的 user 改成当前用户 c80k2,杀掉master进程,然后执行

sudo nginx -c /etc/nginx/nginx.conf

即可。

3.1 在各个location中实验

server {        listen 80;        server_name www.test1.com;        root /opt/wwwroot/test;        #精准匹配        location = /fullpath {                echo 'prefix fullpath with = modifier';        }        #普通匹配        location /fullpath {                echo 'prefix fullpath with no modifier';        }        #~^匹配        location ^~ /fullpath/ {                echo 'prefix fullpath with ^= modifier';        }        #大小写敏感正则匹配        location ~ /fullpath {                echo 'exp-reg fullpath with case-sensitive ~ modifier';        }        #大小写不敏感正则匹配        location ~* /fullpath {                echo 'exp-reg fullpath with case-insensitive ~* modifier';        }        #通用匹配        location / {                echo 'general match path /';        }}

注意,这里的第一个和第三个location,原本都是/fullpath,但是同时打开会报错,如下

sudo nginx -tnginx: [emerg] duplicate location "/fullpath" in /etc/nginx/sites-enabled/default:165nginx: configuration file /etc/nginx/nginx.conf test failed

为了解决错误,我把第三个改为了 location ~^ /fullpath/

3.1.1 首先,我们来看location的语法规则: location [=|~|~*|^~] /uri/ { … }
= 表示精确匹配
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配(和上面的唯一区别就是大小写)

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

/uri/ 表示以这个uri为基础的自然匹配
/ 通用匹配,任何请求都会匹配到,默认匹配,类似于switch...case中的default语句。

3.1.2 location优先级

3.1.2.1 下面这个uri,会命中所有的规则,但返回的是精准规则。

curl www.test1.com/fullpathprefix fullpath with = modifier

为了测试接下来的规则顺序,把它注释掉,下同。

然后把刚才的第三个location ~^ /fullpath/ 改回来,成为 location ~^ /fullpath。发现又出现错误,所以把第二个注释掉,所以第二次配置是

server {        listen 80;        server_name www.test1.com;        root /opt/wwwroot/test;        #location = /fullpath {        #        echo 'prefix fullpath with = modifier';        #}        #location /fullpath {        #        echo 'prefix fullpath with no modifier';        #}        location ^~ /fullpath {                echo 'prefix fullpath with ^= modifier';        }        location ~ /fullpath {                echo 'exp-reg fullpath with case-sensitive ~ modifier';        }        location ~* /fullpath {                echo 'exp-reg fullpath with case-insensitive ~* modifier';        }        location / {                echo 'general match path /';        }}

3.1.2.2 接下来这次请求,会命中剩下的所有规则,

sudo curl www.test1.com/fullpathprefix fullpath with ^= modifier

^~ 修饰符优先

然后把它注释掉,把第二个打开,第三次试验配置为

server {        listen 80;        server_name www.test1.com;        root /opt/wwwroot/test;        #location = /fullpath {        #        echo 'prefix fullpath with = modifier';        #}        #location ^~ /fullpath {        #        echo 'prefix fullpath with ^= modifier';        #}        location /fullpath {                echo 'prefix fullpath with no modifier';        }        location ~ /fullpath {                echo 'exp-reg fullpath with case-sensitive ~ modifier';        }        location ~* /fullpath {                echo 'exp-reg fullpath with case-insensitive ~* modifier';        }        location / {                echo 'general match path /';        }}

3.1.2.3

小写

sudo curl www.test1.com/fullpathexp-reg fullpath with case-sensitive ~ modifier

大写

sudo curl www.test1.com/Fullpathexp-reg fullpath with case-insensitive ~* modifier

正则匹配优先。

我们把 大小写不敏感 放到 大小写敏感 前面,

curl www.test1.com/fullpathabcexp-reg fullpath with case-insensitive ~* modifier

发现 对于大小写敏感和不敏感正则,如果都匹配,则哪个在前,优先使用哪个,即按照配置顺序来执行。

注释掉,

server {        listen 80;        server_name www.test1.com;        root /opt/wwwroot/test;        #location = /fullpath {        #        echo 'prefix fullpath with = modifier';        #}        #location ^~ /fullpath {        #        echo 'prefix fullpath with ^= modifier';        #}        location /fullpath {                echo 'prefix fullpath with no modifier';        }        #location ~* /fullpath {        #       echo 'exp-reg fullpath with case-insensitive ~* modifier';        #}        #location ~ /fullpath {        #        echo 'exp-reg fullpath with case-sensitive ~ modifier';        #}        location / {                echo 'general match path /';        }}

3.1.2.4 剩下两种

sudo curl www.test1.com/fullpathprefix fullpath with no modifier

不带修饰符的前缀匹配

3.1.2.5 最后是通配

sudo curl www.test1.com/fullpathgeneral match /

综上所述,location的匹配优先级如下:

=修饰符前缀匹配 > ^~修饰符前缀匹配 > 大小写敏感或不敏感匹配(内部按配置书写顺序排列) > 无修饰符前缀匹配 > 通配

"如何使用Nginx echo模块测试location匹配优先级"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

大小 配置 正则 规则 模块 优先级 前缀 注释 用户 测试 接下来 三个 过程 顺序 试验 精准 内容 地址 字符 字符串 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 百单网互联网科技有限公司怎么样 互联网科技有限公司经营范围 疫情对互联网科技公司影响 互联网金融公司科技 深圳互联网络科技有限公司 成都互联网科技公司 人人互联网科技是培训 互联网科技类展会 互联网巨头投资的硬核科技 云创客互联网科技公司 常德市电业局互联网科技部 顶级互联网科技成果有哪些 晓花互联网科技有限公司网址 搜狐科技互联网 浙南科技城互联网大厦建设工程 企晟互联网信息科技有限公司 洛阳共同富裕互联网科技有限公司 互联网科技和法律行业 南博望互联网科技有限公司 东风日产互联网科技公司叫什么 耶加雪菲 关于新基建和互联网科技 融势互联网科技 浙南科技城互联网大厦建设工程 互联网企业科技类企业如何做账 浙南科技城互联网大厦招聘 半导体是互联网高科技吗 易租星互联网科技有限公司 互联网科技自媒体灵感 科技互联网企业在行动
0