Location matching and Rewrite rewriting

1. Common Nginx regular expressions

^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+ :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”
. :匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字[0-9]  \s:空白符  \w:任意单词字符包括下划线[a-zA-Z0-9_]
[list] :匹配list列表中的一个字符,例: go[ola]d, [abc]、 [a-z]、 [a-z0-9]、 [0-9]匹配任意一位数字
[^list] :匹配任意非list列表中的一个字符,例: [^0-9]、 [^A-20-9]、 [^a-z]匹配任意一位非小写字母
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置
| :或运算符

2. Location matching

1.location can be roughly divided into three categories:

Exact match: location = / {}
General match: location / {}
Regular match: location ~ / {}

2. Commonly used matching rules for location: 

= : Perform an exact match of ordinary characters, that is, an exact match.
^~ : Indicates ordinary character matching. Use prefix matching. If the match is successful, no other locations will be matched.
~ : Matches case-sensitively.
~* : Case-insensitive match.
!~ : case-sensitive match negation.
!~* : case-insensitive match negation.

3. location priority

 First exact match =
second prefix match ^~
followed by regular match ~ or ~* according to the order in the file
, then prefix match without any modification (general match)
and finally handover/general match
Priority summary:
1. (location = full path) > (location ^~ path) > (location ~, ~* regular order) > (location part start part) > (location /) 2. The priority is the same, the regular order depends on the upper and lower order, and the upper one takes precedence;
general Matching depends on the length, the longest match first

 4. Location example description

(1) location = / {}
= is an exact match of /, and the host name cannot be followed by any string, such as accessing / and /data, then / matches, and /data does not match. For example,
location = /abc, then only /abc is matched , /abc/ or /abcd does not match. If location /abc, it matches /abc, /abcd/ and also matches /abc/.
 
(2) location / {}
Because all addresses start with /, this rule will match all requests such as accessing / and /data, then / matches, and /data also matches, but if it is followed by a regular expression, it will
match The longest string matches first (longest match)
 
(3) location /documents/ {}
matches any address starting with /documents/, after matching, continue to search for other locations,
only the regular expressions behind other locations When there is no match, this one will be used
 
(4) location /documents/abc {}
matches any address starting with /documents/abc. After the match is matched, continue to search for other locations,
only the regular expressions behind other locations This one
 
(5) location ^~ /images/ {}
matches any address starting with /images/. After the match is matched, stop searching for regular expressions and use this one
 
(6) location ~ * \.(gif|jpg|jpeg)$ {}
matches all requests ending in gif, jpg or jpeg
However, all requests for images under /images/ will be processed by location ^~ /images/, because ^~ has a higher priority, so this rule cannot be reached (7) The longest character in location /images/abc {
 
}
matches /images/abc, the lowest priority, continue to search for other locations, you will find that ^~ and ~ exist
 
(8) location ~ /images/abc {}
matches the beginning of /images/abc, the second priority, only remove location ^~ /images/ will use this one
 
(9) location /images/abc/1.html {}
matches the /images/abc/1.html file, if compared with the regular ~ /images/abc/1.html , the regularity priority is higher
 
Priority summary:
(location =) > (location full path) > (location ^~ path) > (location ~,~* regular order) > (location partial starting path) > (location /)

In actual website use, there are at least three matching rule definitions

first mandatory rule

Directly match the root of the website. Accessing the homepage of the website through the domain name is more frequent. Using this will speed up the processing. For example, the official website here is directly forwarded to the back-end application server, or it can be a static homepage

location ^~ /static/ {
    root /webroot/static/;
}
 
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

The second mandatory rule

Handle static file requests, which is the strength of nginx as an http server. There are two configuration modes, directory matching or suffix matching, choose one or use it together

location ^~ /static/ {
    root /webroot/static/;
}
 
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

third rule

General rules, such as forwarding dynamic requests with .php and .jsp suffixes to the backend application server. Non-static file requests are dynamic requests by default.

location / {
    proxy_pass http://tomcat_server;
}

Three, rewrite

  The rewrite function is to use the global variables provided by nginx or the variables set by yourself, combined with regular expressions and tags to achieve URL rewriting and redirection.
        For example: after changing the domain name, it is necessary to keep the old domain name to be transferred to the new domain name, to jump to the new page when a certain webpage changes, to prevent hotlinking of the website, and so on.

   Note : rewrite can only be placed in server{}, location{}, if{}, and by default it can only work on the string after the domain name except the passed parameters. 

rewrite jump implementation

Nginx: The ngx_http_rewrite_module module supports URL rewriting and if condition judgment, but does not support else

Jump: jump from one location to another location, the cycle can be executed up to 10 times, nginx will return 500 error after exceeding

