Nginx basic module rewrite

Rewite rules function

Rewrite rules can achieve URL rewriting and redirection

Role scene:

URL access jump, support development and design, such as page jump, compatibility support, display effect and other
SEO optimization
Maintenance: background maintenance, traffic forwarding, etc.
Security

Rewrite practical scenarios

The implementation of Nginx jump requirements
Use rewrite to match and jump
Use if to match global variables and then jump
Use location to match and then jump
rewrite in the server{}, if{}, location{} section Use
domain name or parameter string
if global variable matches
use proxy_pass reverse proxy
Insert picture description here

Common regular expression metacharacters

The Nginx rewrite module supports regular expressions. Regular expressions are mentioned in the previous article. If you are in a good mood, you can look back.
Insert picture description here

Rewrite

rewrite <regex regular> <replacement content after jump> [flag flag supported by flag rewrite]

Flag mark illustration

Insert picture description here

last vs. break graphInsert picture description here

Location

Location common matching format

location = condition {} [exact match]
ocation condition {} [general match]
location ~ condition {} [regular match]

Common expressions
Insert picture description here

location priority

If the expression types are the same, those with more words will be matched first

The priority of types is as follows
= type
^= type
~ and ~* type
regular string matching type
general matching/

Matching a specific file is based on this
(location = full path)>( location ^~ full path)>( location ~* full path)>( location ~ full path)>(location full path)>( location /)

Use directory to match to access a certain file
(location = directory)>( location ^~ directory)>( location ~ directory)>
(location ~* directory)>( location directory)>( location /)

location = / {
    
    	##精确匹配 /,主机名后面不能带任何字符串'
    [configuraion A ]	
}

location / {
    
    	##所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配'
    [configuraion B ]
}

location /documents/ {
    
    		##匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用'
    [configuraion C ]
}

location ~ /documents/abc {
    
    		##匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用'
    [configuraion D ]
}

location ^~ /images/ {
    
    	##以/images/开头的地址,匹配符合后,停止往下匹配'
    [configuraion E ]
}

location ~*\.(gif|jpg|gpeg)$ {
    
    	##匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高'
    [configuraion F ]
}

location /images/abc {
    
    	##最长字符匹配到 /images/abc,优先级最低'
    [configuraion G ]
}

location ~ /images/abc {
    
    	##以/ Images/abc开头的,优先级次之'
    [configuraion H ]
}

location /images/abc/1.html {
    
    	##如果和正则 ~ images/abc/1.htm相比,正则优先级更高'
    [configuraion I ]

The difference between rewrite and location

Same point

Can jump

difference

Rewrite is to change the path to obtain resources in the same domain name.
Location is to control access or reverse proxy for a type of path, and it can also proxy_pass to other machines.

Rewrite knocks in location, execution order

Execute the rewrite instruction in the server block
Execute location matching
Execute the rewrite instruction in the selected location

bring it on! Show! !

Before nginx, we have been using source code to compile and install, this time we use the big yellow dog to install

[root@5centos ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@5centos ~]# yum -y install nginx

Realize the redirect function according to the domain name

For example, the company’s old domain name www.ora.com suddenly doesn’t want to use it, and needs to use the new domain name www.nge.com instead, but the old domain name cannot be abolished, it needs to be redirected to the new domain name, and the following parameters remain unchanged.
Install DNS service
settings Two domain names www.ora.com and www.nge.com
DNS installation and configuration were written in the previous blog: DNS service configuration

[root@5centos named]# echo "nameserver 20.0.0.5" > /etc/resolv.conf
[root@5centos named]# nslookup www.ora.com
Server:		20.0.0.5
Address:	20.0.0.5#53

Name:	www.ora.com
Address: 20.0.0.5

[root@5centos named]# nslookup www.nge.com
Server:		20.0.0.5
Address:	20.0.0.5#53

Name:	www.nge.com
Address: 20.0.0.5

View the location of nginx configuration file

[root@5centos /]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf     ##默认主配置文件
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug

Modify the main configuration file

[root@5centos /]# vim /etc/nginx/conf.d/default.conf
  server {
    
    
    listen       80;
    server_name  www.ora.com;
  location / {
    
       ##找到这行
        if ($host = "www.ora.com"){
    
    
          rewrite ^/(.*)$ http://www.nge.com/$1 permanent;
        }
        root   /usr/share/nginx/html; ##在这行上面添加
        index  index.html index.html
    }
    ……省略部分……
[root@5centos /]# cd /usr/share/nginx/html/
[root@5centos html]# ls
50x.html  index.html
[root@5centos html]# cp -p index.html index.html.back
[root@5centos html]# vim index.html
<h1>ORA</h1>
~                                 
[root@5centos /]# systemctl start nginx
[root@5centos /]# setenforce 0
[root@5centos /]# iptables -F

Insert picture description hereThis is the function of rewrite and permanent

Redirect based on client IP access

When the company's website is initially online, sometimes it is necessary to allow users to access an interface, allowing developers to access the normal online interface

[root@5centos html]# vim /etc/nginx/conf.d/default.conf 
server {
    
    
    listen       80;
    server_name  www.ora.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    set $rewrite false;     ##设置变量为 false
    if ($remote_addr = "20.0.0.5"){
    
       ##如果访问IP 为本机 IP
                set $rewrite ture;    ##设置变量为 ture
        }
    if ($rewrite = false){
    
                ##若上面一条不成立的情况,如果变量还为 false 
                rewrite (.+) /fangwen.html;
        }
    location = /fangwen.html {
    
            ##匹配上一条的标记
                root /usr/share/nginx/html;   ##目录位置
                index  fangwen.html;          ##主页名称
        }
    location / {
    
                          ##在 变量为 ture 正常匹配
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

Use this machine for access test, the page is normal

Insert picture description hereAccess using client
Insert picture description here

Redirect according to domain name and add webpage path

Visit www.nge.com/post and automatically jump to www.ora.com/bbs/post

[root@5centos post]# vim index.html
[root@5centos post]# pwd
/usr/share/nginx/html/bbs/post
[root@5centos post]# 
server {
    
    
    listen       80;
    server_name  www.nge.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    location /post {
    
    
        rewrite (.+) http://www.ora.com/bbs$1 permanent;
        }
        location / {
    
    

        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

Insert picture description here

Jump according to parameter matching

Visit www.ngecom/500-100 or 200-50, automatically jump to the ora website homepage

[root@5centos /]# vim /etc/nginx/conf.d/default.conf  
server {
    
    
    listen       80;
    server_name  www.nge.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    if ($request_uri ~ ^/500-(100|200)-(\d+).html$){
    
    
        rewrite (.*) http://www.ora.com permanent;
        }
[root@5centos /]# systemctl restart nginx.service

Insert picture description here

Visit PHP Jump to homepage

Visit any PHP file in the upload directory under the nge site, and you will access the ora homepage

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

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
        local ~* /upload/.*\.php$ {
    
    
                rewrite (.+) http://www.ora.com permanent;
        }

Ordinary URL request redirect

Visit www.nge.com/1/test.com and jump to www.ora.com

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

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
        location ~* ^/1/test.html {
    
    
        rewrite (.+) http://www.ora.com permanent;
        }

Guess you like

Origin blog.csdn.net/Ora_G/article/details/107941899