Nginx学习日记第三篇 -- location匹配,rewrite

简介:

一、Nginx Location

 1、lication作用

  lication根据客户端请求的URL进行匹配,并给出相应的操作。lication在server区段中定义,并可定义多个。

 2、lication语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
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。

 

 3、根据上述例子实例配置location

首先重新编译安装Nginx,编译第三方模块echo,便于测试

echo模块项目站点https://github.com/openresty/echo-nginx-module/tags

1
2
3
4
5
6
7
8
9
10
11
12
[root@Nginx ~] # /usr/local/nginx/sbin/nginx -V
nginx version: nginx /1 .10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix= /usr/local/nginx  --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with- file -aio --with-http_v2_module --with-ipv6
 
原编译参数如上,下载 echo 模块并保存至 /root 目录下,编译时加上--add-module= /root/echo-nginx-module-0 .60
 
[root@Nginx ~] # tar xf echo-nginx-module-0.60.tar.gz 
 
[root@Nginx nginx-1.10.3] # ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 --add-module=/root/echo-nginx-module-0.60

注意:将二进制执行文件先备份,再编译。


配置location

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
42
43
44
1、在nginx.conf中includelocations.conf,注释掉server.conf,避免影响
#include server.conf;
include locations.conf;
 
2、vim locations.conf
server {
     listen   80;
     server_name  xn1.51cto.com;
     root html /xn1 ;
     location = / {           # 只匹配 / 的查询
     echo  "A" ;
     }
     location / {             # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配
     echo  "B" ;
     }
     location  /documents/  {   # 匹配/documents/
         echo  "C" ;
     }
     location ^~  /images/  {   # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式
     echo  "D" ;
     }
     location ~* \.(jpg|gif|png)$ {
     echo  "E" ;            # 匹配任何以gif, jpg, or jpeg结尾的文件,但是所有 /images/ 目录的请求将响应D
     }
     access_log logs /locations .log;
 
}
 
3、测试
[root@Nginx 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@Nginx conf] # /usr/local/nginx/sbin/nginx -s reload
 
[root@Nginx conf] # curl xn1.51cto.com
A
[root@Nginx conf] # curl xn1.51cto.com/index.html
B
[root@Nginx conf] # curl xn1.51cto.com/documents/document.html
C
[root@Nginx conf] # curl xn1.51cto.com/images/1.gif
D
[root@Nginx conf] # curl xn1.51cto.com/documents/1.jpg
E

 

 4、总结

(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地址的重写,这个模块在编译时是默认安装的,同时需要pcre包的支持。

 1、rewrite语法:

   rewrite regex replacement [flag];

   rewrite ^/images/(.*\.jpg|jpeg|png)$ /imgs/$1 break;

   regex:正则表达式

^ :必须以^后的实体开头

$ :必须以$前的实体结尾

.   :匹配任意字符

[ ] :匹配指定字符集内的任意字符

[^ ] :匹配任何不包括在指定字符集内的任意字符串

| :匹配 | 之前或之后的实体

() :分组,组成一组用于匹配的实体,通常会有|来协助

   replacement:通过regex匹配到的请求重定向到replacement

   flag:标记符号,

    last:本条规则匹配完成后,继续向下匹配;

    break:中止Rewirte,不在继续匹配;

    redirect:返回临时重定向的HTTP状态302;

    permanent:返回永久重定向的HTTP状态301;

 2、if语法

  语法:if (condition) {...}

应用环境:server, location

  condition:

   (1) 变量名;

 变量值为空串,或者以“0”开始,则为false;其它的均为true; 

   (2) 以变量为操作数构成的比较表达式

 可使用=, !=类似的比较操作符进行测试;

   (3) 正则表达式的模式匹配操作

 ~: 区分大小写的模式匹配检查

 ~*: 不区分大小写的模式匹配检查

 !~和!~*:对上面两种测试取反

   (4) 测试路径为文件可能性:-f, !-f

   (5) 测试指定路径为目录的可能性:-d, !-d

   (6) 测试文件的存在性:-e, !-

   (7) 检查文件是否有执行权限:-x

 

 3、rewrite配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@Nginx conf] # grep -Ev "#|^$" server.conf
server {
     listen       192.168.0.10:8002;
     server_name  xn2.51cto.com;
     location / {
     root  html /xn2 ;
     index  index.html;
     }
}
     server {
         listen       80;
         server_name   xn1.51cto.com;
     
         location / {
         rewrite ^/(.*) http: //192 .168.0.10:8002 permanent;
             root   html /xn1 ;
             index  index.html index.htm;
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }

 rewrite除了在location中定义,还可以在server和if中定义,根据使用场景的不同,可灵活使用rewrite,下面是在if中定义rewrite,实现基于浏览器的网页分离。

1
2
3
4
5
6
7
8
9
10
11
if  ($http_user_agent ~ Firefox) {  
   rewrite ^(.*)$  /firefox/ $1  break ;  
   }  
   
   if  ($http_user_agent ~ MSIE) {  
     rewrite ^(.*)$  /msie/ $1  break ;  
    }  
   
  if  ($http_user_agent ~ Chrome) {  
       rewrite ^(.*)$  /chrome/ $1  break ;  
    }


更多详细信息详见:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html





本文转自 元婴期 51CTO博客,原文链接:http://blog.51cto.com/jiayimeng/1895130




相关文章
|
18天前
|
负载均衡 应用服务中间件 数据处理
Nginx学习使用
Nginx学习使用
38 0
|
4月前
|
自然语言处理 前端开发 应用服务中间件
nginx的Location语法规则
nginx的Location语法规则
|
5月前
|
应用服务中间件 nginx Perl
Nginx系列教程(09) - rewrite
Nginx系列教程(09) - rewrite
60 0
|
5月前
|
应用服务中间件 nginx
Nginx系列教程(07) - Location正则表达式
Nginx系列教程(07) - Location正则表达式
60 0
|
2月前
|
缓存 负载均衡 安全
Nginx 学习
Nginx 学习
30 0
|
4月前
|
应用服务中间件 nginx
【Nginx学习】—Nginx基本知识
【Nginx学习】—Nginx基本知识
|
4月前
|
应用服务中间件 Linux 网络安全
小白带你学习linux Nginx基本部署 (二十一)
小白带你学习linux Nginx基本部署 (二十一)
33 0
|
5月前
|
负载均衡 算法 应用服务中间件
Nginx学习使用
Nginx学习使用
46 1
|
5月前
|
应用服务中间件 API nginx
通过 docker 学习 nginx,附全部配置及 API 测试,使用 apifox 直接打开
本篇文章以前端的视角,介绍下 nginx 的常见配置,并通过 docker 的方式学习 nginx,这保证所有实例配置都能正常运行。
|
6月前
|
数据采集 缓存 负载均衡
Nginx 的学习与实战
Nginx是开源、高性能、高可靠的 Web 和反向代理服务器,能支持高达 50,000个并发连接数。Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强。
50 2