Redirect rewrite rule for Nginx
Brief introduction:
The rewrite function of nginx needs the support of pcre software, that is, to match rules through perl compatible regular expression statements;
nginx compiled with default parameters will support rewriting modules, but it must also be supported by pcre
rewrite is the key instruction to realize url rewriting. According to the content of regex (regular expression), it is redirected to replacement with flag at the end;
[root@node-130 nginx-1.17.10]# ./configure --help | grep rewrite --without-http_rewrite_module disable ngx_http_rewrite_module
rewrite syntax:
rewrite + <regex> + <replacement> + [flag]; regular Alternative content flag sign
Detailed explanation of grammar:
- Regular: perl is compatible with regular expression statements for rule matching
- Substitute content: replace the regular matching content with replacement
- Flag flag: flag flag flag supported by rewrite
- last: after this rule matching is completed, terminate the rule of the current location and continue to match the new location URI rule downward
- break: this rule will terminate upon completion of matching and will not match any subsequent rules
- redirect: 302 is returned for temporary redirection. The browser address will display the URL address after the jump. Turn off the service and redirection cannot be performed.
- Permanent: 301 is returned for permanent redirection, and the URL address after the jump will be displayed in the browser address bar. If the service is shut down, redirection can still be performed, and cache invalidation can be cleared.
Give examples of parameters
[root@node-130 nginx]# vim /data/nginx/conf/vhost/www.liangjiawei.net.conf server { listen 80; server_name www.liangjiawei.net; location / { root /data/html/www/; index index.html index.htm; rewrite ^/(.*)$ http://blog.liangjiawei.net/$1 permanent; } }
rewrite rule experiment
Experiment of domain name jump
- It means you visited www.liangjiawei.com Net website
- Then jump to blog liangjiawei. Net website
Scheme I:
www.liangjiawei. Explanation of rewrite rule of. Net
- In this configuration file, it refers to www.liangjiawei.com Net resources of this website
- ^/(. *) $: it means that you can access any data and enter anything
- Equivalent to regular content
- http://blog.liangjiawei.net/ what is specified here is the website to jump to;
- "/ $1": This represents the input that follows the resource path of the website
- For example, blog liangjiawei. Net / img / – > resources
- permanent: This is equivalent to the flag tag
- Is a permanent redirect, code 301
#Write the configuration file of nginx #Write this rewrite rule from the previous virtual host [root@node-130 nginx]# vim /data/nginx/conf/vhost/www.liangjiawei.net.conf server { listen 80; server_name www.liangjiawei.net; location / { root /data/html/www/; index index.html index.htm; rewrite ^/(.*)$ http://blog.liangjiawei.net/$1 permanent; } } #Take another look at blog liangjiawei. Net configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/blog.liangjiawei.net.conf server { listen 80; server_name blog.liangjiawei.net; location / { root /data/html/blog/; index index.html index.htm; } } #Overload profile verification [root@node-130 nginx]# nginx -s reload #If you use the command line, you need to add the - I parameter [root@node-130 nginx]# curl www.liangjiawei.net -I HTTP/1.1 301 Moved Permanently Server: nginx/1.17.10 Date: Mon, 21 Jun 2021 08:47:11 GMT Content-Type: text/html Content-Length: 170 Connection: keep-alive Location: http://blog.liangjiawei.net/
Browser verification: jump successful
Scheme II experiment
Modify www.liangjiawei.com Net
-
return rule is required
-
The specified status code is 302
-
Then specify the website of the blog
- $request_uri; This is the resource requested by the user
#Modify profile [root@node-130 nginx]# vim /data/nginx/conf/vhost/www.liangjiawei.net.conf server { listen 80; server_name www.liangjiawei.net; location / { root /data/html/www/; index index.html index.htm; return 302 http://blog.liangjiawei.net/$request_uri; } } #blog.liangjiawei.net configuration file remains unchanged #Take another look at blog liangjiawei. Net configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/blog.liangjiawei.net.conf server { listen 80; server_name blog.liangjiawei.net; location / { root /data/html/blog/; index index.html index.htm; } } #Overload profile verification [root@node-130 nginx]# nginx -s reload #If you use the command line, you need to add the - I parameter [root@node-130 nginx]# curl -I www.liangjiawei.net HTTP/1.1 302 Moved Temporarily Server: nginx/1.17.10 Date: Mon, 21 Jun 2021 08:54:06 GMT Content-Type: text/html Content-Length: 146 Connection: keep-alive Location: http://blog.liangjiawei.net//
Browser access – >
Method 3:
- Here is the judgment statement using an if
- if (): it means to judge the contents in parentheses
- $host != ‘ www.liangjiawei.net: this means that if the requested address is not www.liangjiawai Net
- !=: It represents the concept of logical non;
- $host = ‘www.liangjiawei.net 'means to perform the following operations after matching;
- The specified action is the rewrite rule;
#Modify www.liangjiawei.com Net [root@node-130 nginx]# !vim vim /data/nginx/conf/vhost/www.liangjiawei.net.conf server { listen 80; server_name www.liangjiawei.net; if ( $host = 'www.liangjiawei.net' ) { rewrite ^/(.*)$ http://blog.liangjiawei.net/$1 permanent; } location / { root /data/html/www/; index index.html index.htm; } } #blog.liangjiawei.net configuration file remains unchanged #Take another look at blog liangjiawei. Net configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/blog.liangjiawei.net.conf server { listen 80; server_name blog.liangjiawei.net; location / { root /data/html/blog/; index index.html index.htm; } } #Overload profile verification [root@node-130 nginx]# nginx -s reload #If you use the command line, you need to add the - I parameter [root@node-130 nginx]# curl -I www.liangjiawei.net HTTP/1.1 301 Moved Permanently Server: nginx/1.17.10 Date: Mon, 21 Jun 2021 09:02:49 GMT Content-Type: text/html Content-Length: 170 Connection: keep-alive Location: http://blog.liangjiawei.net/
Browser authentication
Scheme 4:
Realize the jump effect of Alibaba cloud:
When accessing mirrors aliyum. Com will automatically jump to developer aliyun. com/mirror/
Use BBS here liangjiawei. Net
#Modify BBS liangjiawei. Net configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/bbs.liangjiawei.net.conf server { listen 80; server_name bbs.liangjiawei.net; location / { if ( $host ~* (.*)\.(.*)\.(.*) ) { set $vhost_name $1; } rewrite ^~/(.*) http://blog.liangjiawei.net/$vhost_name/$1; root /data/html/bbs/; index index.html index.htm; } } #Reload profile [root@node-130 nginx]# nginx -s reload
Realize the jump experiment of different terminals
A website, some users access it by mobile phone,
- Arrange a website for mobile access
- Computer access is also arranged on a website
- It mainly uses $http_user_agent to judge this
This experiment is to realize that different terminal access has different results;
#Create a Shouji liangjiawei. Net server [root@node-130 nginx]# vim /data/nginx/conf/vhost/shouji.liangjiawei.net.conf server { listen 80; server_name shouji.liangjiawei.net; location / { root /data/html/shouji; index index.html index.htm; } } #Create home page file [root@node-130 nginx]# mkdir /data/html/shouji [root@node-130 nginx]# echo "you are visiting the mobile phone page" > / data / HTML / Shouji / index html #Test it [root@node1 nginx]# curl shouji.liangjiawei.net You are visiting the page of your mobile phone #Modify the bbs configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/bbs.liangjiawei.net.conf server { listen 80; server_name bbs.liangjiawei.net; location / { root /data/html/bbs/; index index.html index.htm; if ( $http_user_agent ~* "iphone|android" ) { #This is mainly used to judge the user's browser rewrite ^/(.*)$ http://shouji.liangjiawei.net/$1; } } } #Reload nginx configuration file [root@node-130 nginx]# nginx -s reload
Normal browser access
- Visit the bbs home page
If mobile phone is selected
- browser
- Press F12 – > call up – > select network – > Ctrl + Shift + m – > select language
Language jump experiment of different browsers
- nginx will judge what resources to provide users according to different browser languages
- It mainly involves * * $http_accept_language**
#Modify BBS liangjiawei. Net configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/bbs.liangjiawei.net.conf server { listen 80; server_name bbs.liangjiawei.net; location / { if ( $http_accept_language ~ "^zh-CN" ) { rewrite ^/(.*) /zh/$1; } if ( $http_accept_language ~ "^en" ) { rewrite ^/(.*) /en/$1; } root /data/html/bbs; index index.html index.htm; } location ^~ /zh/ { root /data/html/bbs/; index index.html index.htm; } location ^~ /en/ { root /data/html/bbs/; index index.html index.htm; } } #Prepare Chinese and English content documents [root@node-130 nginx]# mkdir -p /data/html/bbs/{zh,en} [root@node-130 nginx]# echo "this is a Chinese page" > / data / HTML / BBS / zh / index html [root@node-130 nginx]# echo "this is enenenenen " > /data/html/bbs/en/index.html #Reload profile validation [root@node-130 nginx]# nginx -s reload
The error page jumps to the first page
- Some browsers hijack directly and jump to its page
- Note a status code here; two hundred
- If the resource is not found, return to the home page directly
#Modify blog liangjiawei. Net configuration file [root@node-130 nginx]# vim /data/nginx/conf/vhost/blog.liangjiawei.net.conf server { listen 80; server_name blog.liangjiawei.net; location / { root /data/html/blog/; index index.html index.htm; } error_page 404 http://blog.liangjiawei.net/index.html;# Add this line } #Verify after reloading the configuration file [root@node-130 nginx]# nginx -s reload
Browser authentication