一、Nginx Location
1、lication作用
lication根据客户端请求的URL进行匹配,并给出相应的操作。lication在server区段中定义,并可定义多个。
2、lication语法
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
[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、在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) 变量名;
(2) 以变量为操作数构成的比较表达式
(3) 正则表达式的模式匹配操作
(4) 测试路径为文件可能性:-f, !-f
(5) 测试指定路径为目录的可能性:-d, !-d
(7) 检查文件是否有执行权限:-x
3、rewrite配置
[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,实现基于浏览器的网页分离。
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