Rewrite web page jump example (detailed explanation)

Domain-based redirect

Now the company’s old domain name www.wt.com has business requirements changes and needs to be replaced by a new domain name www.dw.com, but the old domain name cannot be abolished, it needs
to be redirected to the new domain name, and the following parameters remain unchanged.

vim /usr/local/nginx/conf/nginx.conf
server {
    listen       80;
	server_name www.wt.com     #域名修改
	charset utf-8
	access_log /var/log/nginx/www.wt.com-accsee.log;    #日志修改
	location / {
	#添加域名重定向
	    if ($host = 'www.wt.com'){            #$host为rewrite全局变量。代表请求主机头字段或主机名
		    rewrite ^/(.*)$ http://www.dw.com /$1 permanent;     #$1为正则匹配的内容,即域名后边的字符串
		}
		root html;
		index index.html index.htm;
	}
}

Insert picture description here

mkdir -p /var/log/ngingx
echo "192.168.153.10 www.wt.com www.dw.com" >> /etc/hosts
systemctl restart nginx

Insert picture description here

mkdir -p /usr/local/nginx/html/test/
echo 'this ih test web!' > /usr/local/nginx/html/test/1.html
systemctl restart nginx.service 

The browser input simulation visit http://www.wt.com/test/1.html
will jump to www.dw.com/test/1.html, check the element, you can see the return 301. Realize the permanent redirection jump And the parameters after the domain name are also redirected normally.
Insert picture description here

Client IP jump access based on domain name

The new version of the company’s business is online today, requiring all IP ranges and any content to display a fixed maintenance page, only the company’s IP: 192.168.153.10 is accessible normally

vim /usr/local/nginx/conf/nginx.conf
server {
     listen        80;
	 server_name   www.wt.com      #域名修改
     charset utf-8
	 access_log /var/log/nginx/www.wt.com-accsee.log;    #日志修改
	 
	 #设置是否合法的IP标记              
	 set $rewirte true;                        #设置变量$rewirte,变量值为boole值true
	 #判断是否为合法IP
	 if ($remote_addr = "192.168.153.10"{      #当客户端IP为192.168.153.10时,将变量值设为false,不进行重写
	     set $rewirte false;    
	}
                                	#除了合法IP,其他都是非法IP,进行重写跳转维护页面
	if ($rewrite = true) {          #当变量值为true时,进行重写
     rewrite (.+) /weihu.html;      #重写在访问IP后边插入/weihu.html,例如192.168.153.10/weihu.html
    }
    location = /weihu.html {
       root /var/www/html;     #网页返回/var/www/html/weihu.html的内容
    }
 
    location / {
         root    html;
         index   index.html index.htm;
    }
 }

Insert picture description here

 mkdir -p /var/www/html/
 echo 'weihu!'> /var/www/html/weihu.html
 systemctl restart nginx

Insert picture description here

Only IP 192.168.153.10 can be accessed normally, other addresses are maintenance pages
Insert picture description here
Insert picture description here

Jump to the new domain name based on the old domain name and add a directory

Now you are visiting http://dw.wt.com, now you need to redirect all visits under this domain name to http://www.wt.com/dw

vim /usr/local/nginx/conf/nginx.conf
server {
	listen 80;
	server_name  dw.wt.com;			#域名修改
	charset utf-8;
	access_log /var/log/nginx/ www.wt.com-access.log; 
	#添加
	location /post {
	rewrite (.+) http://www.wt.com/dw$1 permanent; #这里的$1为位置变量,代表/post
	}
	location / {
		root html;
		index index.html index.html;
	}
}

Insert picture description here

mkdir -p /usr/local/nginx/html/dw/post
echo "this is 1.html" >> /usr/local/nginx/html/dw/post/1.html
echo "192.168.153.10 dw.wt.com" >> /etc/hosts
systemctl restart nginx

Use a browser to visit http://dw.wt.com/post/1.html to jump to http://www.wt.com/dw/post/1.html
Insert picture description here

Jump based on parameter matching

Visit http://www.wt.com/100-(100|200)-100.html to jump to the http//www.wt.com page.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen 80;
	server name www.wt.com;			#域名修改
	charset utf-8;
	access_ log / var/ log/ nginx/www.wt.com-access.log; 
	
	 location ~ ^/100\-(100|200)\-(\d)+\.html$ {
              rewrite (.+) http://www.wt.com permanent;
    }
	
	location / {
		root html;
		index index.html index.htm;
	}
}
	
	
systemctl restart nginx

Insert picture description here

Use a browser to visit http://www.wt.com/100-200-100.html or http://www.wt.com/100-100-100.html to jump to
http://www.wt. com page.
Insert picture description here

Jump based on all php ending files in the directory

Request to visit http://www.wt.com/upload/123.php to jump to the homepage.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen	80;
	server name www.wt.com;		#域名修改
	charset utf-8;
	access_log /var/log/nginx/www.wt.com-access.log; 

	location ~* ^/upload/.*\.php$ {
         rewrite (.+) http://www.wt.com permanent;
    }

	location / {
		root html;
		index index.html index.htm;
	}
}

systemctl restart nginx

Insert picture description here

The browser visits http://www.wt.com/upload/123.php to jump to the http://www.wt.com page.
Insert picture description here

Jump based on the most common url request

Request to visit a specific page such as http://www.wt.com/abc/123.html Jump to the homepage

vim /usr/local/nginx/conf/nginx.conf
server {
	listen 80;
	server name www.wt.com;			#域名修改
	charset utf-8;
	access_log /var/log/nginx/www.wt.com-access.log;
	
	location ~* ^/abc/123.html {
		rewrite (.+) http://www.wt.com permanent;
	}
	location / {
		root html ;
		index index.html index.html; 
	}
}

systemctl restart nginx

Insert picture description here

Browser visit http://www.wt.com/abc/123.html to jump to http://www.wt.com page.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/113665109