Several scenarios for using the Nginx Rewrite module

Several scenarios for using the Nginx Rewrite module

Application scenario 1: Domain name-based redirection

The company's old domain name is www.accp.com. Due to changes in business needs, it needs to be replaced with a new domain name www.kgc.com

1. The old domain name cannot be abolished
2. Jump from the old domain name to the new domain name, and keep its parameters unchanged

Deployment Environment

A Linux server (192.168.142.130)
A test host Windows 7

1. Install Nginx service

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

2. Modify the nginx default configuration file

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf ##Modify the default configuration file server {
    listen 80;
    server_name www.accp.com; ##Modify the host name#charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main; ##Open log service

3. Install bind resolution service

[root@localhost ~]# yum install bind -y

4. Modify the main configuration file (named.conf)

[root@localhost ~]# vim /etc/named.conf 
options {
                listen-on port 53 { any; }; ##Listen to all...
                allow-query { any; }; ##Allow all

5. Modify the zone configuration file (named.rfc1912.zones)

[root@localhost ~]# vim /etc/named.rfc1912.zones ##Configure the zone configuration file zone "accp.com" IN {
                type master;
                file "accp.com.zone"; ##accp zone data configuration file allow-update { none; };
};

6. Modify the regional data configuration file (accp.com.zone)

[root@localhost ~]# cd /var/named/ 
[root@localhost named]# cp -p named.localhost accp.com.zone ##Copy template [root@localhost named]# vim accp.com.zone ##Modify the zone configuration file $TTL 1D
@ IN SOA @ rname.invalid. (
                                    1D ; refresh
                                    1H ; retry
                                    1W ; expire
                                    3H ) ; minimum
                NS@
                A 127.0.0.1
www IN A 192.168.142.130 ##Local address [root@localhost named]# systemctl start named ##Start DNS service [root@localhost named]# systemctl stop firewalld.service ##Close firewall [root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx ##Start nginx service

7. Test the web page with a test machine

8. Modify the configuration file and set the domain name jump

