使用Nginxecho模块测试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/nginx

sudo apt-get install nginx-extras

然后可以看到安装过程

Reading package lists... Done

Building dependency tree

Reading state information... Done

The 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.4

built with OpenSSL 1.1.1d 10 Sep 2019 (running with OpenSSL 1.1.1c 28 May 2019)

TLS SNI support enabled

configure arguments: --with-cc-opt="-g -O2 -fPIE -fstack-protector-span -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 nginx

root 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 process

root 61084 0.0 0.2 225624 9904 ? S 11:09 0:00 nginx: worker process

root 61085 0.0 0.2 225624 9908 ? S 11:09 0:00 nginx: worker process

root 61086 0.0 0.2 225624 9908 ? S 11:09 0:00 nginx: worker process

c80k2 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 /fullpathabc {

echo "natural fullpathabc";

}

#精准匹配

location = /fullpathabc {

echo "extra fullpathabc";

}

#前缀匹配

location ^~ /fullpath {

echo "fullpath with prefix regular";

}

#大小写敏感匹配

location ~ /fullpath {

echo "fullpath with capital sensitive regular";

}

#大小写不敏感匹配

location ~* /Fullpath {

echo "fullpath with capital non-sensitive regular";

}

#通配

location / {

echo "the last general match";

}

}

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

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

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

3.1.2 location优先级

3.1.2.1 这个uri,会命中所有的规则,但返回的是精准规则。为了测试接下来的规则顺序,把它注释掉,下同。

curl www.test1.com/fullpathabc

exact fullpathabc

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

curl www.test1.com/fullpathabc

fullpath with prefix regular

前缀匹配优先

3.1.2.3 剩下四种

curl www.test1.com/fullpathabc

fullpath with capital sensitive regular

但是返回的是 大小写敏感的正则,而没有命中 写在它前面的自然路由 和 前缀匹配路由。

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

curl www.test1.com/fullpathabc

fullpath with capital non-sensitive regular

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

3.1.2.4  剩下两种

curl www.test1.com/fullpathabc

natural fullpathabc

自然匹配优先

3.1.2.5 最后是通配

curl www.test1.com/fullpathabc

the last general match

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

精准匹配 > 前缀匹配 > 大小写敏感或不敏感匹配(内部按配置书写顺序排列) > 自然匹配 > 通配

以上是 使用Nginxecho模块测试location匹配优先级 的全部内容, 来源链接: utcz.com/z/510354.html

回到顶部