Nginx自学手册(三)location匹配,rewrite

简介:

   (一)Nginx location

(一)Nginx Location 

location语法规则

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:
Context: serverlocation

符号

含义

=

开头表示精确匹配

^~

^~开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)

~

开头表示区分大小写的正则匹配

~*

~* 开头表示不区分大小写的正则匹配

!~!~*

!~!~*分别为区分大小写不匹配及不区分大小写不匹配的正则

/

用户所使用的代理(一般为浏览器)

$http_x_forwarded_for

可以记录客户端IP,通过代理服务器来记录客户端的ip地址

$http_referer

可以记录用户是从哪个链接访问过来的

nginx官方的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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,自己创建实例为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[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服务器重启下服务

1
2
3
4
[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,在客户端进行测试如下:

1
2
3
4
5
6
7
8
9
10
11
12
[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服务器日志如下:

1
2
3
4
5
6
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功能就是,使用nginx提供的全局变量或自己设置的变量,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;

* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行


本文转自 lqbyz 51CTO博客,原文链接:http://blog.51cto.com/liqingbiao/1953067


相关文章
|
4月前
|
自然语言处理 前端开发 应用服务中间件
nginx的Location语法规则
nginx的Location语法规则
|
5月前
|
应用服务中间件 nginx Perl
Nginx系列教程(09) - rewrite
Nginx系列教程(09) - rewrite
60 0
|
5月前
|
应用服务中间件 nginx
Nginx系列教程(07) - Location正则表达式
Nginx系列教程(07) - Location正则表达式
54 0
|
8月前
|
应用服务中间件 nginx
Nginx rewrite(URL)地址重定向
Nginx rewrite(URL)地址重定向
313 0
|
6月前
|
应用服务中间件 nginx
【Nginx】记录 nginx 配置文件中 location下根据 ua (user-agent) 判断移动端和电脑端不同浏览器来源
【Nginx】记录 nginx 配置文件中 location下根据 ua (user-agent) 判断移动端和电脑端不同浏览器来源
92 0
|
6月前
|
运维 Java 应用服务中间件
【Nginx用法】nginx location正则表达式写法,详解Nginx location 匹配规则(很详细哦)
【Nginx用法】nginx location正则表达式写法,详解Nginx location 匹配规则(很详细哦)
78 0
|
7月前
|
前端开发 应用服务中间件 PHP
nginx之rewrite
nginx之rewrite
45 0
|
8月前
|
运维 应用服务中间件 nginx
【运维知识进阶篇】Nginx Location语法优先级详解
【运维知识进阶篇】Nginx Location语法优先级详解
78 0
|
8月前
|
应用服务中间件 nginx
nginx: [emerg] duplicate location “/“ in /usr/local/etc/nginx/nginx.conf:142
nginx: [emerg] duplicate location “/“ in /usr/local/etc/nginx/nginx.conf:142
171 0
|
9月前
|
应用服务中间件 nginx
Nginx专题:location
Nginx专题:location
69 0