nginx-rewrite-5

What is rewrite

Rewrite, also known as URL Rewrite, is URL rewriting, which is the process of redirecting incoming Web requests to other URLs.

1. The most common application of URL Rewrite is URL pseudo-static, which is a technology that displays dynamic pages as static pages. For example: http://www.123.com/news/index.php?id=123 can be displayed as http://www.123.com/news/123.html after conversion using URL Rewrite For websites that pursue perfectionism Designers, even the address of a web page want to look as concise and clear as possible. In theory, search engines prefer web pages in the form of static pages, and search engines generally score static pages higher than dynamic pages. Therefore, URLRewrite can make our web pages more easily included by search engines.
2. From a security point of view, if too many parameters are exposed in the URL, May Day will cause a certain amount of information leakage, which may be used by some hackers and cause certain damage to your system, so the static URL address can be used for We bring higher security.
3. Realize website address jumping, for example, when a user visits 360buy.com, it will be redirected to jd.com. For example, when a customer visits port 80 of tianyun.com, it will be redirected to port 443.
rewrite command:

Nginx Rewrite related instructions include if, rewrite, set, return

if statement

Application environment: server, location

Syntax: if (condition) {…}

if can support the following conditions to judge matching symbols
~ Regular match (case sensitive)
~* Regular match (case insensitive)
!~ Regex does not match (case sensitive)
!~* Regex does not match (case insensitive)
-f and !-f are used to determine whether to save the file
-d and !-d are used to judge whether there are directories
-e and !-e are used to determine whether to save files or directories
-x and !-e are used to determine whether the file can be executed

Some Nginx global variables can be referenced during the matching process

$args parameters in the request;

$document_root sets the value for the root path of the current request;

$host “Host” in the request information, if there is no Host line in the request, it is equal to the set server name;

$limit_rate limits the connection rate;

$request_method request method, such as “GET”, “post”, etc.;

$remote_addr client address;

$remote_port client port number;

$remote_user client user name, for authentication;

$request_filename The file pathname of the current request (with the home directory of the website)

$request_uri The file pathname of the current request (without the website’s home directory)

$query_string is the same as $args;

The protocol used by $scheme, such as http or https

$server_protocol request protocol version, “HTTP/1.0” or “HTTP/1.1”;

$server_addr server address, if the server address is not specified with listen, using this variable will initiate a system call to obtain the address (resulting in waste of resources);

$server_name The name of the server where the request arrives;

$document_uri is the same as $uri, URI address;

$server_port The port number of the server where the request arrives;

Example:
if (-d $request_filename) { #The file path name of the current request (with the main directory of the website)
...;
}
 
Whether the address of the matching visit starts with www
if ($host ~* ^www) {
...;
}

Rewrite flag

The Rewrite directive redirects URLs based on expressions, or modifies strings. It can be applied to server, location, and each line of Rewrite instructions in the if environment is followed by a flag at the end. The supported flags are:

last: It is equivalent to the [L] mark in Apache, indicating that the rewrite is completed. The default is last.

break: After this rule is matched, the match is terminated, and the following rules are not matched.

redirect: Return 302 temporary redirection, and the browser address will display the redirected URL.

permanent: Return 301 permanent redirection, and the browser address will display the redirected URL address.

Rewrite matching reference example:

Example 1: http://www.zcg666.com/a/1.html ==> http://www.zcg666.com/b/2.html

[root@localhost ~]# mkdir -p /html/{a,b}
[root@localhost ~]# echo "1.html" > /html/a/1.html
[root@localhost ~]# echo "2.html" > /html/b/2.html
[root@localhost ~]# tree /html/
/html/
├── a
│ └── 1.html
└── b
    └── 2.html
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location /a {
                root /html;
                index 1.html;
        rewrite .* /b/2.html permanent;
        }
        location /b {
                root /html;
                index 2.html;
        }
}
 

Browser access test (rewrite configuration is successful)

Example 2: http://www.zcg666.com/2019/a/1.html ==> http://www.zcg666.com/2018/a/1.html

[root@localhost ~]# tree /html/
/html/
├── 2018
│ ├── a
│ │ └── 1.html
│ └── b
│ └── 2.html
└── 2019
    └── a
        └── 1.htm
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location /2019/a {
                root /html;
                index 1.html;
        rewrite ^/2019/(.*)$ /2018/$1 permanent;
        }
        location /2018/a {
                root /html;
                index 1.html;
        }
}
Reload the nginx configuration file! ! ! 

Browser access test (rewrite configuration is successful)

