上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础。掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足实际的需求场景,例如请求url重写、重定向等等,这都需要对请求的
path进行修改操作的,匹配规则是不能独自完成实际需求的,这就需要掌握nginx的另一个常用功能rewrite,下面就来说说这个常用功能。

Rewrite规则

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。
rewrite只能放在server{}, location{}, if{} 中,并且只能对域名后边传递的参数外的字符串起作用,例如
http://baidu.com/a/we/index.php?id=1&u=str
<http://baidu.com/a/we/index.php?id=1&u=str> 只对/a/we/index.php重写。语法:

rewrite regex replacement [flag];

如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用proxy_pass反向代理。


表面上看rewrite和location功能有点像,都能实现跳转,主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。很多情况下rewrite也会写在location里,它们的执行顺序是:

* 执行server块的rewrite指令
* 执行location匹配
* 执行选定的location中的rewrite指令
如果其中某步URI被重写,则重新循环执行1-3,直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误。

2.1 flag标志位

* last : 停止执行当前ngx_http_rewrite_module的指令集,但是会继续走一遍请求匹配对应server或者location;
* break : 停止执行当前ngx_http_rewrite_module的指令集,请求就此完成。
* redirect : 返回302临时重定向,地址栏会显示跳转后的地址
* permanent : 返回301永久重定向,地址栏会显示跳转后的地址
因为301和302不能简单的只返回状态码,还必须有重定向的URL,这就是return指令无法返回301,302的原因了。

对于上面的flag,有几点需要强调一下:

* last与break对url的重写不会改变地址栏的地址
也就是说,nginx虽然对请求url进行了重写,但是地址栏不会有任何明显的改变,仍然显示nginx重写前的地址;这与redirect和permanent不同。

* last与break的处理策略不同
二者都会终止当前ngx_http_rewrite_module的指令集的执行,但是 last 立即发起新一轮的 请求匹配 而 break 则不会。

* redirect和permanent会终止后续nginx指令的执行
nginx在rewrite遇到flag是二者时,后续的指令是不会执行的。
server { listen 8080; location = /test { break; return 200 $request_uri;
proxy_pass http://127.0.0.1:8080/other; } location / { return 200 $request_uri;
} }
上面例子中,我们访问 curl 127.0.0.1:8080/test,会发现,return 200 $request_uri语句没有执行,而
proxy_pass指令被执行了。这是因为:

return
指令属于ngx_http_proxy_module模块,它会被break终止掉;而rewrite模块它是ngx_http_proxy_module的指令,不会被
break给中断掉。

2.2 if指令与全局变量

if判断指令
语法为if(condition){...}
,对给定的条件condition进行判断。如果为真,大括号内的rewrite指令将被执行,if条件(conditon)可以是如下任何内容:

* 当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false
* 直接比较变量和内容时,使用=或!=
* ~正则表达式匹配,~*不区分大小写的匹配,!~区分大小写的不匹配
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行

例如:
if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; }
//如果UA包含"MSIE",rewrite请求到/msid/目录下 if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1; } //如果cookie匹配正则,设置变量$id等于正则引用部分 if ($request_method = POST) {
return 405; } //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302 if
($slow) { limit_rate 10k; } //限速,$slow可以通过 set 指令设置 if (!-f $request_filename){
break; proxy_pass http://127.0.0.1; } //如果请求的文件名不存在,则反向代理到localhost
。这里的break也是停止rewrite检查 if ($args ~ post=140){ rewrite ^ http://example.com/
permanent; } //如果query string中包含"post=140",永久重定向到example.com location ~*
\.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.jefflei.com
www.leizhenfang.com; if ($invalid_referer) { return 404; } //防盗链 }
全局变量

下面是可以用作if判断的全局变量:

* $args : #这个变量等于请求行中的参数,同$query_string
* $content_length : 请求头中的Content-length字段。
* $content_type : 请求头中的Content-Type字段。
* $document_root : 当前请求在root指令中指定的值。
* $host : 请求主机头字段,否则为服务器名称。
* $http_user_agent : 客户端agent信息
* $http_cookie : 客户端cookie信息
* $limit_rate : 这个变量可以限制连接速率。
* $request_method : 客户端请求的动作,通常为GET或POST。
* $remote_addr : 客户端的IP地址。
* $remote_port : 客户端的端口。
* $remote_user : 已经经过Auth Basic Module验证的用户名。
* $request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
* $scheme : HTTP方法(如http,https)。
* $server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
* $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
* $server_name : 服务器名称。
* $server_port : 请求到达服务器的端口号。
* $request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
* $uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
* $document_uri : 与$uri相同。
例如:
例:http://localhost:88/test1/test2/test.php $host:localhost $server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php $document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php
2.3 常用正则

* . : 匹配除换行符以外的任意字符
* ? : 重复0次或1次
* + : 重复1次或更多次
* * : 重复0次或更多次
* \d :匹配数字
* ^ : 匹配字符串的开始
* $ : 匹配字符串的结束
* {n} : 重复n次
* {n,} : 重复n次或更多次
* [c] : 匹配单个字符c
* [a-z] : 匹配a-z小写字母的任意一个
小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。

2.4 rewrite实例

