Common scene simulation of Nginx jump
1, Nginx jump introduction:
Now, Nginx has become the first choice for many companies as front-end reverse proxy servers. In practical work, they often encounter a lot of requirements for jump (Rewriting uRL).
For example, after changing the domain name, you need to keep the old domain name jump to the new domain name, change a web page and jump to a new page, website anti-theft chain and so on.
If the Apache server is used at the back end, although it can also jump and the rule base is very powerful, the jump efficiency with Nginx will be higher.
2, Common scenarios
① The URL looks more standardized and reasonable;
② In order to make the search engine search the website content and user experience better, enterprises will disguise the dynamic URL address as a static address to provide services; (there is?: hide the specific location in the dynamic address)
③ After changing the domain name of the website, let the old access jump to the new domain name;
④ Some business adjustments on the server side, such as URL adjustment according to special variables, directories and client information.
3, Common scene simulation
1. Domain name based jump:
Old domain name of the company www.lrz.com Com has business requirements changed, and a new domain name www.liuruizhi.com is required Com instead, but the old domain name cannot be abolished. You need to jump to the new domain name and keep the following parameters unchanged.
Steps:
① Add mapping:
vim /etc/hosts 192.168.65.139 www.liuruizhi.com www.lrz.com
② Create log directory:
mkdir -p /var/log/nginx/
③ To modify a profile:
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.lrz.com; #Domain name modification #charset koi8-r; access_log /var/log/nginx/www.lrz.com-access.log; #Open and modify the log saving path location / { #Insert at the original location if ($host = 'www.lrz.com'){ #$host is the rewrite global variable, which represents the request host header field or host name rewrite ^/(.*)$ http://www.liuruizhi.com/ permanent; # is the matching location variable, that is, the string behind the domain name, and it will jump permanently at the same time } } { ----->wq
④ Restart service:
systemctl restart nginx.service This is, all www.lrz.com All visits will jump to www.liuruizhi.com; also www.lrz.com/1.html An error will be reported and the domain name will become www.liuruizhi.com/1.html
2. Jump based on client IP access:
When the new version of the company's business is launched, all IP accesses are required to display a fixed maintenance page, and only the company's IP 192.168.65.129 accesses normally.
Note: delete the previous experimental configuration first.
step
① Profile:
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.lrz.com; #charset koi8-r; access_log /var/log/nginx/www.lrz.com-access.log; #Set whether it is a legal IP tag set $rewrite true; #The variable is $rewrite and the Boolean value is true #Judge whether it is legal IP if ($remote_addr = "192.168.65.129"){ #When the client IP is 192.168.65.129, the variable is set to false without rewriting set $rewrite false; } #In addition to legal IP, others are illegal IP. Rewrite and jump to the maintenance page if ($rewrite = true){ #Boolean value is true, override rewrite (.+) /weihu.html; #Override insert / Weihu. After accessing the IP html } location = /weihu.html { root /var/www/html; #The page returns / var / www / HTML / Weihu HTML content } location / { } } ------->wq
② Create maintenance page
mkdir -p /var/www/html echo '<h1>this is weihu web!</h1>' > /var/www/html/weihu.html systemctl restart nginx.service
Note: win10 virtual machine: add the mapping of hosts under windows/system32/drivers/etc
3. Jump to the new domain name based on the old domain name and add a directory:
When visiting BBS lrz. COM, now you need to jump to www.liuruizhi COM / BBS.
step
① Create the specified directory:
mkdir -p /usr/local/nginx/html/bbs/post echo "<h1>this is 1.html</h1>" >> /usr/local/nginx/html/bbs/post/1.html echo "192.168.65.129 bbs.lrz.com" >> /etc/hosts
② Change configuration file:
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name bbs.lrz.com; #Modify domain name #charset koi8-r; access_log /var/log/nginx/www.liuruizhi.com-access.log; #Modify log name #add to location /post { rewrite (.+) http://www.liuruizhi.com/bbs$1 permanent; } location / { } } ------->wq
③ Restart service
4. Jump based on parameter matching (redundant):
Visit www.lrz.com com/100-(100|200)-100. HTML will jump to www.lrz com.
step
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.lrz.com; #Modify domain name #charset koi8-r; access_log /var/log/nginx/www.lrz.com-access.log; #Modify log name #$request_uri built-in variable, that is, URI,\d is a pure number if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$){ #Set regular matching rewrite (.*) http://www.lrz.com permanent; } } ----->wq
Restart service
5. Jump based on all php ending files in the directory:
Request access http://www.lrz.com/upload/123.php Jump to the home page.
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.lrz.com; #Modify domain name #charset koi8-r; access_log /var/log/nginx/www.lrz.com-access.log; location ~* /upload/.*\.php$ { rewrite (.+) http://www.lrz.com permanent; } } --->wq
Restart service
6. Jump based on the most common url request:
Request access to a specific page: www.lrz.com com/abc123. HTML, jump to the home page.
step
vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.lrz.com; #Modify domain name #charset koi8-r; access_log /var/log/nginx/www.lrz.com-access.log; location ~* /abc/123.html { rewrite (.+) http://www.lrz.com permanent; } } --->wq
Restart service
4, Summary:
nginx:
1. Learn the difference / comparison between nginx and Apache:
Lightweight:
Positioning: nginx - static page processing, seven layer application layer, http https protocol
Static page processing: up to 3-5w concurrent
2. LNMP (combined with nginx experience)
Experience: how to connect nginx with fpm of php to realize dynamic and static separation
3. nginx optimization and anti-theft chain
nginx is one of the most popular services in enterprises
nginx optimization - > improve user experience and server performance
4. nginx jump
Purpose:
Service experience
Based on the exact matching of the visited URL s in different scenarios, guide the jump to the appropriate new scenario
nginx jumps in three ways:
rewrite
if
location