首页 > 代码库 > Nginx自学手册(三)location匹配,rewrite
Nginx自学手册(三)location匹配,rewrite
(一)Nginx location
location语法规则
Syntax: | location [ location |
---|---|
Default: | — |
Context: | server , location |
nginx官方的例子:
location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] }
在上述配置中,当用户请求“/”时,将匹配configuration A,当用户请求“/index.html”时,将匹配configuration B,当用户请求“/documents/document.html”时,将匹配configuration C,当用户请求“/images/1.gif”时,将匹配configuration D;当用户请求“/documents/1.jpg”时,将匹配configuration E。
1,自己创建实例为:
[root@Monitor xn3]# mkdir documents [root@Monitor xn3]# mkdir images [root@Monitor xn3]# vim documents/index.html this is documents html [root@Monitor xn3]# vim images/index.html this is images page [root@Monitor xn3]# vim index.html The path is : = / [root@Monitor xn3]# vim index1.html The path is / [root@Monitor xn3]# vim 1.jpg this is 1.jpg [root@Monitor xn3]# vim 1.gif this is 1.gjf [root@Monitor conf]# vim server/server.conf server { listen 80; server_name xn3.lqb.com; root /html/xn3; location = / { ###精确匹配 / ,主机名后面不能带任何字符串 index index.html; } location / { ###因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求,但是正则表达式与一些较长的字符串将被优先匹配 index index1.html; } location /documents/ { ###匹配/documents/开头的地址,匹配符合以后,还要继续往下搜索,只有后面的正则表达式没有匹配到时,这一条才会采用这一条 index index.html; } location ^~ /images/ { ###匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。 index index.html; } location ~* \.(jpg|gif|png)$ { ###匹配所有以 gif,jpg或jpeg 结尾的请求,但是所有 /images/ 目录的请求将响应/images/请求 index index.html; } }
2,nginx服务器重启下服务
[root@Monitor conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@Monitor conf]# /usr/local/nginx/sbin/nginx -s reload
3,在客户端进行测试如下:
[root@localhost ~]# curl xn3.lqb.com The path is : = / [root@localhost ~]# curl xn3.lqb.com/index1.html The path is / [root@localhost ~]# curl xn3.lqb.com/documents/index.html this is documents html [root@localhost ~]# curl xn3.lqb.com/images/ this is images page [root@localhost ~]# curl xn3.lqb.com/1.gif this is 1.gjf [root@localhost ~]# curl xn3.lqb.com/1.jpg this is 1.jpg
4,nginx服务器日志如下:
192.168.180.23 - - [02/Aug/2017:14:50:36 +0800] "GET / HTTP/1.1" 200 18 "-" "curl/7.29.0" 192.168.180.23 - - [02/Aug/2017:14:50:47 +0800] "GET /index1.html HTTP/1.1" 200 14 "-" "curl/7.29.0" 192.168.180.23 - - [02/Aug/2017:14:51:02 +0800] "GET /documents/index.html HTTP/1.1" 200 23 "-" "curl/7.29.0" 192.168.180.23 - - [02/Aug/2017:14:51:16 +0800] "GET /images/ HTTP/1.1" 200 20 "-" "curl/7.29.0" 192.168.180.23 - - [02/Aug/2017:14:51:26 +0800] "GET /1.gif HTTP/1.1" 200 14 "-" "curl/7.29.0" 192.168.180.23 - - [02/Aug/2017:14:51:33 +0800] "GET /1.jpg HTTP/1.1" 200 14 "-" "curl/7.29.0"
5,总结:
(location =) >(location ^~ 路径) >(location ~* 正则) >(location 路径)
查找顺序和优先级
1:带有“=“的精确匹配优先
2:带有“^~”修饰符的,开头匹配
3:带有“~” 或“~*” 修饰符的,如果正则表达式与URI匹配
4:没有修饰符的,如果指定字符串与URI开头匹配
备注:
=:精确匹配
~:区分大小写
~*:不区分大小写
^~:禁止表达式匹配
具体参考官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
(二)Nginx rewrite
rewrite主要是实现URL地址的重定向去掉恶意的URL优化搜索引擎,这个模块在编译时是默认安装的,同时需要pcre包的支持。
1,语法:
rewrite regex replacement [flag];
rewrite ^/images/(.*\.jpg|jpeg|png)$ /imgs/$1 break; ###$1前面*的引用
解释:
regex:正则表达式
replacement:通过regex匹配到的请求重定向到replacement
^ :必须以^后的实体开头
$ :必须以$前的实体结尾
. :匹配任意字符
[ ] :匹配指定字符集内的任意字符
[^ ] :匹配任何不包括在指定字符集内的任意字符串
| :匹配 | 之前或之后的实体
() :分组,组成一组用于匹配的实体,通常会有|来协助
flag:标记符号:
last:本条规则匹配完成后,继续向下匹配;
break:中止Rewirte,不在继续匹配;
redirect:返回临时重定向的HTTP状态302;
permanent:返回永久重定向的HTTP状态301;
2,详细的配置信息
server { listen 80; server_name xn2.lqb.com; root /html/xn2/; location / { index index.html; } error_page 404 /404.html; } server { listen 80; server_name xn3.lqb.com; root /html/xn3; location = / { rewrite ^/(.*) xn2.lqb.com permanent; index index.html; } error_page 500 502 503 504 /50x.html; location =/50x.html{ root /html/xn3; } }
本文出自 “清风明月” 博客,请务必保留此出处http://liqingbiao.blog.51cto.com/3044896/1953067
Nginx自学手册(三)location匹配,rewrite