last:rewrite匹配后,会再次发起一个请求,只会对location里的规则再次匹配。php
break:rewrite匹配后,不会发起请求,也不会匹配后面的规则。html
举例:dom
location / {url
root html;spa
index index.php index.html index.htm;htm
}blog
if ($request_uri ~* /cms/) {get
rewrite "^/cms/(.*)\.php" /cms/index.php break;it
# 这里使用last也能够,单独的if只处理一次io
# "if"和"("须要空格
}
location / {
root html;
index index.php index.html index.htm;
if ($request_uri ~* /cms/) {
rewrite "^/cms/(.*)\.php" /cms/index.php break;
# 这里使用last会形成死循环,在location里的if会处理屡次
}
}
须要注意url有问号的状况
比较不加"?"和加上"?"标记的URL跳转区别
rewrite ^/test(.*)$ http://www.yourdomain.com/home permanent;
访问http://www.yourdomain.com/test?id=5通过301跳转后的URL是
http://www.yourdomain.com/home?id=5
rewrite ^/test(.*)$ http://yourdomain.com/home? permanent;
访问http://www.yourdomain.com/test?id=5通过301跳转后的URL是
http://www.yourdomain.com/home