Usage and configuration of rewrite and if in nginx

Usage and configuration of rewrite and if in nginx

1. Rewrite application

rewrite syntax

rewrite  <正则表达式>  <跳转后的内容>  [rewrite支持的flag标记]

1. Rewrite jump scene

  • The URL looks more standardized and reasonable
  • Enterprises will disguise dynamic URL addresses as static addresses to provide services
  • After the URL is changed to a new domain name, let the old visits jump to the new domain name
  • Some business adjustments on the server side

2. Rewrite the actual scene

  • 1.3.1 Implementation of Nginx jump requirements
    Use rewrite to match and jump
    Use if to match global variables and then jump
    Use location to match and then jump
  • 1.3.2 rewrite is placed in the server{}, if{}, location{} sections.
    location only works on the string after the domain name except the passed parameters

  • 1.3.3 Use if global variable matching for domain names or parameter strings
    Use proxy_pass reverse proxy

3. Commonly used nginx regular expressions

^:匹配输入字符串的起始位置
$:匹配输入字符串的结束位置
*****:匹配前面的字符零次或多次
+:匹配前面的字符一次或多次
?:匹配前面的字符零次或一次
.:匹配除\n之外的任何单个字符 使用[.\n]可以匹配包括\n在内的任意字符
****:转义符
\d:匹配纯数字
{n}:重复n次
{n,}:重复n次或更多次
[c]:匹配单个字符c
[a-z]:匹配a-z小写字母的任意一个
[a-zA-Z]:匹配a-z小写字母或A-Z大写字母的任意一个

4. Common flags

flag effect
last Basically, this flag is used to indicate that the current match is over, and the next match is continued, with a maximum of 10 to 20 matches. Once the rewrite rule is rewritten, it will no longer be processed by other rewrite rules later. Instead, the UserAgent will re-initiate a request for the rewritten URL and perform a similar process from the beginning.
break Abort Rewrite, do not continue to match Once this rewrite rule is rewritten, the UserAgent will re-initiate the request for the new URL, and it will no longer be checked by any rewrite rules in the current location
redirect Return the new URL with HTTP status 302 temporarily redirected
permanent Return the new URL with HTTP status 301 for permanent redirection

After last and break are redirected, the address bar will not change. This is their similarity. The difference is that last will be written in server and if, and break will be written in location. Last will not terminate the rewritten url match, and break will terminate the rewritten url match.

5. Case configuration

rewrite rewrite flag

rewrite is mainly used to rewrite URL or jump URL instructions

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf		
		root /hhh/xxx;
    	location / {
    
    
                rewrite /1.html /2.html;
                rewrite /2.html /3.html;
        }

        location /2.html {
    
    
                rewrite /2.html /a.html;
        }

        location /3.html {
    
    
                rewrite /3.html /b.html;
        }
        
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@nginx ~]# systemctl restart nginx

[root@nginx ~]# mkdir -p /hhh/xxx
[root@nginx ~]# echo "web1" > /hhh/xxx/1.html
[root@nginx ~]# echo "web2" > /hhh/xxx/2.html 
[root@nginx ~]# echo "web3" > /hhh/xxx/3.html
[root@nginx ~]# echo "web a" > /hhh/xxx/a.html
[root@nginx ~]# echo "web b" > /hhh/xxx/b.html
[root@nginx ~]# curl 192.168.183.138/1.html
web b

When requested 1.html, will eventually access theweb b

Internally location{}, when a break is encountered, all instructions in this location{}and all subsequent ones will no longer be executed, so jump to access to web2location{}2.html

		location / {
    
                                    
                rewrite /1.html /2.html break;
                rewrite /2.html /3.html;            
        }                                                                                     
        location /2.html {
    
                              
                rewrite /2.html /a.html;            
        }                                      
                                                
        location /3.html {
    
                              
                rewrite /3.html /b.html;            
        }   
        
[root@nginx ~]# curl 192.168.183.138/1.html
web2

Inside location{}, when last is encountered, subsequent instructions in this location{} will not be executed, and the rewritten url will re-initiate a request to the server{…} tag where it is located, and match the rules from beginning to end, and execute whichever matches.

		location / {
    
    
                rewrite /1.html /2.html last;
                rewrite /2.html /3.html;
        }

        location /2.html {
    
    
                rewrite /2.html /a.html;
        }

        location /3.html {
    
    
                rewrite /3.html /b.html;
        }
        
[root@nginx ~]# curl 192.168.183.138/1.html     web a

Two, if application

1. The judgment condition in the if statement

regular expression match

  • ==: equivalent comparison;
  • ~: Returns "true" when it matches the specified regular expression pattern, and distinguishes between uppercase and lowercase characters when judging whether it matches or not;
  • ~*: Returns "true" when it matches the specified regular expression pattern, and does not distinguish between uppercase and lowercase characters when judging whether it matches or not;
  • !~: Returns "true" when it does not match the specified regular expression pattern, and distinguishes between uppercase and lowercase characters when judging whether it matches or not;
  • !~*: Returns "true" when it does not match the specified regular expression pattern, and does not distinguish between uppercase and lowercase characters when judging whether it matches or not

2. File and directory matching judgment

  • -f, !-f: Determine whether the specified path exists and is a file;
  • -d, !-d: Determine whether the specified path exists and is a directory;
  • -e, !-e: Determine whether the specified path exists, either a file or a directory;
  • -x, !-x: Determine whether the file in the specified path exists and is executable;

3. Browser-based separation case

if ($http_user_agent ~ Firefox) {
  rewrite ^(.*)$ /firefox/$1 break;
}

if ($http_user_agent ~ MSIE) {
  rewrite ^(.*)$ /msie/$1 break;
}

if ($http_user_agent ~ Chrome) {
  rewrite ^(.*)$ /chrome/$1 break;
}

4. Anti-leech case

location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.idfsoft.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.idfsoft.com/403.html;
  }
}

Guess you like

Origin blog.csdn.net/qq_65998623/article/details/127346155