Nginx--Rewrite rewrite

Table of contents

One, rewhite jump scene

Second, the actual scene of nginx

Three, nginx regular expression

1. Grammar format

Fourth, location

1, location can be divided into three categories

2,location

3, location matching priority

Five, location example description

1. What is the location priority?

Sixth, in actual network use, at least three matching rules are defined

Seven, experiment

1. Jump based on domain name

2. Jump based on client IP address

3. Jump to the new domain name and add a directory based on the old domain name

Fourth, based on the file jumping at the end of php in the directory

Fifth, jump based on the most common url request


One, rewhite jump scene

1. The URL looks more standardized and reasonable

2. Enterprises will disguise dynamic URL addresses as static addresses to provide services

3. After the website is changed to a new domain name, let the old visit jump to the new domain name

4. Some business adjustments on the server side

Second, the actual scene of nginx

Implementation of Nginx Jump Requirements

  • Use rewhite for matching jumps
  • Use if to jump after matching global variables
  • Use location to match and then jump

rewhite is placed in the server{}, if{}, location{} sections

  • location only works on the characters after the domain name except the passed parameters

For domain name or parameter string

  • Use if global variable to match
  • Reverse proxy using proxy_pass

Three, nginx regular expression

^ : match the starting position of the input string
$ : match the ending position of the input string
* : match the preceding character zero or more times. For example, "ol*" can match "o" and "ol", "oll"
+: Match the preceding character one or more times. For example, "ol+" can match "ol" and "oll", "olll", but cannot match "o"
?: Match the preceding character zero or one time, such as "do(es)?" can match "do" or " does", "?" is equivalent to "{0,1}"
. : matches any single character except "\n", to match any character including "\n", use something like "[ .\n]" like pattern
\ : marks the following character as a special character or a literal character or a backreference. For example, "\n" matches a newline, and "\$" matches "$"
\d: matches pure numbers
\w: matches letters or numbers or underscores or Chinese characters
\s: matches any whitespace character
\b: matches words start or end of
{n}: repeat n times
{n,}: repeat n or more times
{n,m}: repeat n to m times
[]: define the range of characters to match
[c]: match a single character c
[az] : match any one of az lowercase letters
[a-zA-Z0-9] : match all uppercase and lowercase letters or numbers
() : the start and end position of the expression such as (jpg|gif|swf)
| : OR operation symbol

1. Grammar format

rewrite  <regex> <replacement> [flag];

last is equivalent to the L mark of apache, indicating that the rewrite is completed

break local rule matching, termination when complete, not matching any subsequent rules

redirect returns 302 temporary redirection, the browser address will display the redirected URL address, and the crawler will not update the 3URL

permanent returns 301 permanent redirection, and the browser address bar will display the redirected

Fourth, location

1, location can be divided into three categories

精确匹配;location = / {......}

一般匹配:location /{...}

正则匹配: location ~ / {...}

2,location

=   :进行普通字符精确匹配,也就是完全匹配。
^~  :表示普通字符匹配,使用前缀匹配,如果匹配成功,则不再匹配其它location
~   :区分大小写的匹配
~*  :不区分大小写的匹配
!~  :区分大小写的匹配取非
!~* :不区分大小写的匹配取非

3, location matching priority

相同类型的表达式,字符串长的会优先匹配
按优先级排列

首先精确匹配  =
其次前缀匹配  ^~
其次按文件中顺序的正则匹配 ~或~*
然后匹配不带任何修饰符的前缀匹配
最后是交给 / 通用匹配

Five, location example description

① location = / {}

= is an exact match of /, the host name cannot be followed by any string, such as access to / and /data, then / matches, /data does not match. Another example is location = /abc, which only matches /abc, but does not match /abc/ or /abcd. If location /abc , it will match /abc , /abcd/ , /abc/.

② location / {}

Because all addresses start with /, this rule will match all common requests, such as accessing / and /data, then / matches and /data also matches. But if there is a regular expression and the longest character after it, it will be matched first (the longest match)

③location /documents/ {}

Match any address starting with /documents/. After the match, you will continue to search for other locations. Only when the regular expressions behind other locations are not matched, this one will be used.

④location /documents/abc {}

Match any address starting with /documents/abc. After the match, continue to search for other locations. Only when the regular expressions behind other locations are not matched, this one will be used.

⑤ location ^~ /images/ {}

Match any address starting with /images/, after the match, stop searching for the regular, and use this one directly.

⑥ location ~ .(gif|jpg|jpeng)$ {}*

Matches all requests ending with gif, jpg or jpeng. However, all requests under /images/ will be processed by location ^~ /images/, because ^~ has a higher priority, so this rule cannot be reached.

