Implementation of rewrite jump in nginx

Implementation of rewrite jump in nginx

1. New and old domain name jump

Application scenario: Domain name-based redirection. The company’s old domain name is: www.peihua.com

If there are business needs to be changed, a new domain name www.zhenguo.com needs to be used instead, but the old domain name cannot be abolished. You need to jump to the new domain name, and the following parameters remain unchanged

Configure DNS, and configure www.peihua.com (old) and www.zhenguo.com (new) resolution respectively

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

(You must have the official source to install nginx with yum)

yum install nginx -y
rpm -qc nginx //Find the configuration file

Modify the nginx configuration file

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

server {
  listen 80;
  server_name www.peihua.com; #Domain name modification#charset koi8-r;
  access_log /var/log/nginx/peihua.com-access.log main; #Log modification location / {
    #Domain redirectionif ($host = 'www.peihua.com') {
 rewrite ^/(.*)$ http://www.zhenguo.com/$1 permanent;
    }
    root /usr/share/nginx/html;
    index index.html index.htm;
  }

[root@localhost named]# systemctl restart nginx

Configuring Domain Name Resolution

yum -y install bind

#Modify the main configuration file [root@localhost conf.d]# vim /etc/named.conf
options {
    listen-on port 53 { any; }; //Change to any
    listen-on-v6 port 53 { ::1; };
    directory "/var/named";
    dump-file "/var/named/data/cache_dump.db";
    statistics-file "/var/named/data/named_stats.txt";
    memstatistics-file "/var/named/data/named_mem_stats.txt";
    recursing-file "/var/named/data/named.recursing";
    secroots-file "/var/named/data/named.secroots";
    allow-query { any; }; //Change to any

#Modify the regional configuration file [root@localhost conf.d]# vim /etc/named.rfc1912.zones 
zone "peihua.com" IN {
    type master;
    file "peihua.com.zone";
    allow-update { none; };
};

zone "zhenguo.com" IN {
    type master;
    file "zhenguo.com.zone";
    allow-update { none; };
};

#Modify the regional data file [root@localhost conf.d]# cd /var/named/
[root@localhost named]# cp -p named.localhost peihua.com.zone
[root@localhost named]# cp -p peihua.com.zone zhenguo.com.zone

[root@localhost named]# systemctl start named

Browser Testing

The browser input simulates access to http://www.peihua.com/test/1/index.php (although this request content does not exist) and jumps to
http://www.zhneguo.com/test/1/index.php, from the headers you can see 301, which means a permanent redirect has been achieved, and the parameters after the domain name also redirect normally.

2. IP-based access redirection

Application scenario: Access redirection based on client IP. For example, today the company's business version is launched. All IPs accessing any content will display a fixed maintenance page. Only the company IP: 12.0.0.100 can access normally.

Modify the nginx configuration file

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

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

 #charset koi8-r;
  charset 'utf-8'; //Recognize Chinese characters access_log /var/log/nginx/peihua.com-access.log main;
  #Set whether the IP is legal or not set $rewrite true;
  #Judge whether it is a legal IP
  if ($remote_addr = "12.0.0.100") {
 set $rewrite false;
 }
 # Illegal IP is judged and marked if ($rewrite = true) {
 rewrite (.+) /maintenance.html;
 }
 #Match the tag to jump to the site location = /maintenance.html {
 root /usr/share/nginx/html;
 }

Add custom page content to maintenance.html

[root@localhost html]# cat /usr/share/nginx/html/maintenance.html 
<h1>Website is under maintenance</h1>

Browser Testing

Use the IP address 12.0.0.100 to access http://www.peihua.com, and it can be accessed normally. Use the IP address 12.0.0.99 to access http://www.peihua.com, and the maintenance page will be displayed.

3. Jump to the new domain name based on the old domain name and add a directory after it

Application scenario: Jump to the new domain name based on the old domain name and add a directory after it, for example, the current visit is http://bbs.peihua.com.
Now you need to redirect all posts under this domain name to http://www.peihua.com/bbs. Remember to keep the parameters unchanged after the domain name redirection.

Modify the nginx configuration file

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

  listen 80;
  server_name bbs.peihua.com;
  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;
  location /post {
       rewrite (.+) http://www.peihua.com/bbs$1 permanent;
  }