[root@localhost named]# vim /etc/nginx/conf.d/default.conf ##Modify the configuration file server {
      listen 80;
      server_name www.accp.com;

      #charset koi8-r;
      access_log /var/log/nginx/www.accp.com-access.log main;

      location / {
          if ($host = "www.accp.com"){ ##Match if the domain name is the old domain name rewrite ^/(.*)$ http://www.kgc.com/$1 permanent; ##Then permanently set the jump to the new domain name}
          root /usr/share/nginx/html;
          index index.html index.htm;
      }

9. Add new domain name resolution

[root@localhost named]# vim /etc/named.rfc1912.zones 

zone "kgc.com" IN {
                type master;
                file "kgc.com.zone"; ##accp zone data configuration file allow-update { none; };
};

[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
##Copy the regional data configuration file as the data configuration file of kgc [root@localhost named]# systemctl restart named ##Restart the resolution service [root@localhost named]# systemctl restart nginx ##Restart the nginx service

10. Use the old domain name to access and check the web page jump


11. Add parameters after the old domain name and check whether there are parameters when jumping to the new domain name


Application scenario 2 - Access redirection based on client IP

The company's business version is online. All IPs accessing any content will display a fixed maintenance page. Only company IPs can access normally.

1. Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf 

server {
    listen 80;
    server_name www.accp.com;
    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    #Set whether the IP is legal or not set $rewrite true; ##Set the variable to true #Judge whether it is a legal IP
    if ($remote_addr = "192.168.142.120"){
        set $rewrite false; ##match a valid IP, set the variable to false, and redirect the page normally}
    #Judge and mark illegal IP if ($rewrite = true){ ##Match illegal IP and jump to main webpage rewrite (.+) /main.html;
    }
    #Match the tag to jump to the site location = /main.html { ##Exact match root /usr/share/nginx/html; ##Site path}

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

2. Create illegal IP sites and main web pages

[root@localhost conf.d]# cd /usr/share/nginx/html/ ##Switch to the site [root@localhost html]# vim main.html ##Edit illegal IP access web page content <h1>this is test web</h1>
[root@localhost html]# systemctl restart nginx ##Restart Nginx service

3. Visit the test web page


Application scenario 3 - Redirect based on old and new domain names and add directories

Redirect all posts under the domain name http://bbs.accp.com to http://www.accp.com/bbs and keep the parameters unchanged after the domain name redirection

1. Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf ##Modify the default configuration file server {
    listen 80;
    server_name bbs.accp.com; ##Modify service name#charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    location /post { ##Use location to match the post directory rewrite (.+) http://www.accp.com/bbs$1 permanent; ##Permanent redirect jump}

2. Modify the DNS zone data configuration file (accp.com.zone)

[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.com.zone ##Modify the regional data configuration file $TTL 1D
@ IN SOA @ rname.invalid. (
                          0 ; serial
                          1D ; refresh
                          1H ; retry
                          1W ; expire
                          3H ) ; minimum
        NS@
        A 127.0.0.1
bbs IN A 192.168.142.130
[root@localhost named]# systemctl restart named ##Restart resolution service [root@localhost named]# systemctl restart nginx ##Restart Nginx service [root@localhost named]# echo "nameserver 192.168.142.130" > /etc/resolv.conf 
##Put the resolution server address in the local resolution configuration file

3. Test the web page


Application scenario 4 - Jump based on parameter matching

Browser access: http://www.accp.com/100-(100|200)-100.html Jump to http://www.accp.com page

1. Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf 

server {
    listen 80;
    server_name www.accp.com;
    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$){    
    ##Match regular expressions starting with 100-(100|200)-one or more integers ending with html rewrite (.*) http://www.accp.com permanent; ##Permanently redirect to the homepage}

2. Modify the DNS zone data configuration file

  [root@localhost conf.d]# vim /var/named/accp.com.zone ##Modify the regional data configuration file www IN A 192.168.142.130  
  [root@localhost conf.d]# systemctl restart named ##Restart parsing service [root@localhost conf.d]# systemctl restart nginx ##Restart Nginx service

3. Test the web page


Application scenario 5 - Jump based on all PHP files in the directory

Visit http://www.accp.com/upload/1.php to jump to the homepage

1. Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
  [root@localhost conf.d]# vim default.conf ##Modify the default configuration file server {
      listen 80;
      server_name www.accp.com;
      #charset koi8-r;
      access_log /var/log/nginx/www.accp.com-access.log main;
      location ~* /upload/.*\.php$ { ##Match is case insensitive, match zero or more rewrites ending with .php after upload (.+) http://www.accp.com permanent; ##Jump to the homepage}
  [root@localhost conf.d]# systemctl restart nginx ##Restart Nginx service

2. Test web page


Application scenario 6 - Based on the most common URL request jump, visit a specific page to jump to the home page

1. Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
  [root@localhost conf.d]# vim default.conf ##Modify Nginx default configuration file server {
      listen 80;
      server_name www.accp.com;
      #charset koi8-r;
      access_log /var/log/nginx/www.accp.com-access.log main;
      location ~* ^/abc/123.html { ##Match a specific webpage rewrite (.+) http://www.accp.com permanent; ##Jump to the homepage}
  [root@localhost conf.d]# systemctl restart nginx ##Restart Nginx service

2. Test web page


The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx rewrite jump application scenarios

<<:  Detailed explanation of how MySQL (InnoDB) handles deadlocks

>>:  Vue implements anchor positioning function

Recommend

jQuery canvas generates a poster with a QR code

This article shares the specific code for using j...

Vue uses element-ui to implement menu navigation

This article shares the specific code of Vue usin...

HTML table tag tutorial (12): border style attribute FRAME

Use the FRAME property to control the style type ...

JavaScript Dom Object Operations

Table of contents 1. Core 1. Get the Dom node 2. ...

In-depth explanation of Set and WeakSet collections in ES6

Table of contents Set is a special collection who...

...

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...

Nginx stream configuration proxy (Nginx TCP/UDP load balancing)

Prelude We all know that nginx is an excellent re...

Basic use of javascript array includes and reduce

Table of contents Preface Array.prototype.include...

SQL query for users who have placed orders for at least seven consecutive days

Create a table create table order(id varchar(10),...

How to run py files directly in linux

1. First create the file (cd to the directory whe...

How to manage large file uploads and breakpoint resume based on js

Table of contents Preface Front-end structure Bac...