Nginx: Rewrite jump + regular expression + 6 production cases! The content is too real

Why jump

Please be patient to finish the theory

Now Nginx has become the first choice of many companies as a front-end reverse proxy server. In actual work, it often
encounters many requests for redirection (URL rewriting). For example, after changing the domain name, you need to keep the old domain name to be able to jump to the new domain name
, a certain webpage needs to jump to a new page after a change, the website anti-theft chain, etc. If you use the Apache
server in the backend , although it can also do jumps, the rule base is also very powerful, but it will be more efficient to use Nginx to jump.

Rewrite jump scene

●The URL that users browse can be adjusted, which looks more standardized and meets the needs of development and product personnel.

●In order to allow search engines to search for website content and better user experience, companies 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. For example, visiting 360buy.com on JD will redirect to jd.com.

●Adjust URLs based on special variables, directories, and client information.

Rewrite jump implementation

You have to be patient, it's all dry goods

● Nginx supports url rewriting and if condition judgment through the ngx http rewrite module module, but does not
support else. In addition, this module requires PCRE support. You should specify PCRE support when compiling Nginx, and it is installed by default.

● According to the redirection of related variables and the selection of different configurations, jump from one location to another location, but such a loop can be executed up to 10 times, after which Nginx will return a 500 error.

●At the same time, the rewrite module contains the set instruction to create a new variable and set its value, which is very useful in some scenarios, such as recording condition identification, passing parameters to other locations, recording what has been done, and so on.

● The rewrite function is to use global variables provided by Nginx or variables set by yourself, combined with regular expressions and flags to achieve URL rewriting and redirection.

Rewrite actual scene

I know you are slumping fast, so take a look anyway!

● In actual work scenarios, there are three ways to achieve Nginx jump requirements. You can directly use rewrite to perform matching jumps, or you can use if to match global variables and then jump

●In addition, you can also use location to match and then jump, so rewrite can only be placed in the server{}.ifo, location{} section. For example, location can only work on
the string after the domain name except the passed parameters , such as http ://www.domain.com/index.php?id=1 only rewrites /index.php.

●If you want to work on domain names or parameter strings, you can use if global variable matching, or proxy pass reverse proxy.

Rewrite syntax

You must read this, otherwise, what if you don’t understand the case!

The syntax of the Rewrite command is as follows

rewrite [flag];
regex: regular
replacement: the content after the jump
flag: flag flag supported by rewrite

Flag description:

last: It is equivalent to the [L] mark of Apache, which means the rewrite is completed.
break: This rule will be terminated when the matching is completed, and will not match any subsequent rules.
redirect: Return 302 temporary redirect, the browser address will display the URL address after the redirect, and the crawler will not update the URL (because it is temporary).
permanent: Return 301 permanent redirection, the browser address bar will display the URL address after the jump, and the crawler updates the url.

●If the flag is not followed, the default is 302 temporary redirection. In actual work scenarios, there is another return specification. Because 301 and 302 cannot simply return the status code, but also must have a redirect URL, which is why the return command cannot return 301, 302.

The difference between last and break is a bit difficult to understand. Last is generally written in server and if, while break is generally used in location. Last does not terminate the rewritten URL matching, that is, the new URL will go through the matching process from the server again, and break Match after termination of rewriting

Nginx regular expression

●Before learning Rewrite, you must be familiar with regular expressions, and give some commonly used regular expression
metacharacters. It is necessary to deeply understand and learn the meaning of each metacharacter, and do more hands-on experiments, in the actual production environment to combine multiple metacharacters to use

●Commonly used regular expression metacharacters

character description
^ Match the starting position of the input string
$ Match the end position of the input string
* Matches the preceding character zero or more times. Such as "ol*" can match". "o" and "ol", "oll"
+ Match the preceding character one or more times. For example, "ol+" can match "ol" and "oll", "oll", but cannot match "o"
? Matches the preceding character zero or one time, such as "do(es)?" can match "do" or "does", "?" is equivalent to "{0,1}"
. Match any single character except "\n". To match any character including "\n", please use a pattern such as "[.\n]".
\ Mark the following character as a special character or a literal character or a back reference. For example, "\n" matches a newline character, and "$" matches "$"
\d Match pure numbers
{n} Repeat n times
{n,} Repeat n times or more
[c] Match a single character c [az] Match any one of az lowercase letters
[a-z,A-Z] Match any of az lowercase letters or AZ uppercase letters

Location category