Note: accp.com.zone needs to change the host domain name resolution, change www to bbs

[root@localhost named]# cd /var/named
[root@localhost named]# vim peihua.com.zone 
$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 12.0.0.25

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

Browser access

The browser visits http://bbs.peihua.com/post/a.html and is redirected to www.peihua.com/bbs/post/a.txt

4. Jump based on parameter matching

Application scenario: Redirection based on parameter matching, for example, visiting http://www.peihua.com/100-(100 | 200)-100.html
Jump to http://www.peihua.com

Modify the nginx configuration file

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

server {
  listen 80;
  server_name www.peihua.com;
  
  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;
  
  if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
     rewrite (.*) http://www.peihua.com permanent;
  }

Note: \d matches a number between 0 and 9

Modify the DNS and change bbs to www

[root@localhost named]# vim peihua.com.zone 
$TTL 1D
@ IN SOA @ rname.invalid. (
                    0 ; serial
                    1D ; refresh
                    1H ; retry
                    1W ; expire
                    3H ) ; minimum
    NS@
    A 127.0.0.1
www IN A 12.0.0.25

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

Browser Testing

The browser visits http://www.peihua.com/100-200-26.html and is redirected to the homepage of www.peihua.com

5. Redirect based on all pages ending with php and based on a certain page

Modify the nginx configuration file

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

server {
  listen 80;
  
  server_name www.peihua.com;
  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;
  location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.peihua.com permanent;
  }


[root@localhost named]# systemctl restart nginx

Browser access

The browser visits http://www.peihua.com/upload/a.php and is redirected to www.peihua.com

6. Redirect based on a specific page /abc/123.html

Modify the nginx configuration file

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

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

  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;

  location ~* ^/abc/123.html {
    rewrite (.+) http://www.peihua.com permanent;
  }
  
[root@localhost named]# systemctl restart nginx

Browser access

The browser visits http://www.peihua.com/abc/123.html and is redirected to www.peihua.com

This is the end of this article about the implementation of rewrite jump in nginx. For more relevant nginx rewrite jump content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the solution for NGINX to jump from https to http
  • How to redirect URL using nginx rewrite
  • How to redirect HTTP 301 to a domain name with www in Nginx server
  • How to force nginx to use https access (http jumps to https)
  • How to configure Nginx page redirection according to different browser languages
  • Detailed explanation of nginx to solve the problem of home page jump
  • Detailed explanation of nginx 301 redirect to domain name with www
  • Solution to nginx not jumping to the upstream address
  • How to redirect to https through nginx load balancing
  • Using Nginx's map command to redirect pages
  • Solve the problem of only redirecting to the home page when deploying thinkPHP 5 with nginx
  • Example code for using Nginx to implement 301 redirect to https root domain name
  • Nginx prohibits direct access via IP and redirects to a custom 500 page
  • Detailed explanation of Nginx rewrite jump application scenarios
  • Detailed explanation of how to enable HSTS in nginx to force the browser to redirect to HTTPS access
  • Detailed explanation of location matching and rewrite redirection in Nginx
  • Nginx hidden redirect (browser URL remains unchanged after redirection)

<<:  A brief analysis of MySQL's WriteSet parallel replication

>>:  Element Timeline implementation

Recommend

How to solve the problem that the project in eclipse cannot be added to tomcat

1. Right-click the project and select properties ...

Tutorial on installing Ceph distributed storage with yum under Centos7

Table of contents Preface Configure yum source, e...

Native JS to achieve blinds special effects

This article shares a blinds special effect imple...

Detailed description of the function of meta name="" content="

1. Grammar: <meta name="name" content...

Docker solution for logging in without root privileges

When you use the docker command for the first tim...

mysql-canal-rabbitmq installation and deployment super detailed tutorial

Table of contents 1.1. Enable MySQL binlog 1.2. C...

Suggestions on creating business HTML emails

Through permission-based email marketing, not onl...

Linux kernel device driver system call notes

/**************************** * System call******...

If I change a property randomly in Vue data, will the view be updated?

Interviewer: Have you read the source code of Vue...

How to use the VS2022 remote debugging tool

Sometimes you need to debug remotely in a server ...

uniapp project optimization methods and suggestions

Table of contents 1. Encapsulate complex page dat...