例1:
http { # 定义image日志格式 log_format imagelog '[$time_local] ' $image_file ' '
$image_type ' ' $body_bytes_sent ' ' $status; # 开启重写日志 rewrite_log on; server {
root /home/www; location / { # 重写规则信息 error_log logs/rewrite.log notice; #
注意这里要用‘’单引号引起来,避免{} rewrite
'^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4; #
注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行 set $image_file $3; set $image_type $4;
} location /data { # 指定针对图片的日志格式,来分析图片类型和大小 access_log logs/images.log mian;
root /data/images; # 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里 try_files
/$arg_file /image404.html; } location = /image404.html { # 图片不存在返回特定的信息 return
404 "image not found\n"; } }
对形如/images/ef/uh7b3/test.png的请求,重写到/data?file=test.png,于是匹配到location /data,先看
/data/images/test.png文件存不存在,如果存在则正常响应,如果不存在则重写tryfiles到新的image404
location,直接返回404状态码。

例2:
rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$
/resizer/$1.$4?width=$2&height=$3? last;
对形如/images/bla_500x400.jpg的文件请求,重写到/resizer/bla.jpg?width=500&height=400
地址,并会继续尝试匹配location。

例3:
见 ssl部分页面加密 <http://seanlook.com/2015/05/28/nginx-ssl/> 。

2.5 rewrite需要注意的问题

上面说过,rewrite的指令规则为:rewrite regex replacement [flag];


rewrite指令用指定的regex来匹配请求的uri,若匹配成功则用replacement来重写请求uri。这里需要注意的replacement字符串的内容:

1、 若replacement以http://、https://或者$scheme
开头,则告诉nginx这是重定向操作(flag默认为redirect),nginx则停止处理后续内容,并直接重定向返回给客户端。
location / { # 当匹配 正则表达式 /test/(.*)时 请求将被临时重定向到 http://www.baidu.com/$1 #
flag默认为redirect rewrite /test/(.*) https://www.baidu.com/$1; return 200 ’ok'; #
此处没有机会执行 }
2、replacement非以上三种情况开头,则就是简单的url重写
location / { # 当匹配 正则表达式 /test/(.*)时 请求将被临时重定向到 www.baidu.com/$1 #
flag无值则rewrite会顺序执行 rewrite /test/(.*) www.baidu.com/$1; return 200 ’ok'; #
此处因为rewrite顺序执行而得到执行机会 }
对于上面两种情况,还需要特别留意一个redirect端口的问题,先上一个例子:
## server.com机器上nginx的配置如下: server { listen 8000; location /test1/ { rewrite
/test1/index.html http://server1.com/demo/test1 redirect; } location /test2/ {
rewrite /test2/index.html /demo/test2 redirect; proxy_pass
http://192.168.1.3:8000; } }
当访问http://server.com/test1/index.html <http://server.com/test1/index.html>
时,会命中/test1的location规则,访问server1.com对应内容一直失败,发现重定向后响应头的Location字段值为
http://server1.com:8000/demo/test1 <http://server1.com:8000/demo/test1>
,带有8000端口,我们并没有配置,表现的比较诡异?

访问http://server.com/test2/index.html <http://server.com/test2/index.html>
时,命中/test2的location规则,同样访问失败,但是访问的重定向后响应头Location字段值为
http://server.com:8000/demo/test2 <http://server.com:8000/demo/test2>
,其带有server.com的server_name和8000的端口,更加诡异?

看到上面的现象,疑惑重重;其实这跟nginx的server_name_in_redirect
<http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name_in_redirect>
和port_in_redirect
<http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect>指令有关:

在绝对路径中,server_name_in_redirect 和port_in_redirect 指令表示是否将server块中的 server_name
和 listen 的端口作为redirect用, 重定向的完整url地址根据$scheme跟server_name_in_redirect和
port_in_redirect来确定的。

在绝对路径中,server_name_in_redirect默认是禁用的,而port_in_redirect是默认启用的。对于带有$scheme
重定向的绝对路径,nginx会从replacement中获取指定的server_name和port来进行重定向:


第一种,若replacement带请求协议http(s),而其中没有指定port的话,nginx会默认取当前server的listen端口作为重定向的端口。这是上面访问
http://server.com/test1/index.html <http://server.com/test1/index.html>时重定向到
http://server.com:8000/demo/test2 <http://server.com:8000/demo/test2>
时会携带8000的原因。

第二种,若replacement不带请求协议http(s),而是相对本地服务器的绝对地址的话,如上面访问
http://server.com/test2/index.html <http://server.com/test2/index.html>的情况,此时
server_name_in_redirect
由于禁用它会去请求的host来作为server_name,取当前server的listen端口作为重定向的端口,最终重定向到
http://server.com:8000/demo/test2 <http://server.com:8000/demo/test2>。

或许你会问,访问http://server.com/test2/index.html <http://server.com/test2/index.html>
为什么不会重定向到http://192.168.1.3:8000/demo/test2 <http://192.168.1.3:8000/demo/test2>
上?这是因为rewrite的redirect flag会终止后续指令的执行,所以其后的proxy_pass指令不会执行。

参考

* http://www.nginx.cn/216.html <http://www.nginx.cn/216.html>
* http://www.ttlsa.com/nginx/nginx-rewriting-rules-guide/
<http://www.ttlsa.com/nginx/nginx-rewriting-rules-guide/>
* https://segmentfault.com/a/1190000008102599
<https://segmentfault.com/a/1190000008102599>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信