In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Nginx echo module to test location matching priority". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The Nginx-echo module allows users to output strings directly in nginx, which is convenient for users to debug when doing nginx configuration. Document address: https://www.nginx.com/resources/wiki/modules/echo/, github address: https://github.com/openresty/echo-nginx-module
1, install
Here, take the nginx version: nginx/1.10.3 (Ubuntu) version of ubuntu 16.04 as an example, the process is very simple:
Cd / etc/nginxsudo apt-get install nginx-extras
Then you can see the installation process.
Reading package lists... DoneBuilding dependency tree Reading state information... DoneThe following package was automatically installed and is no longer required:...
Know the reminder 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 always choose D first and then check the differences. The first choice is Y and the second is N.
Then the installer continues to execute 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).
At this point, the echo module installation is complete. Confirm
Nginx-V / / View nginx version and module nginx version: nginx/1.17.4built with OpenSSL 1.1.1d 10 Sep 2019 (running with OpenSSL 1.1.1c 28 May 2019) TLS SNI support enabledconfigure arguments:-- with-cc-opt='-g-O2-fPIE-fstack-protector-strong-Wformat-Werror=format-security-fPIC-Wdate-time-classified FORTIFYSOURCE2'-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 all start with-- with,-- add,-- http, and so on.
2, grammar
You can use echo, echo_sleep pause, echo_reset_time timer, echo_flush emptying, echo_before, echo_after front and rear, echo_duplicate repeat, etc.
3, experiment
3. Before the experiment, we start nginx and solve the corresponding problems. For example, the current nginx is executed as a root user, but the configuration file is www-data, and there is a permission problem.
Ps-aux | grep nginxroot 59733 0.00.4 225624 17604? Ss 10:47 0:00 nginx: master process / usr/sbin/nginx-g daemon on; master_process on;root 61083 0.00.2 225624 9904? S 11:09 0:00 nginx: worker processroot 61084 0.0 0.2 225624 9904? S 11:09 0:00 nginx: worker processroot 61085 0.0 0.2 225624 9908? S 11:09 0:00 nginx: worker processroot 61086 0.0 0.2 225624 9908? S 11:09 0:00 nginx: worker processc80k2 61126 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 then execute
Sudo nginx-c / etc/nginx/nginx.conf
That's it.
3.1 experiment with server {listen 80; server_name www.test1.com; root/ opt/wwwroot/test; # exact match location = / fullpath {echo 'prefix fullpath with = modifier';} # normal match location / fullpath {echo' prefix fullpath with no modifier' in each location } # ~ ^ match location ^ ~ / fullpath/ {echo 'prefix fullpath with ^ = modifier';} # case-sensitive regular matching location ~ / fullpath {echo' exp-reg fullpath with case-sensitive ~ modifier' } # case insensitive regular matching location ~ * / fullpath {echo 'exp-reg fullpath with case-insensitive ~ * modifier';} # Universal matching location / {echo' general match path /';}}
Note that the first and third location here were originally / fullpath, but an error will be reported if they are opened at the same time, as follows
Sudo nginx-tnginx: [emerg] duplicate location "/ fullpath" in / etc/nginx/sites-enabled/default:165nginx: configuration file / etc/nginx/nginx.conf test failed
To solve the problem, I changed the third one to location ~ ^ / fullpath/
3.1.1 first, let's look at the syntax rules of location: location [= | ~ | ~ * | ^ ~] / uri/ {… }
= indicates an exact match
~ indicates case-sensitive regular matching
~ * indicates a case-insensitive regular match (the only difference from the above is case)
^ ~ means that uri begins with a regular string and can be understood as matching the url path. Nginx does not encode url, so the request is / static/20%/aa, which can be matched by the rule ^ ~ / static/ / aa (note that there is a 20% decoded space between the two /).
/ uri/ represents a natural match based on this uri
/ Universal match, any request will be matched, default match, similar to the default statement in switch...case.
3.1.2 location priority
3.1.2.1 the following uri hits all rules, but returns precise rules.
Curl www.test1.com/fullpathprefix fullpath with = modifier
To test the order of the following rules, comment it out, the same below.
Then change the third location ~ ^ / fullpath/ back to location ~ ^ / fullpath. Found another error, so comment out the second one, so the second configuration is
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 this next request will hit all the remaining rules
Sudo curl www.test1.com/fullpathprefix fullpath with ^ = modifier
^ ~ modifier takes precedence
Then comment it out, open the second one, and configure the third test to
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
Lowercase
Sudo curl www.test1.com/fullpathexp-reg fullpath with case-sensitive ~ modifier
Uppercase
Sudo curl www.test1.com/Fullpathexp-reg fullpath with case-insensitive ~ * modifier
Regular matching takes precedence.
We put case insensitivity in front of case sensitivity.
Curl www.test1.com/fullpathabcexp-reg fullpath with case-insensitive ~ * modifier
It is found that for case-sensitive and case-insensitive regularities, if both match, which comes first and which is used first, that is, it is executed in the order of configuration.
Comment out
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 two remaining species
Sudo curl www.test1.com/fullpathprefix fullpath with no modifier
Prefix matching without modifier
3.1.2.5 finally is a wildcard.
Sudo curl www.test1.com/fullpathgeneral match /
To sum up, the matching priority of location is as follows:
= modifier prefix matching > ^ ~ modifier prefix matching > case-sensitive or insensitive matching (internally arranged by configuration writing order) > no modifier prefix matching > wildcard "how to use Nginx echo module to test location matching priority" is here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 289
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.