Example 3: http://www.zcg666.com/a/1.html==>http://jd.com

[root@localhost ~]# tree /html/
/html/
└── a
    └── 1.html
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location /a {
        root /html;
        rewrite .* http://jd.com permanent;
        }
}
Reload the nginx configuration file

Browser Access Test

Example 4: http://www.zcg666.com/a/1.html==>http://www.hjf777.com/a/1.html

[root@localhost ~]# tree /html/
/html/
└── a
    └── 1.html
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location /a {
        root /html;
        if ($host ~* 666.com) {
        rewrite .* http://www.hjf777.com$request_uri permanent;
        }
        }
}
Reload the nginx configuration file

Browser Access Test

Example 5: Add / after accessing the directory (if there is a / after the directory, then do not add /)

# http://www.zcg666.com/a/b/c
# $1: /a/b
# $2: c
# http://$host$1$2/

[root@localhost ~]# tree /html/
/html/
└── a
    └── b
        └── c
            └── 1.html
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location /a/b/c {
                root /html;
                index 1.html;
                if (-d $request_filename) {
                rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
                }
        }
}
Restart the nginx configuration file

Browser Access Test

Example 6: http://www.zcg666.com/login/login.html==>http://www.zcg666.com/reg/login.html?user=login

[root@localhost ~]# tree /html/
/html/
├── login
│ └── login.html
└── reg
    └── login.html
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location /login {
                root /html;
                rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1;
        }
        location /reg {
                root /html;
                index login.html;
        }
}
Reload the nginx configuration file

Browser Access Test

Example 7: http://www.zcg666.com/11-22-33/1.html==>http://www.zcg666.com/11/22/33/1.html

[root@localhost ~]# tree /html/
/html/
└── 11
    └── 22
        └── 33
            └── 1.html
[root@localhost ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
        listen 80;
        server_name www.zcg666.com;
        location / {
        rewrite ^/([0-9] + )-([0-9] + )-([0-9] + )(.*)$ http://$host/$1/$2/$3$4 permanent;
 
        }
        location /11/22/33 {
                root /html;
                index 1.html;
        }
}
Reload the nginx configuration file

Browser Access Test

set

The set command is used to define a variable and assign a value, application environment: server, location, if

Example:
#http://alice.testpm.com ==> http://www.testpm.com/alice
#http://jack.testpm.com ==> http://www.testpm.com/jack
 
[root@nginx-server conf.d]# cd /usr/share/nginx/html/
[root@nginx-server html]# mkdir jack alice
[root@nginx-server html]# echo "jack.." >> jack/index.html
[root@nginx-server html]# echo "alice.." >> alice/index.html
Locally resolve the domain name host file
10.0.105.202 www.testpm.com
10.0.105.202 alice.testpm.com
10.0.105.202 jack.testpm.com
 
Edit configuration file:
server {
    listen 80;
    server_name www.testpm.com;
 
    location / {
         root /usr/share/nginx/html;
         index index.html index.htm;
         if ( $host ~* www.testpm.com) {
                break;
                }
         if ( $host ~* "^(.*)\.testpm\.com$" ) {
                set $user $1;
                rewrite .* http://www.testpm.com/$user permanent;
                }
        }
    location /jack {
         root /usr/share/nginx/html;
         index index.html index.hml;
        }
    location /alice {
         root /usr/share/nginx/html;
         index index.html index.hml;
        }
}

return

The return command is used to return the status code to the client, application domain: server, location, if

Example:
1. If the file ending in .sh is accessed, a 403 operation rejection error will be returned
server {
    listen 80;
    server_name www.testpm.cn;
    #access_log /var/log/nginx/http_access.log main;
 
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        }
 
    location ~* \.sh$ {
        return 403;
        }
}

last, break

[root@localhost test]# cat /etc/nginx/conf.d/last_break.conf
server {
    listen 80;
    server_name localhost;
    access_log /var/log/nginx/last.access.log main;
 
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    location /break/ {
        root /usr/share/nginx/html;
        rewrite.* /test/break.html break;
    }
    location /last/ {
        root /usr/share/nginx/html;
        rewrite.* /test/last.html last;
    }
    location /test/ {
        root /usr/share/nginx/html;
        rewrite.* /test/test.html break;
    }
 
}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html
 
Notice:
? The last tag will re-initiate the request to the server { … } tag where this rewrite rule is executed;
 
? The break tag means that after the matching of this rule is completed, the matching will be stopped and no subsequent matching will be done;
 
? When using the alias command, you must use last;
 
? When using the proxy_pass command, you must use break.