您好,登錄后才能下訂單哦!
本篇內容介紹了“如何使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。