Using Nginx echo module to test location matching priority

The nginx echo module allows users to directly output strings in nginx, which is convenient for users to debug when configuring nginx. Document address: https://www.nginx.com/resources/wiki/modules/echo/ , github address: https://github.com/openresty/echo-nginx-module

1, installation

Here, take nginx version: nginx/1.10.3 (Ubuntu) of ubuntu 16.04 as an example. The process is very simple:

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

Then you can see the installation process

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

Know whether to change the configuration

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

I'm here to select D first, and then check the differences. The first one is Y, and the second one is N

Then setup continues until the end

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) ...

So far, the echo module installation is complete. Confirm it.

nginx -V //View nginx versions and modules

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

You can see that the configuration parameters start with -- and there are -- with, --add, --http and so on.

 

2, grammar

Echo, echo sleep pause, echo reset time timer, echo flush clear, echo before, echo after, echo duplicate and so on can be used.

3, experiment

3.0 before the test, we start nginx to solve the corresponding problems. For example, the current nginx is executed by root, but the configuration file is www data, which has permission problems.

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

Change the user in / etc/nginx/nginx.conf file to the current user c80k2, kill the master process, and execute

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

Yes.

3.1 experiment in each location

server {
        listen 80;
        server_name www.test1.com;
        root /opt/wwwroot/test;
        #Natural matching
        location /fullpathabc {
                echo 'natural fullpathabc';
        }
        #Precise matching
        location = /fullpathabc {
                echo 'extra fullpathabc';
        }
        #prefix match
        location ^~ /fullpath {
                echo 'fullpath with prefix regular';
        }
        #Case sensitive matching
        location ~ /fullpath {
                echo 'fullpath with capital sensitive regular';
        }
        #Case insensitive match
        location ~* /Fullpath {
                echo 'fullpath with capital non-sensitive regular';
        }
        #Pass through
        location / {
                echo 'the last general match';
        }
}

3.1.1 first, let's look at the syntax rules of location: location [=|~|~*|^~] /uri / { }
=Express exact match
~- indicates case sensitive regular matching
~*Represents a case insensitive regular match (the only difference from the above is case)

^~Indicates that the uri starts with a regular string, which can be understood as matching the url path. nginx The url is not encoded, so the request is / static/20%/aa, which can be matched by the rule ^ ~ / static/ /aa (note the space).

/uri / represents a natural match based on this uri
/Universal match, any request will match to the default match, similar to the default statement in switch...case.

3.1.2 location priority

3.1.2.1 this uri will hit all rules, but return accurate rules. In order to test the next rule order, comment it out, the same below.

curl www.test1.com/fullpathabc
exact fullpathabc

3.1.2.2 next, this request will hit all the remaining rules,

curl www.test1.com/fullpathabc
fullpath with prefix regular

Prefix match first

3.1.2.3 the remaining four

curl www.test1.com/fullpathabc
fullpath with capital sensitive regular

However, it returns case sensitive regularity without hitting the natural route and prefix matching route written in front of it.

We put case sensitivity after case insensitivity,

curl www.test1.com/fullpathabc
fullpath with capital non-sensitive regular

It is found that for case sensitive and case insensitive regularities, if they match, which is the first and which is the priority, that is, they are executed according to the configuration order.

3.1.2.4 remaining two

curl www.test1.com/fullpathabc
natural fullpathabc

Natural match first

3.1.2.5 the last is general configuration

curl www.test1.com/fullpathabc
the last general match

To sum up, the matching priority of location is as follows:

Precise matching > prefix matching > case sensitive or insensitive matching (internal arranged in the order of configuration writing) > Natural matching > general matching

Keywords: Programming Nginx curl github Ubuntu

Added by sufian on Sun, 03 Nov 2019 06:01:12 +0200