⑦ location /images/abc {}

The longest character matches /images/abc, and the priority is the lowest. If you continue to search for other locations, you will find that ^~ and ~ exist

⑧ location ~ /images/abc {}

Matches starting with /images/abc, the priority is second, only if location ^~ /images/ is removed, this entry will be used

⑨ location /images/abc/1.html {}

Match /images/abc/1.html file, if compared with the regular location ~ /images/abc/1.html, the regular priority is higher

1. What is the location priority?

匹配某个具体文件
(location =完整路径) > (location ^~完整路径) > (location ~*完整路径) > (location ~完整路径) >
(location完整路径) > (location /)
location ~★完整路径> location ~完整路径/path
正则:原则精确度自定义一》为了尽量的节省资源
Path
path
PATH
更容易被定位到

用目录做匹配访问某个目录
(location =目录) > (location ^~目录/) > (location ~目录) > (location ~★目录) > (location 目录) >
(location /)
正则:原则精确度自定义--》为了尽量的节省资源
Path
install.html引导文件
path
install/bbs.html
PATH
install/ index . html
www . kgc. com/ Path/


文件目录为什么只会在区不区分大小写上会有变动
正则表达式:日的是为了尽量精确的匹配
文件一》尽量精确匹配,区分大小写精确、不区分更为精确
日录一-》 尽量精确匹配,区分大小写精确优先级更高,不区分大小写的
Path
install.html引导文件
path
install/bbs.html
PATH
install/ index . html
www . kgc. com/ Path/

Sixth, in actual network use, at least three matching rules are defined

#第一个必选规则
直接匹配网站根,通过域名访问网站首页比较频繁(www.baidu.com/),使用这个会加速处理,比如说官网。
可以是一“个静态首页,也可以直接转发给后端“应用服务器”一》PHP 、Apache
location / {
root    html;
index index.html index. htm;

}



#第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项(1、静态请求处理的能力+高并发处理能力+资源消耗较低)
有两种配置模式,日录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root / webroot/static/ ;

}

location ~* \. (html IIgif I1jpg ljpeglpng1cssljslico)$ {
root /webroot/res/ ;
}





#*★.第三个规则就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
非静态文件请求就默认是动态请求(跳转/反向代理)
upstream     tomcat_ server.{
server 192. 168.226.128:8080 weight 1;
server 192.168.226.132:8080 weight 1;

}

location   ^/ \. (phpljsp)$ {
proxy_ pass http: I I tomgat_ server:

}

根“/"匹配一》匹配首页
静态资源匹配
动态跳转的匹配,是第三类必要匹配规则

Seven, experiment

1. Jump based on domain name

Now the company's old domain name www.cxk.com has business needs to change, it needs to be replaced by the usable domain name www.kya.com , but the old domain name cannot be abolished, it needs to jump to the new domain name, and the following parameters remain unchanged (path

 Add mapping

 Create log directory

Create log directory

[root@kya conf]# cd /var/log/nginx/ [root@kya nginx]# ll total usage 4 -rw-r--r--. 1 root root 3780 Aug 14 16:03 test.com- accss.log-20220813

location /{ if ($host = ' www.cxk.com ') { rewrite ^/(.*)$ http://www.kya.com/ $1 permanent; Make a jump and assign the previous matching result to a $1)

}

Create a test.html inside html

-rw-r--r--. 1 root root 15 8月 16 01:00 test.html [root@kya html]# cat test.html this is a test [root@kya html]#

 

 

 

2. Jump based on client IP address

Today, the new version of the company's business is online, requiring all external visits and ip access to display a fixed maintenance page, only the company's internal ip192.168.135.112 access is normal.

vim /usr/local/nginx/conf/

 set $rewrite true;
       if ($remote_addr = "192.168.135.112") {
        set $rewrite false;
}
       if ($rewrite = true) {
       rewrite (.+) /weihu.html;
}
       location = /weihu.html {
       root /var/www/html;
}

 test

Accessing with other ip will point to weihu.html

 Use a passable ip, you can access it normally

 Enter the log file and view the log

 

3. Jump to the new domain name and add a directory based on the old domain name

When visiting http://bbs.kgc.com/msds/1.html , it will automatically jump to Loading...

Create the specified directory

 Add mapping

 

 verify

 

Fourth, based on the file jumping at the end of php in the directory

Request to visit Loading... Jump to the home page www.kya.com (Scenario: Register, Login

 

 Jump directly to www.kya.com

 

Fifth, jump based on the most common url request

 

 

Jump directly to www.kya.com

 

Guess you like

Origin blog.csdn.net/m0_54594153/article/details/126630093