PCRE support: syntax rule matching for perl-compatible regular expressions

Rewrite the module set instruction: create a new variable and assign it a value

Rewrite execution sequence
1. Execute the rewrite command in the server block
2. Execute location matching
3. Execute the rewrite command in the selected location

rewrite syntax format

Syntax rewrite<regex><replacement><flag>;
regex: indicates the regular matching rule
replacement: indicates the content after the jump
flag: indicates the flag tag supported by rewrite

Flag tag description

last : After the matching of this rule is completed, continue to match the new location URI rule downwards, which is generally used in server and if.
break : This rule is terminated after matching, and will not match any subsequent rules. It is generally used in location.
redirect: Return 302 temporary redirection, and the browser address will display the redirected URL address.
permanent : Return 301 permanent redirection, and the URL address after the redirection will be displayed in the browser address bar.
 

rewrite example

1) Jump based on domain name

Now the company’s old domain name www.test.com has changed business requirements and needs to be replaced by a new domain name www.lisi.com, but the old domain name cannot be abolished and needs to be redirected to the new domain name, and the parameters behind it remain unchanged.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;  #域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log; #日志修改
	location / { 
	    if ($host = 'www.test.com'){    #$host为rewrite全局变量,代表请求主机头字段或主机名
	        rewrite ^/(.*)$ http://www.lisi.com/$1 permanent;   #$1为正则匹配的内容,即域名后边的字符串
	    }
        root   html;
        index  index.html index.htm;
    }
}
 
echo "192.168.50.26 www.lisi.com www.test.com" >> /etc/hosts
systemctl restart nginx

2) Access jump based on client IP

Today, the new version of the company's business is online, requiring all IPs to access any content to display a fixed maintenance page, only the company's IP: 192.168.50.26 access is normal

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;
 
	#设置是否合法的IP标记;设置变量$rewrite,变量值为boole值true
    set $rewrite true;
    #判断是否为合法IP;当客户端IP为192.168.50.26时,将变量值设为false,不进行重写
    if ($remote_addr = "192.168.50.26"){
        set $rewrite false;
    }
	#除了合法IP,其它都是非法IP,进行重写跳转维护页面
	#当变量值为true时,进行重写
    if ($rewrite = true){
        #重写在访问IP后边插入/weihu.html,例如192.168.50.11/weihu.html
        rewrite (.+) /weihu.html;
    }
    location = /weihu.html {
        #网页返回/var/www/html/weihu.html的内容
        root /var/www/html;
    }
	location / {
        root   html;
        index  index.html index.htm;
    }
}
 
mkdir -p /var/www/html/
echo "As server maintenance, please visit later, thank you." > /var/www/html/weihu.html
echo "192.168.50.26 www.test.com" >> /etc/hosts
systemctl restart nginx

3) Based on the old domain name, jump to the new domain name and add a directory

Now I am visiting http://lisi.test.com, now I need to redirect all visits under this domain name to http://www.test.com/lisi

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  lisi.test.com;
	charset utf-8;
	access_log  /var/log/nginx/lisi.test.com.access.log;
	#添加;这里的$1为位置变量,代表/post
	location /post {
        rewrite (.+) http://www.test.com/lisi$1 permanent;
    }
	location / {
        root   html;
        index  index.html index.htm;
    }
}
 
mkdir -p /usr/local/nginx/html/lisi/post
echo "this is 1.html" > /usr/local/nginx/html/lisi/post/1.html
echo "192.168.50.26 lisi.test.com www.test.com" >> /etc/hosts
systemctl restart nginx.service
使用浏览器访问
http://lisi.test.com/post/1.html 跳转到 http://www.test.com/lisi/post/1.html

4) Jump based on parameter matching

Now visit http://www.test.com/100-(100|200)-100 (any number).html Jump to http://www.test.com page

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;
 
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.+) http://www.test.com permanent;
    }
 
    location / {
        root   html;
        index  index.html index.htm;
    }
}
 
echo "192.168.50.26 www.test.com" >> /etc/hosts
systemctl restart nginx
 
浏览器访问
http://www.test.com/100-200-100.html 或 
http://www.test.com/100-100-100.html 跳转到http://www.test.com页面。

5) Jump based on all files ending in php in the directory

Requires access to http://www.test.com/upload/123.php to jump to the home page.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;
	
location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.test.com permanent;
}
 
location / {
    root   html;
    index  index.html index.htm;
}
}
 
echo "192.168.50.26 www.test.com" >> /etc/hosts
systemctl restart nginx
 
浏览器访问
http://www.test.com/upload/abc.php 跳转到http://www.test.com页面。

Guess you like

Origin blog.csdn.net/weixin_42054864/article/details/132215690