Example of a method for Nginx rewrite regular matching rewrite
- 2020-05-24 06:47:32
- OfStack
The rewrite feature of Nginx supports regular matching rewriting, which temporarily or permanently redirects the URL address to a new location, similar to redirection. This feature is used when the site structure to make major changes, such as web site before use URL for www mp3 resources. site1. org/mp3 visit, now mp3 directory is already in use on the server music directory to replace, the rewrite can easily implement this feature. Secondly, if site1.org can be forced to adjust to www.site1.org, or vice versa. This directive is located in the ngx_http_rewrite_module module. This article describes and demonstrates the use of this instruction.
1. Grammar description of rewrite instruction
Syntax: rewrite regex replacement [flag];
Default: -
Context: server, location, if
If the specified regular expression matches the request URI, URI changes as specified in the replacement string.
The rewrite directive is executed in the order in which it appears in the configuration file. You can use the flag to terminate the step 1 processing of the directive.
If the replacement string begins with "http://", "https://" or "$scheme", the processing stops and the redirect is returned to the client.
The flag flag is used to control whether to continue checking the subsequent rewrite rules when the corresponding rewrite rules are matched
The optional flag parameter can be 1 of the following:
last
1. When the current rule is matched and overwritten, it immediately stops checking the subsequent rewrite rules, and then re-initiates the request through the rewritten rule;
break
1. Stop the subsequent rewrite rules immediately after they are matched and overwritten by the current rule, and then continue the subsequent operation by nginx;
redirect
If the replacement string does not start with "http://", "https://" or "$scheme", then use, return 302 temporary redirection;
permanent
Return 301 permanent redirect;
Note: 1 generally use the break flag when writing rewrite in location, or rewrite in the context of if;
Other instructions
rewrite_log on|off
Whether to log the rewrite process in an error log; The default is notice level; The default is off;
return code:
To end the rewrite rule and return a status code for the customer; The available status codes are 204, 400, 402-406, 500-504, etc.
2. rewrite function demonstration based on location context
The native environment
# more /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
# nginx -v
nginx version: nginx/1.12.2
configuration nginx
# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name site1.orag www.site1.org;
location / {
root /www/site1.org;
index index.html index.htm;
}
}
# mkdir -pv /www/site1.org/images
# echo "This is a rewrite test page.">/www/site1.org/index.html
# cp /usr/share/backgrounds/gnome/*.jpg /www/site1.org/images/
# vim /etc/hosts
192.168.1.175 site1.org
192.168.1.175 www.site1.org
# curl http://www.site1.org
This is a rewrite test page.
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 03:47:58 GMT
Content-Type: image/jpeg
Content-Length: 458818
Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT
Connection: keep-alive
ETag: "59f942f4-70042"
Accept-Ranges: bytes
Modify the rewrite.conf File, add rewrite instruction
location / {
root /www/site1.org;
index index.html index.htm;
rewrite ^/images/(.*)$ /imgs/$1 last;
}
# systemctl reload nginx
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 04:02:38 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
# mkdir -pv /www/site1.org/imgs
# mv /www/site1.org/images/Waves.jpg /www/site1.org/imgs/.
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 04:05:07 GMT
Content-Type: image/jpeg
Content-Length: 458818
Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT
Connection: keep-alive
ETag: "59f942f4-70042"
Accept-Ranges: bytes
# curl -I http://www.site1.org/imgs/Waves.jpg ## It's accessible in this way
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 04:06:17 GMT
Content-Type: image/jpeg
Content-Length: 458818
Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT
Connection: keep-alive
ETag: "59f942f4-70042"
Accept-Ranges: bytes
simulation rewrite As a result of http 500 error
Once again to rewrite.conf The file is modified as follows,
location / {
root /www/site1.org;
index index.html index.htm;
rewrite ^/images/(.*)$ /imgs/$1 last;
rewrite ^/imgs/(.*)$ /images/$1 ;
}
# systemctl restart nginx
# curl -I http://www.site1.org/imgs/Waves.jpg
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 05:23:16 GMT
Content-Type: text/html
Content-Length: 193
Connection: close
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 05:23:28 GMT
Content-Type: text/html
Content-Length: 193
Connection: close
According to the above test, there is a dead loop 500 Error.
Nginx Official reference sample:
server {
... ##rewrite Instructions in the server context
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
## will /download The directory contains media Any file request in the directory is redirected to donwload/ any /mp3/ any .mp3
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
## will /download The directory contains audio Any file request in the directory is redirected to donwload/ any /mp3/ any .mp3
return 403;
...
}
location /download/ { ##rewrite Instructions in the location context
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
## the last The logo should be replaced break , otherwise, nginx Will make the 10 Return in cycles 500 A mistake
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
return 403;
}
3. Judge rewrite function demonstration based on if condition
# vi /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name site1.orag www.site1.org;
if ($host != 'www.site1.org' ) {
rewrite ^/(.*)$ http://www.site1.org/$1 permanent;
}
location / { ##Author : Leshami
root /www/site1.org; ##Blog : http://blog.csdn.net/leshami
index index.html index.htm;
rewrite ^/images/(.*)$ /imgs/$1 last;
rewrite ^/imgs/(.*)$ /images/$1 ;
}
}
# systemctl reload nginx.service
Local test ( Modify the local host file )
# curl http://site1.org
<html> ## return 301 Status code
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
Windows Environmental test
By modifying the Windows The machine Host After the file, add the following entry
192.168.1.175 centos7-router.com
192.168.1.175 www.centos7-router.com
Open a browser and access the domain name http://site1.org It will automatically jump to http://www.site1.org( Demonstrate a )
4. Rewrite http to https
When https is a partial station, https is required for some sensitive data, which can also be realized through rewrite
The following example, assume that https: / / www site1. org/user directory containing sensitive information, according to the rewrite can press the following way
location ^~ /user {
rewrite ^/ https://www.site1.org$request_uri? permanent;
}
Total station https
server {
listen 80;
server_name site1.orag www.site1.org;
access_log /var/log/nginx/http-access.log;
error_log /var/log/nginx/http-error.log;
rewrite ^/ https://www.site1.org$request_uri;
}
The above demonstration is abbreviated