Nginx--regular matching--realizing rewrite jump and application examples


One, Nginx regular matching

1. Regular expression

1.1 Overview

  • Complex path matching needs to be represented by regular expressions. Regular expressions use a single string to describe and match a series of strings that meet a certain syntax rule
  • In many text editors, regular expressions are usually used to retrieve and replace text that meets a certain pattern
  • Many programming languages ​​support string manipulation using regular expressions

1.2 Commonly used regular expression symbols

symbol description
^ Match the starting position of the input character
$ Match the end position of the input string
* Match the preceding character zero or more times
+ Match the preceding character one or more times
Match the preceding character zero or one time
. Matches any single character except "\n"
\ Mark the following character as a special character or a literal character or a back reference
\d Match pure numbers
{n} Repeat n times
{n,} Repeat n times or more
{n,m} Repeat n to m times
[] Define the matched character range
[c] Match a single character
[a-z] Match any of az lowercase letters
[a-zA-Z0-9] Match all uppercase and lowercase letters or numbers
() The start and end position of the expression
| Or operator

2.location

2.1 Classification

Location can be roughly divided into the following three categories:

  1. Exact match: location = / {…}
  2. General match: location / {...}
  3. Regular match: location ~ / {…}

2.2 Common matching rules

symbol description
= Perform an exact match of ordinary characters, that is, an exact match
^~ Indicates that ordinary characters are matched, and the prefix is ​​used for matching; if the match is successful, the subsequent location will not be matched
~ Case-sensitive matching
~* Case-insensitive match
!~ Case-sensitive match negation
!~* Case-insensitive match negation

2.3 Priority

  1. First accurate matching =
  2. Second prefix match^~
  3. The second is the regular matching according to the order in the file ~ or ~*
  4. Then there is prefix matching without any modification, that is, general matching
  5. Finally handed over / general matching, the lowest priority

2.4 Example

  1. localtion = / {}
    = is an exact match /, there can be no string after the host name, such as / and /xcf, then / matches, /xcf does not match
  1. location / {}
    Because all addresses start with /, this rule will match all requests, such as accessing / and /data, then / will match, and /data will also match,
    but if it is followed by a regular expression, it will match Longest string first match (longest match)
  1. location /documents/ {}
    matches any address starting with /documents/, after the match is matched, continue to search for other locations
  1. location /documents/abc {}
    matches any address starting with /documents/abc. After the match is matched, continue to search for other locations.
    Only when the regular expression behind other locations does not match, this one will be used
  1. location ^~ /images/ {}
    matches any address starting with /images/, after the match is met, stop searching the regular, and use this
  1. location ~* .(gif|jpg|jpeg)$ {}
    matches all requests ending with gif, jpg, and jpeg.
    However, all images under /images/ will be processed by location ^~ /images/ because of ^~ The priority is higher, so this rule cannot be reached
  1. location /images/abc {} The
    longest character matches /images/abc, the priority is the lowest, continue to search for other locations, you will find that ^~ and ~ exist
  1. location ~ /images/abc {}
    matches those starting with /images/abc, the priority is second, and only if location ^~ /images is removed, this one will be used
  1. location /images/abc/1.html {}
    matches /images/abc/1.html file, if compared with regular ~ /images/abc/1.html, regular priority is higher
  1. Priority summary:
    (location = full path)> (location ^~ path)> (location , * regular order)> (location part starting path)> (location /)

2.5 Matching rule definition

In the use of actual websites, there are at least three matching rule definitions:

The first mandatory rule:
directly match the root of the website. It is more frequent to visit the homepage of the website through the domain name. Using this will speed up the processing. For example, the official website
can be a static homepage or directly forwarded to the back-end application server.

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

The second mandatory rule:
processing 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 a combination

location ^~ /static/ {
    
    
    root /webroot/static/;
}

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

The third required rule: it
is a general rule, for example, it is used to forward dynamic requests with .php and .jsp suffixes to the back-end application server.
Non-static file requests are dynamic requests by default.