Location can be roughly divided into three categories:
location = patt () [exact match
location patt () [General match]
location ~ patt ()[Regular match]

●Precise matching and general matching do not need to be explained in detail, mainly regular matching. The following are some expressions of regular matching, which need to be kept in mind.

~ Means to perform a regular match, case sensitive
~* Means to perform a regular match, not case sensitive
!~ Indicates that a regular match is performed, which is case-sensitive and does not match
!~* Means to perform a regular match, insensitive to case mismatch
^~ Indicates that ordinary characters match. Use prefix matching. If the match is successful, no other location will be matched
= Perform an exact match of ordinary characters. That is, an exact match
@ It defines a named location, used when targeting internally, such as error_page

Location priority

●The location of Nginx is not related to the order of location in the configuration. It is related to the type of location expression. For expressions of the same type, longer strings will be matched first.

●The following is the order of priority:

1. The equal sign type (=) has the highest priority. Once the match is successful, no other matches will be found.
2.^~Type expression. Once the match is successful, no more matches are found
3. Regular expression type ( and *) has the second priority.
4. Regular string matching type. Match by prefix.
5. General match (/), if there is no other match, any request will be matched.

From the functional point of view, rewrite and location seem to be a bit similar, and they can both achieve jumps. The main difference is that rewrite is to change the path to obtain resources within the same domain name, while location is to control access or reverse proxy for a type of path, and it can also proxy_pass. To other machines. In many cases, rewrite will also be written in location, and their execution order is:

1. Execute the rewrite command in the server block
2. Perform location matching
3. Execute the rewrite command in the selected location

Examples of different location priorities

location= /{#Exact match/, no character string can be attached to the host name. [configuration A]}
location /{#因为所有的地址都以/开头,所以这条规则将匹配到所有请求,但是正和最长字符串会优先匹配。[ configuration B]}
location /documents/{#匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索,只有后面的正则表达式没有匹配到时,这一条才会起作用。[ configuration C]}
location /documents/abc {#匹配任何以/documents/abc开头的地址,匹配符合以后,还要继续往下搜索,只有后面的正则表达式没有匹配到时,这一条才会起作用。[ configuration D]}
location ^~ /images/ {#匹配任何以/images/开头的地址,匹配符合以后,停止往匹配。[configuration E]}
location ~* .(gifIpgIjpeg)$ {#匹配所有以gif, jpg 或 jpeg 结尾的请求, 然而, 所有请求/images/下的图片会被[ configuration E] 处理, 因为^~的优先级更高。[ configuration F]}
location /images/abc {#最长字符匹配到/images/abc,优先级最低[configuration G ]}
location ~/images/abc {#匹配以/images/abc开头的,优先级次之。[configuration H]}
location /images/abc/1.html {#匹配以/images/abc/1.html文件,如果和正则~/images/abc/1.htmL相比,正则优先级更高[ configuration I ]}

location 优先级总结:

1.如果是匹配某个具体文件:
(location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) > (location ~完整路径) > (location完整路径) > (location /)
2.如果是用目录做匹配访问某个文件:
(location = 目录) > (location ^~目录/ > (location ~ 目录) > (location~*目录) > (location 目录) > (location /)

Rewrite6个生产案列

少年,你真的步考虑回过头去看看理论吗?给个面子看看吧

首先搭建环境:

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
  server {
    
    
      listen       80;
      server_name    www.51xit.top;                   ####更改域名

      #charset koi8-r;

      access_log /var/log/nginx/www.51xit.top.access.log ;       ###改访问日志地址

      location / {
    
     
          root html;
          index index.html index.htm;
      }

[root@localhost ~]# mkdir -p /var/log/nginx/
[root@localhost ~]# touch /var/log/nginx/www.51xit.top.access.log

[root@localhost ~]# systemctl restart nginx

去电脑的的C:\Windows\System32\drivers\etc\hosts 添加两个:

20.0.0.26 www.51xit.top
20.0.0.25 www.52xit.top

打开网页www.51xit.top显示正常
在这里插入图片描述
打开网页www.52xit.top显示正常,这里52xit我用的是Apache
在这里插入图片描述

生产案列1

基于域名的跳转,现在公司旧域名www.51xit.top有业务需求有变更,需要使用新
域名www.52xit.top代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参
数保持不变。

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        #access_log  /usr/local/nginx/logs/www.51xit.top.access.log;

        location / {
    
    
            #root   html;
            #index  index.html index.htm;
                if ($host = 'www.51xit.top') {
    
    
                rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;
                }
        }
    .....     省部分
    }

[root@localhost~# systemctl restart nginx

打开网页测试一下,需要清除历史缓存:访问www.51xit.top

在这里插入图片描述

生产案列2

基于客户端IP访问跳转,例如今天公司业务版本上线, 所有IP访问任何内容都显
示一个固定维护页面,只有公司IP访问正常。

为了方便验证,我开了三台系统:

Linux两台:
20.0.0.26 配置网站用 网页地址www.51xit.top
20.0.0.140 做客户机使用
一台Windows:
20.0.0.1 做客户机使用
##第一步:修改配置文件
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

#####需要添加的配置文件如下####

    server {
    
    
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        #access_log  /usr/local/nginx/logs/www.51xit.top.access.log;
        set $rewrite true;
        if ($remote_addr = "20.0.0.1"){
    
    
        set $rewrite false;
        }

        if ($rewrite = true){
    
    
        rewrite (.+) /wh.html;
        }

        location = /wh.html {
    
    
        root /usr/local/nginx/html/;
        }

        location / {
    
    
            root   html;
            index  index.html index.htm;
        }
    .....    省略部分
    }

######命令解析#############

#设置是否合法的IP标识
set $rewrite true; ##·新增·

###判断是否合法的ip###允许20.0.0.1IP正常访问
if ($remote_addr =“20.0.0.1”)
set Srewrite false;

##其余跳转到wh.html页面
rewrite (.+) /wh.html;

##wh.html存放的路径
root /usr/local/nginx/html/:

#第二步:建一个wh网站以便访问

[root@localhost ~]#  echo "网页维护中,请联系管理员">/usr/local/nginx/html/wh.html

[root@localhost html]# systemctl restart nginx
#第三步,验证

#首先用20.0.0.25访问 www.51xit.top
#再用20.0.0.1访问 www.51xit.top
##下面为140的操作
zzt@localhost ~]$ su - root
Password: 
Last login: Tue Sep  8 18:27:19 CST 2020 from 20.0.0.1 on pts/2

[root@localhost ~]# vi /etc/hosts
20.0.0.26 www.51xit.top

网页跳转到维护

再用20.0.0.1主机访问

在这里插入图片描述

生产案列3

基于旧域名跳转到新域名后面加上目录的跳转,例如现在访问的是http://ww.51 xit.top/post/
现在需要将这个域名下面的访问都跳转到http://www.52xit.top/bbs
而且注意保持域名跳转后的参数不变

#第一步,修改配置文件
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
    
    server {
    
    
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        #access_log  /usr/local/nginx/logs/www.51xit.top.access.log;

        location /post {
    
    
        rewrite (.+) http://www.51xit.top/bbs$1 permanent;
        }

##第二步   验证

#清除浏览器缓存
#浏览器输入www.51xit.top/post

在这里插入图片描述

生产案列4

基于参数匹配的跳转,例如现在访问:
http://www.51xit.top/100-(100I200)-100.html 跳转到http://www.51xit.top页面
咳咳,插一行,作者想点奶茶了,稍等一下
#第一步,修改配置文件
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        #access_log  /usr/local/nginx/logs/www.51xit.top.access.log;
        if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
    
    
        rewrite (.*) http://www.51xit.top permanent;
        }

##添加从if开始
你看看,看不懂了吧,所以回头看看理论呀
这条是通过访问的url判断的
#第二步,验证  需要访问以下4个:

#浏览器输入http://www.51xit.top/100-100-100.html 
#浏览器输入http://www.51xit.top/100-200-100.html
#浏览器输入http://www.51xit.top/100-200-10078789.html
#浏览器输入http://www.51xit.top/100-200-10078789dddd.html

#下面图片按顺序排放

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结:前三个都能跳转,最后一个因为不是纯数字结尾的所以不能跳转

生产案列5

基于目录下所有php结尾的文件跳转,访问http://www.51xit.top/upload/1.php要跳转到首页

#老规矩

[root@localhost htmlj# vi /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        #access_log  /usr/local/nginx/logs/www.51xit.top.access.log;

        location ~* /upload/.*\.php$ {
    
    
        rewrite (.+) http://www.51xit.top permanent;
        }
#第二步验证

1、浏览器输入http://www.51xit.top/upload/1.php   
2、浏览器输入http://www.51xit.top/upload/bbs/1.php
3、浏览器输入http://www.51xit.top/upload/index.html

#以下图片按顺序排放

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

生产案列6

基于最普通一条url请求的跳转,访问一个具体的页面跳转到首页

[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf

   server {
    
    
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        #access_log  /usr/local/nginx/logs/www.51xit.top.access.log;

        location ~* ^/1/text.html {
    
    
        rewrite (.+) http://www.51xit.top permanent;
        }

###验证

1、浏览器输入http://www.51xit.top/1/test.html
2、浏览器输入http://www.51xit.top/2/test.html
3、浏览器输入http://www.51xit.top/1/test     

#以下图片按顺序排放

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

看看开头的理论吧

Guess you like

Origin blog.csdn.net/weixin_48190891/article/details/108532802