Article Directory
- 1. Overview
- 2. Experiment
-
- Experiment 1: redirect based on domain name
- Experiment 2: Access jump based on client IP
- Experiment 3: Jump and add directories based on old and new domain names
- Experiment 4: Jump based on parameter matching
- Experiment 5, jump based on all php files in the directory
- Experiment 6: Jump based on the most common URL request
1. Overview
1. Rewrite jump scene
- URL looks more standardized and reasonable
- Enterprises will disguise dynamic URL addresses as static addresses to provide services
- After the URL is changed to a new domain name, let the old visits jump to the new domain name
- Some business adjustments on the server
2. Rewrite actual scene
1. How to implement Nginx jump requirements
- Use rewrite for matching jump
- Jump after matching global variables using if
- Use location to match and then jump
2. Rewrite is placed in the server{}, if{}, location{} section
- location only works on the string after the domain name, except for the passed parameters
3. For domain name or parameter string
- Use if global variable matching
- Use proxy_pass reverse proxy
3. Compare rewrite and location
1. Similarities
- Can jump
2. The difference
- Rewrite is to change the path to obtain resources within the same domain name
- Location is to control access or reverse proxy for one-class path, and it can also proxy_ pass to other machines
3. The rewrite will be written in the location and the order of execution
- Execute the rewrite instruction in the server block
- Perform location matching
- Execute the rewrite command in the selected location
4. Location priority rules
1. Match a specific file
(location=full path)> (location ^~full path)> (location ~*full path)> (location ~full path)> (location full path) >(location /)
2. Use Directory matching access a certain file person
(location=directory)> (location ^~ and record/)> (location~directory) >(location~*directory)> (location directory)> (location /)
2. Experiment
Experiment 1: redirect based on domain name
Experimental needs
Based on the redirection of the domain name, the company’s old domain name www.kgc.com has business requirements and changes, and the new domain name www.newkgc.com needs to be used instead, but the old domain name cannot be abolished, and it needs to be redirected to the new domain name, and the following parameters constant.
Experimental steps
1. Install nginx and DNS
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y
yum install bind -y
Configure DNS and configure two domain names
www.kgc.com
www.newkgc.com
2. Edit the configuration file
vim /etc/nginx/conf.d/default.conf
3. Start the service
systemctl start named
systemctl start nginx
4. Web page access test
Address visit: www.newkgc.com
Experiment 2: Access jump based on client IP
Experimental needs
Today, the company’s business version is online, and all IP access to any content will display a fixed maintenance page, and only the company’s IP access is normal
Experimental steps
1. Edit the configuration file
vim /etc/nginx/conf.d/default.conf
The new content of the file is as follows
set $rewrite true;
if ($remote_addr = “192.168.235.100”) {
set $rewrite false;
}
if ($rewrite = true) {
rewrite (.+) /main.html;
}
location = /main.html {
root /usr/share/nginx/html;
}
2. Turn off the firewall and restart
iptables -F
setenforce 0
systemctl restart nginx
3. Modify the interface file for testing
cd /usr/share/nginx/html/
ls
cp -p index.html main.html
vim main.html
4. Interface access test
www.kgc.com
Experiment 3: Jump and add directories based on old and new domain names
Experimental needs
All posts under the domain name http://bbs.kgc.com are redirected to http://www.kgc.com/bbs, and the parameters remain unchanged after the domain name is redirected
Experimental steps
1. Modify dns configuration file
cd ~
cd / var / named /
ls
vim kgc.com.zone
2. Restart the DNS service
systemctl restart named
3. Edit the nginx configuration file
vim /etc/nginx/conf.d/default.conf
4. Restart the nginx service
systemctl restart nginx
5. Access test
Client setting domain name address: echo "nameserver 192.168.235.151"> /etc/resolv.conf
Browser access address: bbs.kgc.com/post/1.html
Automatically jump to the address: www.kgc.com/post/1.html
Experiment 4: Jump based on parameter matching
Experimental needs
Jump based on parameter matching, for example, now visit http://www.kgc.com/100-(100|200)-100.html to jump to http://www.kgc.com page.
Experimental steps
1. Edit the configuration file
vim /etc/nginx/conf.d/default.conf
2. Restart the service
systemctl restart nginx
3. Access test
Visit address: www.kgc.com/100-100-100.html, the interface automatically jumps to: www.kgc.com
Experiment 5, jump based on all php files in the directory
Experimental needs
Visit http://www.kgc.com/upload/1.php to jump to the homepage
Experimental steps
1. Edit the configuration file
vim /etc/nginx/conf.d/default.conf
2. Restart the service
systemctl restart nginx
3. Access test
The visit address is: www.kgc.com/upload/1.php, the interface automatically jumps to the homepage: www.kgc.com
Experiment 6: Jump based on the most common URL request
Experimental needs
Requirements: Visit a specific page to jump to the homepage
Verification: Browser visit http://www.kgc.com/abc/test.html to jump to the homepage
Experimental steps
1. Edit the configuration file
vim /etc/nginx/conf.d/default.conf
2. Restart the service
systemctl restart nginx
3. Access test
The visit address is: www.kgc.com/abc/test.html, the interface automatically jumps to the homepage: www.kgc.com