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

What is a MySQL tablespace?

The topic I want to share with you today is: &quo...

How to implement digital paging effect code and steps in CSS

A considerable number of websites use digital pagi...

Front-end JavaScript Promise

Table of contents 1. What is Promise 2. Basic usa...

MySQL 8.0.3 RC is about to be released. Let’s take a look at the changes

MySQL 8.0.3 is about to be released. Let’s take a...

Analysis of the reasons why MySQL field definitions should not use null

Why is NULL so often used? (1) Java's null Nu...

How to use domestic image warehouse for Docker

1. Problem description Due to some reasons, the d...

Analysis of rel attribute in HTML

.y { background: url(//img.jbzj.com/images/o_y.pn...

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

Steps to change mysql character set to UTF8 under Linux system

Table of contents 1. Check the MySQL status in th...

Content-type description, that is, the type of HTTP request header

To learn content-type, you must first know what i...

WeChat applet picker multi-column selector (mode = multiSelector)

Table of contents 1. Effect diagram (multiple col...

MySQL Optimization: Cache Optimization

I am happy that some bloggers marked my article. ...

The concept and characteristics of MySQL custom variables

A MySQL custom value is a temporary container for...