Web server basics-Nginx rewrite case combat (error page redirection, virtual directory alias redirection, domain name redirection)

Nginx rewrite case combat (error page redirection, virtual directory alias redirection, domain name redirection)


This environment is based on the Centos 7.8 system to build the Nginx learning environment. For
specific construction, please refer to Nginx-1.18.0 Environment Deployment

Nginx rewrite is the same as Apache and other web service software, the main function of Nginx rewrite is to realize URL address rewriting. Nginx's rewrite rules need the support of PCRE software, that is, rule matching is performed through Perl compatible regular expression grammar.


1. Regularity in Nginx

Regular matching in Nginx

  • The difference between ~ and ~*
  • ~ Matching content is case sensitive
  • ~* Matching content indiscriminately lowercase
  • !~ Invert
  • ^~ But multiple matches exist at the same time, match the content of ^~ first; do not check the regular expression (priority processing)

Use grammar

The
location instruction of the ocation syntax description table is used to execute different applications according to the URI requested by the user.
Description of the matching order of different uri and special character combinations
location [=| | *|^~] uri { }

location [=、~ 、*、^] hate {…}
instruction Match ID Matching website address Configuration section to be executed after matching URI

Insert picture description here

Two, Nginx rewrite case actual combat

Flag description at the end of the rewrite command

flag symbol Description
last After this rule is matched, continue to match the new location URI rule
break This rule will be terminated when the matching is completed, and will no longer match any subsequent rules
redirect Return to 302 temporary redirection, the URL address after the redirection will be displayed in the address bar of the browser
permanent Return to 301 permanent redirection, the URL address after the redirection will be displayed in the address bar of the browser

Enterprise application scenarios of Nginx rewrite

  • Can adjust the user's browsingURL, looks more canonical, In line with the needs of development and product personnel.
  • In order to make search engines search for website content and better user experience, companies willDynamic URL address disguised as static addressProvide services.
  • After the URL is changed to a new domain name, let the old one visitJump to a new domain nameon. For example, visiting 360buy.com on JD will redirect to jd.com.
  • according toURL adjustment for special variables, directories, client information, etc.

Configure Nginx-based web services

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf 
    server {
    
    
        listen       192.168.5.11:80;
        server_name  news.123.cn;
        location / {
    
    
            root   /usr/share/nginx/html/news;
            index  index.html index.htm;
           }        
       }

[root@node01 ~]# vim /usr/share/nginx/html/news/index.html 
news test page...

[root@node01 ~]# systemctl enable --now nginx
[root@node01 ~]# netstat -lnutp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2219/nginx: master  

Browser access: http://news.123.cn/
Insert picture description here

1. Error page redirection

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf 
    server {
    
    
        listen       192.168.5.11:80;
        server_name  news.123.cn;
        location / {
    
    
            root   /usr/share/nginx/html/news;
            index  index.html index.htm;
            if (!-f $request_filename){
    
    
            rewrite /.* err.html permanent;
            }
           }
       }
[root@node01 ~]# nginx -s reload

[root@node01 news]# echo 'this page is not exists...' > err.html
[root@node01 news]# ll
total 8
-rw-r--r-- 1 root root 27 Feb 22 22:45 err.html
-rw-r--r-- 1 root root 18 Feb 22 14:23 index.html

Test: http://news.123.cn/big_date.jpg
Insert picture description here

2. Virtual directory alias redirection

[root@node01 bbs]# vim /etc/nginx/conf.d/host.conf 
    server {
    
    
        listen       192.168.5.11:80;
        server_name  bbs.123.cn;
        location / {
    
    
            root   /usr/share/nginx/html/bbs;
            autoindex on;
            index  index.html index.htm;
            rewrite ^/virtual_dir/(.*) /first/secoend/web_age/$1 last;
        location /nginx_status {
    
    
        stub_status on;
        access_log off;
        }
        }
   }

[root@node01 bbs]# nginx -s reload

[root@node01 bbs]# cd /usr/share/nginx/html/bbs/
[root@node01 bbs]# mkdir first/secoend/web_age -p
[root@node01 bbs]# echo 'this is virtual dir test page...' > /first/secoend/web_age/virtusl_dir.html

Test: http://bbs.123.cn/virtual_dir/virtusl_dir.html
Insert picture description here

3. Domain name redirection

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf
    server {
    
    
        listen       192.168.5.11:80;
        server_name  bbs.123.cn;
        rewrite .* http://mirrors.aliyun.com/;
        location / {
    
    
            root   /usr/share/nginx/html/bbs;
            index  index.html index.htm;
           }
        }
[root@node01 ~]# nginx -s reload

Test:
Browser visit: http://bbs.123.cn/

Insert picture description here
Jump successfully
Insert picture description here

[root@node01 ~]# vim /etc/nginx/conf.d/host.conf 
    server {
    
    
        listen       192.168.5.11:80;
        server_name  bbs.123.cn;
        rewrite .* https://blog.csdn.net/XY0918ZWQ;
        location / {
    
    
            root   /usr/share/nginx/html/bbs;
            index  index.html index.htm;
           }
        }

[root@node01 ~]# nginx -s reload

Test:
Browser access: http://bbs.123.cn/
Insert picture description here
jumped successfully
Insert picture description here

Guess you like

Origin blog.csdn.net/XY0918ZWQ/article/details/113956416