使用Nginx echo模塊測試location匹配優先級

Nginx-echo模塊讓用戶能夠直接在nginx中直接輸出字符串,方便用戶在作nginx配置時進行調試。文檔地址: https://www.nginx.com/resources/wiki/modules/echo/ , github地址: https://github.com/openresty/echo-nginx-modulephp

1, 安裝

這裏以ubuntu 16.04的nginx version: nginx/1.10.3 (Ubuntu) 版本做爲例子,過程很是簡單:nginx

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

而後能夠看到安裝過程git

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
.
.
.

知道提醒是否變動配置github

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,第二個選的是Nshell

而後安裝程序繼續執行,直到結束ubuntu

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模塊安裝完成了。確認一下bash

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等。dom

 

2,語法

可以使用 echo, echo_sleep暫停器,echo_reset_time定時器, echo_flush清空,echo_before, echo_after前置後置, echo_duplicate重複等。curl

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 = /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 location優先級

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的匹配優先級以下:

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

另外,location選定以後,就不會再匹配其餘的了。 

相關文章
相關標籤/搜索