location和rewrite规则的使用
1.location规则
- 概念:我们可以通过配置location指令块来决定哪个客户端发过来的请求URL如何处理
- 语法:
Syntax: location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
Default: —
Context: server, location
location配置可以在server和location指令块中配置。
=:精确匹配(必须全部相等)
~:大小写敏感(正则表达式)
~* :忽略大小写(正则表达式),这里要注意忽略大小写的意思是请求的字符大小写都可以,但是不会进行大小转换,请求的大小写对应的文件必须存在。
^~ :只需匹配uri部分
@ :内部服务跳转
- 配置实例
(1)在这里为了不每次更改index.html文件来测试试验效果,特此引用return来简化
[root@localhost ~]# curl 192.168.202.132/jf.jpg
this is /jf.jpg
因为此时只有一个匹配规则,所以输出此匹配规则的输出值
(2)增加匹配规则 “~”
[root@localhost ~]# curl 192.168.202.132/jf.jpg
this is ~ \.(jpg|png)
此时可以看到直接跳过第一个规则匹配到第二个规则
(3)继续添加规则 “~*”
[root@localhost ~]# curl 192.168.202.132/jf.jpg
this is ~ \.(jpg|png)
[root@localhost ~]# curl 192.168.202.132/jf.JPG
this is ~* \.(jpg|png)
以上两种不同方式访问匹配到不同规则
(4)继续添加规则 “^~”
[root@localhost ~]# curl 192.168.202.132/image/jf.jpg
this is ^~ /image/jf.jpg
以上直接跳过前面三个规则匹配第四个规则
(5)继续添加规则 “=”
[root@localhost ~]# curl 192.168.202.132/image/jf.jpg
this is = /image/jf.jpg
直接匹配到第五个规则
- 优先级
可以按照此文件配置来测试
如果客户端请求是 curl 192.168.202.132/image/jf.jpg
那么按照匹配规则顺序应该是这样的:
第一步:取出uri:/image/jf.jpg
第二步:去匹配localtion规则,查找有没有 = /image/jf.jpg 的规则,有则停止匹配
第三步:将location = /image/jf.jpg 规则注释,继续查找有没有 ^~ /image/ 的规则
第四步:将 location ^~ /image/注释,这是它会去查找有没有正则匹配规则
第五步:其他的都注释后,因为优先匹配规则都没有找到,最后匹配到 /image/规则
注意:其中,第二个和的第三个规则都是正则,这时会按照至上而下的顺序匹配
2.rewrite规则
- 语法:
rewrite regex replacement [flag];
正则 替代内容 flag标记
正则:perl兼容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement
flag标记:rewrite支持的flag标记
flag标记说明:
last #本条规则匹配完成终止当前location的规则,继续向下匹配新的location URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址,关闭服务,无法重定向。
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,关闭服务,依然可以重定向,清除缓存失效。
- rewrite运用
准备测试主页:
[root@localhost ~]# echo "this is test page" > /usr/local/nginx/html/test.html
[root@localhost ~]# echo "this is test1 page" > /usr/local/nginx/html/test1.html
[root@localhost ~]# echo "this is test2 page" > /usr/local/nginx/html/test2.html
[root@localhost ~]# echo "this is test3 page" > /usr/local/nginx/html/test3.html
(1)没加rewrite规则访问主页
[root@localhost ~]# curl 192.168.202.132
test access page
(2)配置文件添加重定向
[root@localhost ~]# curl 192.168.202.132
this is test page
此时已经重定向到test.html,所以输出为其内容
(3)配置文件继续添加重定向
[root@localhost ~]# curl 192.168.202.132
this is test1 page
由上可知重定向在匹配到的第一个location规则里重定向,不能跨location。
(4)添加last标记
[root@localhost ~]# curl 192.168.202.132
this is test2 page
此时是匹配到第一个location的第一个重定向后遇到last标记直接跳到下一个location。
(5)添加break标记
[root@localhost ~]# curl 192.168.202.132
this is test page
此时定位到第一个rewrite后,因为break的作用,既不会在同一个location继续重定向下去,更不会跳转到下一个location的重定向规则中,匹配到此为止。
redirect和permanent适用于外部跳转,即域名跳转。当然也可以用在内部跳转但没必要。这两者的区别在于临时重定向和永久重定向。
(6)域名跳转配置
方案一:
server {
listen 80;
server_name test.com;
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
server {
listen 80;
server_name www.test.com;
location / {
root /data/www/;
index index.html index.htm;
}
}
方案二:
server {
listen 80;
server_name test.com;
return 302 http://www.test.com$request_uri ;
}
server {
listen 80;
server_name www.test.com;
location / {
root /data/www/;
index index.html index.htm;
}
}
方案三:
server {
listen 80;
server_name www.test.com test.com;
location / {
if ( $host != 'www.linux.com' ) {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
root /data/www/;
index index.html index.htm;
}
}