本篇内容介绍了“如何使用Nginx echo模块测试location匹配优先级”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Nginx-echo模块让用户可以直接在nginx中直接输出字符串,方便用户在做nginx配置时进行调试。文档地址: https://www.nginx.com/resources/wiki/modules/echo/ , github地址: https://github.com/openresty/echo-nginx-module
这里以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-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等。
可使用 echo, echo_sleep暂停器,echo_reset_time定时器, echo_flush清空,echo_before, echo_after前置后置, echo_duplicate重复等。
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
即可。
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 -t nginx: [emerg] duplicate location "/fullpath" in /etc/nginx/sites-enabled/default:165 nginx: 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.1 下面这个uri,会命中所有的规则,但返回的是精准规则。
curl www.test1.com/fullpath prefix 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/fullpath prefix 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/fullpath exp-reg fullpath with case-sensitive ~ modifier
大写
sudo curl www.test1.com/Fullpath exp-reg fullpath with case-insensitive ~* modifier
正则匹配优先。
我们把 大小写不敏感 放到 大小写敏感 前面,
curl www.test1.com/fullpathabc exp-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/fullpath prefix fullpath with no modifier
不带修饰符的前缀匹配
3.1.2.5 最后是通配
sudo curl www.test1.com/fullpath general match /
综上所述,location的匹配优先级如下:
=修饰符前缀匹配 > ^~修饰符前缀匹配 > 大小写敏感或不敏感匹配(内部按配置书写顺序排列) > 无修饰符前缀匹配 > 通配
“如何使用Nginx echo模块测试location匹配优先级”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。