Resource separation and Rewrite of NGINX Rewrite

Resource separation and Rewrite of NGINX Rewrite

·Dynamic and static separation

·Introduction and use of Rewrite

1, Dynamic and static separation

Introduction: it mainly separates the static resources of website resources and places them at the hanging point

be based on bbs Based on successful deployment
1
,establish NFS Mount point mkdir /static vim /etc/exports /static 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666) systemctl restart nfs-server chown -R www.www /static/ 2,Place static resources in the mount point mkdir /opt/static/s/ [root@web01 static]# cp -r /opt/bbs/static/* /opt/static/s/ 3,Mount to lb yum install nfs-utils -y mount -t nfs 172.16.1.31:/static /opt/static/ 4,test

2, Basic overview of Rewrite

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

Basic overview of Rewrite

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 the user requests the 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 engine, SEO optimization depends on url path, and easy to remember url is convenient for search engine input.
ps: improve search rate and strengthen search ranking

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;
}

Rewrite Flag

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:

flageffect
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
server {
    server_name _;
    listen 80;
    location ~ ^/break {
        rewrite (.*) /test break;
    }

    location ~ ^/last {
        rewrite (.*) /test last;
    }

    location /test {
        default_type text/html;
        return 200 "test";
    }
}

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 / test in the local site directory
4. If found, return / code / test / index HTML content;
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 content;
4. If it is not found, it will re initiate the request for the current server. At this time, the access address will become Linux rewrite. com/test
5. Re requesting the server will match the location /test / and directly return the content of the location
6. If there is no location match, return 404;

The difference between redirect and permanent

redirect
server {
    server_name _;
    listen 80;
    location ~ ^/break {
        rewrite (.*) /test break;
    }

    location ~ ^/last {
        rewrite (.*) /test last;
    }

    location /test {
        default_type text/html;
        return 200 "test";
    }    
    location /redirect {
    rewrite (.*) http://www.baidu.com redirect;
     }
     location /permanent {
          rewrite (.*) http://www.baidu.com 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, it will not ask the server, and jump directly through the address cached by the browser.

 

 

 

 

Added by martinco on Tue, 11 Jan 2022 14:21:47 +0200