6. Nginx rewrite 配置(一)
nginx的rewrite配置是nginx配置中比较核心的部分,rewrite可以实现域名跳转(重定向)、URL重写(伪静态)、动静分离(跳转域名,并接入CDN实现加速)。rewrite依赖pcre库,用到的模块是ngx_http_rewrite_module。
rewrite相关指令
if指令
格式: if (条件判断) { 具体的rewrite规则 }
- 条件举例:
条件判断语句有nginx内置变量、逻辑判断符号和目标字符串三部分组成。
其中,内置变量是nginx固定的非自定义的变量,如$request_method、$request_uri等。
逻辑判断符号有 =、!=、~、~*、!~、!~*。
!表示取反,~为匹配符号,它右侧为正则表达式,区分大小写,而~*为不区分大小写匹配。
目标字符串可以是正则表达式,通常不用加引号,但表达式中有特殊符号时,比如空格、花括号、分号等,需要用单引号引起来。
- 示例1:
if ($request_method = POST)
{
return 405;
}
当请求的方法为POST时,直接返回405状态码。if中支持用return指令。
- 示例2:
if ($http_user_agent ~ MSIE )
{
return 403;
}
user_agent带有MSIE(IE浏览器)字符的请求,直接返回403状态码。
如果想同时限制多个user_agent,还可以写成这样:
if ($http_user_agent ~ "MSIE|firefox|spider")
{
return 403;
}
- 示例3:
if (!-f $request_filename)
{
rewrite 语句;
}
当请求的文件不存在时,将会执行下面的rewrite规则。
- 示例4:
if ($request_uri ~* 'gid=\d{9,12}/')
{
rewrite 语句;
}
\d表示数字,{9,12}表示数字出现的次数是9到12次,比如gid=123456789是符合条件的,就会执行下面的rewrite规则。
break和last指令
两个指令用法相同,但含义不同,需要放到rewrite规则的末尾,用来控制重写后的链接是否继续被nginx配置执行(主要是rewrite、return指令)。
- 示例1:
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on; #打开rewrite日志,在error.log中
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
重载配置:
# echo "111111" > /data/wwwroot/www.1.com/2.html
# echo "222222" > /data/wwwroot/www.1.com/2.html
# echo "333333" > /data/wwwroot/www.1.com/3.html
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问测试:
# curl -x127.0.0.1:80 www.1.com/1.html
333333
说明已经从1.html跳转到3.html,实际访问到的是3.html。
查看日志:
# tail /usr/local/nginx/logs/error.log
2019/03/11 17:51:27 [notice] 28386#0: *1 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 17:51:27 [notice] 28386#0: *1 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 17:51:27 [notice] 28386#0: *1 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 17:51:27 [notice] 28386#0: *1 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
- 示例2:
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
重载配置:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问测试:
# curl -x127.0.0.1:80 www.1.com/1.html
222222
说明这一次是从1.html跳转到2.html,没有继续往下面跳转。
查看日志:
# tail /usr/local/nginx/logs/error.log
2019/03/11 18:02:18 [notice] 28507#0: *2 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:02:18 [notice] 28507#0: *2 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
- 示例3:
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
重载配置:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问测试:
# curl -x127.0.0.1:80 www.1.com/1.html
222222
查看日志:
# tail /usr/local/nginx/logs/error.log
2019/03/11 18:08:21 [notice] 28533#0: *3 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:08:21 [notice] 28533#0: *3 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
说明这一次也是从1.html跳转到2.html,没有继续往下面跳转。在server部分配置break和last作用一致。
- 示例4:
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
重载配置:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问测试:
# curl -x127.0.0.1:80 www.1.com/1.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
查看日志:
# tail /usr/local/nginx/logs/error.log
2019/03/11 18:18:11 [notice] 6932#0: signal 17 (SIGCHLD) received from 28533
2019/03/11 18:18:27 [notice] 28558#0: *4 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 "/3.html" matches "/3.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 rewritten data: "/b.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 "/1.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [notice] 28558#0: *4 "/2.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:18:27 [error] 28558#0: *4 open() "/data/wwwroot/www.1.com/b.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
首先匹配到1.html,1.html跳转到2.html;再匹配到2.html,2.html又跳转到3.html;接下来匹配到3.html,3.html跳转到b.html;b.html还会继续匹配,但没有匹配到,所以访问b.html,因为b.html不存在,所以返回404状态码。
- 示例5:
如果我们在server部分使用了location,那break和last的作用就有区别了。
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
重载配置:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问测试:
# curl -x127.0.0.1:80 www.1.com/1.html
222222
查看日志:
# tail /usr/local/nginx/logs/error.log
2019/03/11 18:32:55 [notice] 6750#0: *5 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:32:55 [notice] 6750#0: *5 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
可以看到,只rewrite一次,从1.html跳转到2.html就直接退出,后面的location部分也不再执行了。
- 示例6:
如果我们在server部分使用了location,那break和last的作用就有区别了。
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
重载配置:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
访问测试:
# curl -x127.0.0.1:80 www.1.com/1.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
查看日志:
# tail /usr/local/nginx/logs/error.log
2019/03/11 18:38:57 [notice] 6759#0: *6 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:38:57 [notice] 6759#0: *6 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:38:57 [notice] 6759#0: *6 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:38:57 [notice] 6759#0: *6 rewritten data: "/a.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:38:57 [notice] 6759#0: *6 "/1.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:38:57 [notice] 6759#0: *6 "/2.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/03/11 18:38:57 [error] 6759#0: *6 open() "/data/wwwroot/www.1.com/a.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
先从1.html跳转到2.html,因为有last,所以本location段内的下面的内容不再执行,但是会继续执行下面的location段,最后匹配到2.html(因为比 / 更精准),从2.html跳转到a.html,因为a.html不存在,所以返回404状态码。
综上,我们可以得到结论:
* 当rewrite规则在location{}外,break和last作用一样,遇到break或last后,其后续的rewrite/return语句不再执行。但后续有location{}的话,还会近一步执行location{}里面的语句,当然前提是请求必须要匹配该location。
* 当rewrite规则在location{}里,遇到break后,本location{}与其他location{}的所有rewrite/return规则都不再执行。
* 当rewrite规则在location{}里,遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。
return用法
return指令一般用于对请求的客户端直接返回响应状态码。在该作用域内return后面的所有nginx配置都是无效的。可以使用在server、location以及if配置中。
除了支持跟状态码,还可以跟字符串和url链接。
返回状态码
- 示例1:
server {
listen 80;
server_name www.1.com;
return 403;
rewrite /(.*) /abc/$1; #该行配置不会被执行
}
.*
表示所有,$1表示前面的
.*
# vim /usr/local/nginx/conf/vhost/default.conf
server {
listen 80 default_server;
return 403;
rewrite /(.*) /abc/$1;
}
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 e2rwejqw.com
<html>
<head><title>403 Forbidden</title></head> #返回403
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
- 示例2:
server {
......
if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 405;
rewrite /(.*) /aaa.txt; #该行配置不会被执行
}
#如果下面还有其他配置,会被执行
......
}
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 405;
rewrite /(.*) /aaa.txt;
}
}
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I
HTTP/1.1 405 Not Allowed #返回405
Server: nginx
Date: Mon, 11 Mar 2019 08:20:55 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive
返回字符串
- 示例3:
server {
listen 80;
server_name www.1.com;
return 200 "hello";
}
如果想返回字符串,必须加上状态码,否则会报错。
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 200 "error";
rewrite /(.*) /aaa.txt;
}
}
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Mar 2019 08:26:58 GMT
Content-Type: application/octet-stream
Content-Length: 5
Connection: keep-alive
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd
error
另外还可以支持JSON数据;支持写一个变量;支持html代码。
- 场景实战:
背景:网站被黑,凡是在百度点击到本网站的请求,全部都跳转到一个赌博网站。
通过nginx解决:
server {
......
if ( $http_referer ~ 'baidu.com' ) {
return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>";
}
}
如果写成:
return http://$host$reauest_uri;
,这在浏览器中会提示“重定向的次数过多”。
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>";
rewrite /(.*) /aaa.txt;
}
}
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Mar 2019 08:54:17 GMT
Content-Type: application/octet-stream
Content-Length: 79
Connection: keep-alive
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd
<html><script>window.location.href='//www.1.com/123/.htpasswd';</script></html> #返回的就是域名和链接
返回url
- 示例4:
server {
listen 80;
server_name www.1.com;
return http://www.baidu.com;
rewrite /(.*) /abc/$1; #该行配置不会被执行
}
注意:return后面的url必须是以
http://
或者
https://
开头的。
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return http://www.baidu.com;
rewrite /(.*) /abc/$1;
}
}
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 11 Mar 2019 08:44:07 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://www.baidu.com #临时重定向到www.baidu.com
url前面也可以加状态码,但只能是301或302,如果是200,这url会变成字符串返回。
# vim /usr/local/nginx/conf/vhost/www.1.com.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite_log on;
if ( $request_uri ~ "\.htpasswd|\.bak" ) {
return 200 http://www.baidu.com;
rewrite /(.*) /abc/$1;
}
}
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd -I
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 11 Mar 2019 09:02:15 GMT
Content-Type: application/octet-stream
Content-Length: 20
Connection: keep-alive
# curl -x127.0.0.1:80 www.1.com/123/.htpasswd
http://www.baidu.com