rewrite of nginx

Rewrite of Nginx

1. What is rewrite

Rewrite mainly implements url address rewriting and redirection, which is the process of redirecting incoming web requests to other URLs

2.rewrite usage scenario

  1. Address jump, users can visit www.linux.com Com this URL is to direct it to a new domain name www.baidu.com com
  2. Protocol jump: when a user requests a website through http protocol, it will jump to https protocol again
  3. Pseudo static, a technology that displays dynamic pages as static pages, which is convenient for search engine input. At the same time, a dynamic URL address is built to expose too many parameters to improve higher security.
  4. For search engines, SEO optimization depends on the url path, and easy to remember URLs are easy for search engines to enter

3.rewrite syntax

Syntax: rewrite regex replacement [flag];
Default:    —
Context:    server, location, if
 
rewrite         #Module command
regex           #Requested link (regular expressions supported)
replacement     #Jump link
[flag];         #label
 
location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;
    return  403;
}

4.rewrite Flag

parameter

The rewrite instruction redirects the URL or modifies the string according to the expression. It can be applied to the server, location and if environment. Each row of the rewrite instruction is followed by a flag tag. The supported flag tags are as follows:

flag effect
last After this rule is matched, stop matching and no longer match the following rules
break After this rule is matched, stop matching and no longer match the following rules
redirect Return to 302 temporary redirection, and the address bar will display the address after the jump
permanent Return to 301 permanent redirection, and the address bar will display the address after jump

The difference between last and break

In lb01

[root@lb01 conf.d]# vim flag.conf
server {
    listen 80;
    server_name flag.com;
    location ~ ^/break {
        rewrite (.*) /test break;  # (. *) here represents all matching requests, and then performs the following operations
    }
    location ~ ^/last {
        rewrite (.*) /test last;
    }
    location /test {
        default_type text/html;
        return 200 'test';
    }
}
# Then write 172.16.1.5 flag in window host Finally, visit flag.com with different suffixes com

Test flag first com/last

Retest flag COM / break error reporting

Look at the error log

break Request:
1.request linux.rewrite.com/break
2.matching location ~ ^/break Will jump to linux.rewrite.com/test
3.After requesting a jump, go back and find the directory of the local site /test
4.If found, return/code/test/index.html Contents of the;
5.If the directory is not found, an Error 404 is reported. If the directory is found and the corresponding file is not found, an error 403 is reported
 
last request:
1.request linux.rewrite.com/last
2.matching location ~ ^/last Will jump to linux.rewrite.com/test
3.If found, return/code/test/index.html Contents of the;
4.If not found, the current server When a request is made, the access address becomes linux.rewrite.com/test
5.Re request server Will match location /test/ Directly return to the location Content of
6.If not location Match, and then return 404;

5. Difference between redirect and permanent

redirect: each request will ask the server. If the server is unavailable, it will fail to jump.
permanent: the first request will ask, and the browser will record the jump address. The second time, the server will not be asked, and jump directly through the address cached by the browser.

In lb01

[root@lb01 conf.d]# vim flag.conf
server {
    listen 80;
    server_name flag.com;
    location /redirect {
        rewrite (.*) http://www.baidu.com redirect;
    }
    location /permanent {
        rewrite (.*) http://www.baidu.com permanent;
    }
}

Test flag first com/redirect

Close nginx

Retest flag com/permanent

Close nginx

Keywords: Nginx

Added by bobbfwed on Sat, 08 Jan 2022 04:15:21 +0200