location / {
    
    
    proxy_ pass http://tomcat_server;

二、rewrite

1 Overview

  • The rewrite function is: use the global variables provided by nginx or the variables set by yourself, combined with regular expressions and mark bits to achieve URL (full domain name + path we visit) rewriting and redirection
  • For example: after changing the domain name, you need to keep the old domain name to be able to jump to the new city name, a certain webpage needs to jump to a new page, the website anti-theft chain, etc.
  • rewrite can only be placed in server{}, location{}, if{}, and by default can only work on the string after the domain name except for the passed parameters
  • For example: http://www.xcf.com/a/w/index.php?id=1&u=str Only rewrite /a/we/index.php

2. Jump implementation

mark

  • The anti-theft chain we learned before is actually based on the function of rewrite.
  • Nginx uses more and is faster than Apache
  • Nginx: supports URL rewriting and judgment of if conditions through the ngx_http_rewrite_module module, but does not support els
  • Jump: Jump from one location to another. The loop can be executed up to 10 times. After the noise, nginx will return a 500 error
  • PCRE support: Perl compatible regular expression syntax rule matching
  • Rewrite module set instruction: create a new variable and set its value

3. Jump scene

  • Adjust the URL that users browse to look more standardized and meet the needs of developers and product personnel
  • In order to enable 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, the old visitor will be redirected to the new domain name; for example, a visit to 360buy.com of Jingdong will redirect to jd.com
  • Some business adjustments on the server, such as URL adjustments based on special variables, directories, and client information

3. Order of execution

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

4. Grammar and flg mark description

rewrite <regex> <replacement> [flag];
  • regex: Represents regular matching rules
  • replacement: indicates the content after the jump
  • flag: indicates the flag flag supported by rewrite

Flag description:

  1. last: After this rule is matched, continue to match the new location URL rule downwards, generally used in server and if.
  2. break: This rule will be terminated when the matching is completed, and will no longer match any subsequent rules, generally used in location
  3. redirect: return to 302 temporary redirection, the browser address will display the redirected URL address
  4. permanent: return to 301 permanent redirection, the browser address bar will display the redirected URL address

Three, rewrite example

Environmental preparation:

  • Install local YUM source warehouse
  • Compile and install Nginx service from source code
  • VMware, based on CentOS7
  • One source host CentOS7 (192.168.126.15)
  • A Win10 (192.168.126.10) is used for testing
  • If you have any questions above, you can read my previous blog

1. Redirect based on domain name

Demand: Now the company's old domain name www.xcf.com has business requirements change, and the new domain name www.zxc.com needs to be used instead, but the old domain name cannot be abolished, and it needs to be redirected to the new domain name, and the following parameters remain unchanged

mkdir -p /var/log/nginx/

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                        #修改域名       

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
                ##日志保存路径修改

        location / {
    
    
        if ($host = 'www.xcf.com') {
    
    
        #$host为rewrite全局变量,代表请求主机头字段或主机名
                rewrite ^/(.*)$ http://www.zxc.com/$1 permanent;
                                #$1为正则匹配得内容,即域名后边得字符串
        }
            root   html;
            index  index.html index.htm;
        }


echo "192.168.126.15 www.xcf.com" >> /etc/hosts
echo "192.168.126.15 www.zxc.com" >> /etc/hosts

systemctl restart nginx.service 

mark

Open the browser at this time, when you visit http://www.xcf.com, it will automatically jump to zxc
mark

2. Access redirect based on client IP

Requirements: The new version of the company’s business is online, requiring all IPs to access any content to be displayed as a fixed maintenance page, only the company’s IP 192.168.126.15 is accessible

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
        ##设置是否是合法的IP标记
        set $rewrite true;
        #设置变量$rewrite,变量值为boole值true

        #判断是否为合法IP
        if ($remote_addr = "192.168.126.15") {
    
    
                set $rewrite false;
                #当客户端IP为192.168.126.15时,将变量值设为flase,不进行重写
        }

        ##除了合法IP,其它都是非法IP,进行重写跳转到维护页面
        if ($rewrite = true) {
    
    
        #当变量值为true时,进行重写
                rewrite (.+) /weihu.html;
                #重写在访问IP后边插入/weihu.html,例如192.168.126.65/weihu.html
        }
        location = /weihu.html {
    
    
            root /var/www/html;
        #页面返回/var/www/html/weihu.html的内容
        }

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


mkdir -p /var/www/html

echo '<h1>weihu!!</h1>' > /var/www/html/weihu.html

systemctl restart nginx.service 

mark

Enter win10, modify the configuration file to map the source host DNS domain name resolution
mark

Try access
mark
only the source host with IP 192.168.126.15 can access the website normally

3. Based on the old domain name jump to the new domain name and add the directory

Requirement: To change the domain name of the website (http://bbs.xcf.com), all visits under the old domain name need to be redirected to the new website (http:www.xcf.com/bbs)

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  bbs.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
        #添加
        location /post {
    
    
                rewrite (.+) http://www.xcf.com/bbs$1 permanent;
                #这里$1为位置变量,代表/post
        }
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }



mkdir -p /usr/local/nginx/html/bbs/post

echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.126.15 bbs.xcf.com" >> /etc/hosts

systemctl restart nginx.service 

mark

At this time, when you visit http://bbs.xcf.com/post/1.html with a browser, it will automatically jump to http://www.xcf.com/bbs/post/1.html
mark

4. Jump based on parameter matching

Requirements: Visit http://www.xcf.com/100-(100|200)-100.html and you will be redirected to the page of http://www.xcf.com

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;

        if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$) {
    
    
        #设置正则匹配
                rewrite (.*) http://www.xcf.com permanent;
                #设置重写
        }
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }



systemctl restart nginx.service 

Use a browser to visit http://www.xcf.com/100-100-100.html or http://www.xcf.com/100-200-100.html, it will automatically jump to http://www. xcf.com page
mark

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

Requirements: request to visit http://www.xcf.com/upload/123.php to jump to the homepage

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;

        location ~* /upload/.*\.php$ {
    
    
           rewrite (.+) http://www.xcf.com permanent;
        }

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


systemctl restart nginx.service

Browser visit http://www.xcf.com/upload/123.php to jump to the homepage of http://www.xcf.commark

6. Jump based on the most common URL request

Request to visit a specific page, such as: http://www.xcf.com/abc/123.html, can jump to the homepage

vim /usr/local/nginx/conf/nginx.conf

    server {
    
    
        listen       80;
        server_name  www.xcf.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;

        location ~* /abc/123.html {
    
    
           rewrite (.+) http://www.xcf.com permanent;
        }

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


systemctl restart nginx.service 

Browser visit http://www.xcf.com/abc/123.html to jump to http://www.gcc.com
mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/112644626