nginx配置文件rewrite规则
if
语法:if (condition) {…}
应用场景:
server段
location段
常见的condition
变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
正则表达式的模式匹配操作
~:区分大小写的模式匹配检查
~:不区分大小写的模式匹配检查
!和!:对上面两种测试取反
测试指定路径为文件的可能性(-f,!-f)
测试指定路径为目录的可能性(-d,!-d)
测试文件的存在性(-e,!-e)
检查文件是否有执行权限(-x,!-x)
Rewite 规则介绍
Rewrite规则可以实现对url的重写,以及重定向
作用场景
URL访问跳转,支持开发设计,如页面跳转,兼容性支持,展示效果等
SEO优化
维护:后台维护、流量转发等
安全
注:nginx官方文档:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
配置语法
Syntax: rewrite regex replacement flag;
Default:——
Context:server、location、if
rewrite语法
server {
rewrite {规则} {定向路径} {重写类型} ;
}
1、规则:可以是字符串或者正则来表示想匹配的目标url
2、定向路径:表示匹配到规则后要定向的路径,如果规则里有正则,则可以使用$index来表示正则里的捕获分组
flag标志位
last : 相当于Apache的[L]标记,表示完成rewrite
break : 停止执行当前虚拟主机的后续rewrite指令集
redirect : 返回302临时重定向,地址栏会显示跳转后的地址
permanent : 返回301永久重定向,地址栏会显示跳转后的地址
因为301和302不能简单的只返回状态码,还必须有重定向的URL,这就是return指令无法返回301,302的原因了。这里 last 和 break 区别有点难以理解:
last一般写在server和if中,而break一般使用在location中
last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配
break和last都能组织继续执行后面的rewrite指令
配置rewrite规则
示例:
//先在主页目录下创建一个image的目录。
[root@localhost html]# mkdir images
[root@localhost html]# ls
50x.html images index.html
[root@localhost html]# cd images/
[root@localhost images]# ls
[root@localhost images]# echo "hello kg" >images.html
[root@localhost images]# ls
images.html
[root@localhost images]# pwd
/usr/local/nginx/html/images
//进入配置文件配置rewrite重写规则,匹配images的目录
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /images {
rewrite ^/images/(.*)$ /images/images.html break;
}
//匹配根下的images目录然后,images目录下的任意字符,都转发到/images/images.html。所以此时我们不管怎么访问,只要是在/images/目录下随便输入什么字符都可以匹配到。
[root@localhost images]# systemctl restart nginx.service
[root@localhost images]# curl 192.168.171.142/images/
hello kg
[root@localhost images]# curl 192.168.171.142/images/aaaz
hello kg
[root@localhost images]# curl 192.168.171.142/images/aaaaaaaa
hello kg
//如果这个时候访问的文件发生了改变,比如名字发生了改变如
那么怎么样才能让那个访问方式继续访问到文件的内容。
[root@localhost images]# ls
images.html
[root@localhost images]# mv images.html imgs.html
[root@localhost images]# ls
imgs.html
[root@localhost images]#
//修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /images {
rewrite ^/images/(.*)$ /images/imgs.html break;
}
[root@localhost ~]# !systemctl
systemctl restart nginx.service
//匹配到的images/目录下的任意字符都转发给后面的/images/imgs.html 所以就算文件名字发生了改变照样能完成之前的效果,因为我们在后面转发的位置指向了文件。
[root@localhost ~]# curl 192.168.171.142/images/1314520
hello kg
[root@localhost ~]# curl 192.168.171.142/images/131452000
hello kg
last二次转发
二次转发是利用了last的特性,匹配到了也不会停止匹配会继续转发到下一个rewrite的规则上去,直到转发到的规则里有break,或者匹配失败。
//修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /images {
rewrite ^/images/(.*)$ /images/imgs.html last;
}
location /imgs {
rewrite ^/imgs/(.*)$ http://www.baidu.com break;
}
//上面的location匹配到images目录下的imgs.html文件,然后因为last的原因所以它不会停止,会找到下一个location然后继续匹配,所以这里我写了一个rewrite规则,这里如果上面匹配到以imgs开头以任意字符结尾的就转发到百度。
[root@localhost ~]# !systemctl
systemctl restart nginx.service