In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the introduction of Nginx regular expression and Nginx rewrite rewriting function, the content of the article is of high quality, so share it for your reference. I hope you can get something after reading this article.
1. Rewrite Jump implementation
Service agreement function module
Url resource location path
Nginx---- supports url rewriting and if condition judgment, but does not support else jumps-the loop can be executed up to 10 times, after which nginx will return 500 code errors rewrite---- uses nginx' global variables or self-set variables, combines regular expressions and flag bits to implement url rewriting and redirection
2. Rewrite uses rewrite for matching jump using if to match global variables and then jumps using location matching
1.rewrite is placed in server {}, if {}, location {} segments
two。 For domain names or parameter strings: use if global variable matching, use proxy_pass reverse proxy
3. Nginx regular expression
Common regular expression metacharacters
4. Rewrite command
Location classification
Common expressions for regular matching
5. Location priority
Sort by priority: = type ^ ~ type expression regular expression (and) type regular string match type, prefix matching universal match (/), if there is no other match, any request will match to
Location priority rules (from highest to lowest)
1. Match a specific file location = full path location ^ ~ full path location ~ * full path location ~ full path location full path location /
two。 Use directory matching to access a file location = directory location ^ ~ directory location ~ directory location ~ * directory location directory
Location /
VI. Application scenario testing
The company's old domain name www.domain.com needs to be replaced by a new domain name www.newdomain.com due to business requirements. It cannot abolish the old domain name, jump from the old domain name to the new domain name, and keep its parameters unchanged.
[root@localhost bin] # rpm- Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm get http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm warning: / var/tmp/rpm-tmp.IHyTHc: header V4 RSA/SHA1 Signature Key ID 7bd9bf62: NOKEY in preparation. # # [100%] upgrading / installing... 1:nginx-release-centos-7-0.el7.ngx # # # # [root@localhost bin] # yum install nginx-y [root@localhost ~] # mkdir / abc [root@localhost ~] # mount.cifs / / 192.168.56.1 / mntPassword for root@//192.168.254.10/linuxs: [root@localhost ~] # cd / abc/LNMP-C7/LNMP-C7/ [root@localhost LNMP-C7] # lsDiscuz_X3.4_SC_UTF8.zipmysql-boost-5.7.20.tar.gzncurses-5. 6.tar.gznginx-1.12.2.tar.gzphp-5.6.11.tar.bz2php-7.1.10.tar.bz2php-7.1.20.tar.bz2php-7.1.20.tar.gzzend-loader-php5.6-linux-x86_64_update1.tar.gz [root@localhost LNMP-C7] # tar-zxvf nginx-1.12.2.tar.gz-C / opt [root@localhost LNMP-C7] # useradd-M-s / sbin/nologin nginx [ Root@localhost LNMP-C7] # cd / opt/nginx-1.12.2/ [root@localhost nginx-1.12.2] # lsauto CHANGES.ru configure html man srcCHANGES conf contrib LICENSE README [root@localhost nginx-1.12.2] # yum install gcc gcc-c++ pcre pcre-devel make zlib-devel-y [root@localhost nginx-1.12.2] #. / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with- Http_stub_status_ module [root @ localhost nginx-1.12.2] # make & & make install [root@localhost conf] # vim nginx.conf37 server_name www.accp.com 41 access_log logs/www.accp.com/access.log main; [root@localhost conf] # yum install bind-y [root@localhost conf] # vim / etc/named.conf 13 listen-on port 53 {any;}; 21 allow-query {any;}; [root@localhost conf] # vim / etc/named.rfc1912.zones 25 zone "accp.com" IN {26 type master;27 file "accp.com.zone" 28 allow-update {none;}; 29} [root@localhost conf] # cd / var/named/ [root@localhost named] # lsdata named.ca named.localhost slavesdynamic named.empty named.loopback [root@localhost named] # cp-p named.localhost accp.com.zone [root@localhost named] # vim accp.com.zone www IN A 192.168.247.202 [root@localhost named] # systemctl start named [root@localhost named] # systemctl stop firewalld.service [root@localhost named] # setenforce 0 [root@ Localhost named] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ [root@localhost named] # cd / usr/local/nginx/ [root@localhost nginx] # lsconf html logs sbin [root@localhost nginx] # cd logs/ [root@localhost logs] # mkdir www.accp.com [root@localhost logs] # lserror.log www.accp.com [root@localhost logs] # nginx [root@localhost logs] # netstat-natp | grep 80tcp 000.0. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0. LISTEN 6
Open the win10 client, configure the dns server, and access using the domain name.
[root@localhost logs] # vim / usr/local/nginx/conf/nginx.conf location / {# Domain name Redirect'if ($host = 'www.accp.com') {' rewrite ^ / (. *) $http://www.kgc.com/$1 permanent;'} root / html; index index.html index.htm } [root@localhost logs] # vim / etc/named.rfc1912.zones zone "kgc.com" IN {type master; file "kgc.com.zone"; allow-update {none;};} [root@localhost logs] # cd / var/named/ [root@localhost named] # lsaccp.com.zone dynamic named.empty named.loopbackdata named.ca named.localhost slaves [root@localhost named] # cp-p accp.com.zone kgc.com.zone [root@localhost named] # systemctl restart named [root@localhost named] # killall-1 nginx
[root@localhost html] # vim / usr/local/nginx/conf/nginx.conf37 server_name bbs.accp.com;42 location / post {43 rewrite (. +) http://www.accp.com/bbs$1 permanent;44} [root@localhost html] # cd-/ var/named [root@localhost named] # vim accp.com.zone [root@localhost named] # cat accp.com.zone $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H); minimumNS @ A 127.0.0.1bbs IN A 192.168.247.202 [root@localhost named] # killall-3 nginx [root@localhost named] # nginx [root@localhost named] # systemctl restart named
Access redirection based on client-side IP
[root@localhost named] # vim / usr/local/nginx/conf/nginx.conf42 # set the legal IP flag 43 set $rewrite true;44 # to determine whether it is legal IP and allowed IP45 if ($remote_addr = "192.168.247.139") {46 set $rewrite false 47} 48 # disallowed IP to judge, mark 49 if ($rewrite = true) {50 rewrite (. +) / maintenance.html;51} 52 # matching mark to jump site 53 location = / maintenance.html {54 root html 55} [root@localhost named] # cd / usr/local/nginx/html/ [root@localhost html] # ls50x.html index.html [root@localhost html] # vim maintenance.html [root@localhost html] # killall-3 nginx [root@localhost html] # nginx
Jump based on parameter matching-jump to home page
[root@localhost named] # vim / usr/local/nginx/conf/nginx.conf37 server_name www.accp.com;42 if ($request_uri ~ ^ / 100-(100 | 200)-(\ d +) .html $) {43 rewrite (. *) http://www.accp.com permanent;44} [root@localhost named] # vim accp.com.zone [root@localhost named] # cat accp.com.zone $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H); minimumNS @ A 127.0.0.1www IN A 192.168.247.202 [root@localhost named] # systemctl restart named [root@localhost named] # killall-3 nginx [root@localhost named] # nginx
Jump based on all PHP files under the directory
[root@localhost named] # vim / usr/local/nginx/conf/nginx.conf42 location ~ * / upload/.*\ .php ${43 rewrite (. +) http://www.accp.com permanent 44} [root@localhost named] # nginx-tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@localhost named] # killall-3 nginx [root@localhost named] # nginx
Jump based on the most common URL request-jump to the home page
[root@localhost named] # vim / usr/local/nginx/conf/nginx.conf42 location ~ * ^ / abc/123.html {43 rewrite (. +) http://www.accp.com permanent;44}
Jump based on the most common URL request-jump to the home page
[root@localhost named] # vim / usr/local/nginx/conf/nginx.conf42 location ~ * ^ / abc/123.html {43 rewrite (. +) http://www.accp.com permanent 44} [root@localhost named] # killall-3 nginx [root@localhost named] # nginx so far, you have a basic understanding of Nginx regular expressions and Nginx rewrite rewriting, but you still need to consolidate and practice the specific usage. If you want to know more about it, please pay attention to the industry information.
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: 0
*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.