如何使用Nginxecho模塊測試location匹配優(yōu)先級

本篇內(nèi)容介紹了“如何使用Nginx echo模塊測試location匹配優(yōu)先級”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)建站專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗(yàn)!已為三輪攪拌車等企業(yè)提供專業(yè)服務(wù)。

Nginx-echo模塊讓用戶可以直接在nginx中直接輸出字符串,方便用戶在做nginx配置時進(jìn)行調(diào)試。文檔地址: 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,查看差異之后,再進(jìn)行的選擇,第一個選的是Y,第二個選的是N

然后安裝程序繼續(xù)執(zhí)行,直到結(jié)束

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模塊安裝完成了。確認(rèn)一下

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

可以看到配置參數(shù)都是以 -- 開頭,有 --with, --add, --http等。

2,語法

可使用 echo, echo_sleep暫停器,echo_reset_time定時器, echo_flush清空,echo_before, echo_after前置后置, echo_duplicate重復(fù)等。

3, 試驗(yàn)

3.0 在試驗(yàn)前,我們先把nginx啟動,解決相應(yīng)的問題。比如,當(dāng)前的nginx以root用戶在執(zhí)行,但是配置文件卻是 www-data,存在權(quán)限問題。

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 改成當(dāng)前用戶 c80k2,殺掉master進(jìn)程,然后執(zhí)行

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

即可。

3.1 在各個location中實(shí)驗(yàn)

server {
        listen 80;
        server_name www.test1.com;
        root /opt/wwwroot/test;
        #精準(zhǔn)匹配
        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的語法規(guī)則: location [=|~|~*|^~] /uri/ { … }
= 表示精確匹配
~  表示區(qū)分大小寫的正則匹配
~* 表示不區(qū)分大小寫的正則匹配(和上面的唯一區(qū)別就是大小寫)

^~ 表示uri以某個常規(guī)字符串開頭,理解為匹配 url路徑即可。nginx不對url做編碼,因此請求為/static/20%/aa,可以被規(guī)則^~ /static/ /aa匹配到(注意兩個/之間是20%解碼之后的空格)。

/uri/ 表示以這個uri為基礎(chǔ)的自然匹配
/ 通用匹配,任何請求都會匹配到,默認(rèn)匹配,類似于switch...case中的default語句。

3.1.2 location優(yōu)先級

3.1.2.1 下面這個uri,會命中所有的規(guī)則,但返回的是精準(zhǔn)規(guī)則。

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

為了測試接下來的規(guī)則順序,把它注釋掉,下同。

然后把剛才的第三個location ~^ /fullpath/ 改回來,成為 location ~^ /fullpath。發(fā)現(xiàn)又出現(xiàn)錯誤,所以把第二個注釋掉,所以第二次配置是

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 接下來這次請求,會命中剩下的所有規(guī)則,

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

^~ 修飾符優(yōu)先

然后把它注釋掉,把第二個打開,第三次試驗(yàn)配置為

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

正則匹配優(yōu)先。

我們把 大小寫不敏感 放到 大小寫敏感 前面,

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

發(fā)現(xiàn) 對于大小寫敏感和不敏感正則,如果都匹配,則哪個在前,優(yōu)先使用哪個,即按照配置順序來執(zhí)行。

注釋掉,

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的匹配優(yōu)先級如下:

=修飾符前綴匹配 > ^~修飾符前綴匹配 > 大小寫敏感或不敏感匹配(內(nèi)部按配置書寫順序排列) > 無修飾符前綴匹配 > 通配

“如何使用Nginx echo模塊測試location匹配優(yōu)先級”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

當(dāng)前文章:如何使用Nginxecho模塊測試location匹配優(yōu)先級
網(wǎng)站路徑:http://muchs.cn/article26/pppjcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站制作、品牌網(wǎng)站設(shè)計、面包屑導(dǎo)航、、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司