table of Contents
Nginx Rewrite (redirect)
1. What is rewrite
Rewrite and URL rewriting are mainly to achieve address rewriting and redirection, which is the process of redirecting the request input to the Web to other URLs.
2. Rewrite usage scenarios
- URL address redirection, include users visiting old.com and redirect them to oldboy.com, or when users visit old.com via http, redirect them to https to visit oldboy.com.
- URL pseudo-static, a technology for displaying dynamic pages as static pages, which facilitates the entry of search engines, while reducing excessive external exposure of dynamic URL addresses to improve security.
- Search engine SEO optimization relies on URL paths in order to support search engine entry.
- The URL that the user browses can be adjusted, which looks more standardized and meets the requirements of developers and product personnel.
3. Rewrite configuration syntax
Example
Syntax:rewrite regex replacement [flag];
Default:–
Context:server,location,if
4. Commonly used regular expressions
character | description |
---|---|
\ | Mark the following character as a special character or a literal character or a backquote |
^ | Match the starting position of the input string |
$ | Match the end position of the input string |
* | Match the preceding character zero or more times |
+ | Match the preceding string one or more times |
? | Matches the preceding string zero or one time |
. | Match all single characters except "\n" |
(pattern) | Match the pattern in parentheses |
5. Rewrite Flag
The rewrite instruction redirects rul based on the expression, or modifies the string. It can be applied to server, location, and if environments. Each rewrite command is followed by a flag at the end. The supported flags are shown in the following table:
Mark symbol | Description |
---|---|
last | After this rule is matched, continue to match the new location URI rule downward |
break | This rule is terminated after the match is completed, and no rule is matched |
redirect | Return 302 temporary redirect |
permanent | Return 301 permanent redirect |
Last and break comparison summary
last
- After the last Rewrite match is successful, stop the current request, and re-initiate a request to the Server according to the Rewrite matching rules.
- The content of the new request is the domain name + URL, and the corresponding address is www.lodboy.com/2017_old
break
- After the break is successfully matched by Rewrite, it will not re-initiate the request like last. First, find out whether the default return page of /clod/2017_old/ in the site directory exists, if it does not exist, it will be 404, and if there is, continue to match.
- According to the matching rules of Rewrite, jump to the URL address of www.oldboy.com/2017_old/. After the matching is successful, the matching will not continue.
6. Quote Nginx's global variables
列:http://localhost:88/test1/test2/test.php
- $host:localhost
- $server_port: 88
- $request_uri:/test1/test2/test.php
- $ document_uri: /test1/test2/test.php
- $document_root: /var/www/html
- $request_filename: /var/www/html/test1/test2/test.php
7. Rewrite matching priority
- 1. Execute the rewrite instruction of the server block
- 2. Perform location matching
- 3. Perform rewrite in the selected location
8. Turn on Nginx's Rewrite log function
vim /etc/nginx/nginx.conf
#1. Set the error log level of nginx to notice
error_log /var/log/nginx/error.log notice;
#2. At the http module layer, add a line of rewrite on to enable the log function
http {
rewrite_log on;
server {
.......
}
}
9. Case
Case number one:
The page jumps to www.baidu.com
server {
listen 80;
server_name yan.wordpress.com;
charset utf-8;
root /html/wordpress;
index index.php index.html;
location / {
rewrite ^/ https://www.baidu.com; //正则匹配 到www.baidu.com
}
location ~ \.php$ {
root /html/wordpress;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_index index.php;
}
}
Case 2:
The user visits /abc/1.html actually visits ccc/bbb/2.html
http://rewrite.odboy.com/abc/1.html => http://rewrite.odboy.com/ccc/bbb/2.html
[root@web02 html]# mkdir -p /ccc/bbb
[root@web02 html]# echo "bbbbbcccccc" > /ccc/bbb/2.html
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
root /html;
index index.html;
location ~* /abc/1.html {
rewrite /abc/1.html /ccc/bbb/2.html;
}
}
Configuration optimization one:
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
root /html;
index index.html;
location ~* /abc/1.html {
rewrite .* /ccc/bbb/2.html;
}
}
Configuration optimization two:
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
root /html;
index index.html;
location ~* /abc/ {
//这两行都是302跳转,不要一起配置。
#rewrite .* /ccc/bbb/2.html redirect;
return 302 /ccc/bbb/2.html;
}
}
Case Three:
User visits http://rewrite.oldboy.com/2018/ccc/bbb/2.html ==> http://rewrite.oldboy/2014/ccc/bbb/2.html
[root@web02 html]# mkdir 2014
[root@web02 html]# mv ccc 2014/
[root@web02 html]# chown -R www.www 2014/
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
root /html;
index index.html;
location /2018 {
rewrite ^/2018/(.*)$ /2014/$1 redirect;
}
}
Case Four:
The user visits any content in the /test directory, in fact, the real visit is rewirte.oldboy.com
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
index 2.html;
root /html/ccc/bbb;
location /test {
rewrite (.*) http://rewrite.oldboy.com redirect;
}
}
Case Five:
#http://rewrite.oldboy.com/course-11-22-33-course_33.html ==> http://rewrite.oldboy.com/course/11/22/33/course_33.html
[root@web02 html]# mkdir -p 11/22/33
[root@web02 html]# echo "/11/22/23" > 11/22/33/course_33.html
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
index 2.html;
root /html;
location / {
#灵活
rewrite ^/(.*)-(.*)-(.*).html$ /$1/$2/$3/course_$3.html redirect;
#固定
rewrite ^/(.*) /11/22/33/course_33.html redirect;
}
}
Case Six:
Speaking of http request, jump to https
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.oldboy.com;
rewrite ^(.*) https://$server_name$1 redirect;
#return 302 https://$request_uri;
}
server {
listen 443;
server_name rewrite.oldboy.com;
ssl on
}