(一)Nginx location
(一)Nginx Location
location语法规则
Syntax: | location [ location |
---|---|
Default: | — |
Context: | server , location |
符号 |
含义 |
= |
= 开头表示精确匹配 |
^~ |
^~开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格) |
~ |
~ 开头表示区分大小写的正则匹配 |
~* |
~* 开头表示不区分大小写的正则匹配 |
!~和!~* |
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则 |
/ |
用户所使用的代理(一般为浏览器) |
$http_x_forwarded_for |
可以记录客户端IP,通过代理服务器来记录客户端的ip地址 |
$http_referer |
可以记录用户是从哪个链接访问过来的 |
nginx官方的例子:
location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /p_w_picpaths/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] }
在上述配置中,当用户请求“/”时,将匹配configuration A,当用户请求“/index.html”时,将匹配configuration B,当用户请求“/documents/document.html”时,将匹配configuration C,当用户请求“/p_w_picpaths/1.gif”时,将匹配configuration D;当用户请求“/documents/1.jpg”时,将匹配configuration E。
1,自己创建实例为:
[root@Monitor xn3]# mkdir documents [root@Monitor xn3]# mkdir p_w_picpaths [root@Monitor xn3]# vim documents/index.html this is documents html [root@Monitor xn3]# vim p_w_picpaths/index.html this is p_w_picpaths 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 ^~ /p_w_picpaths/ { ###匹配任何以 /p_w_picpaths/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。 index index.html; } location ~* \.(jpg|gif|png)$ { ###匹配所有以 gif,jpg或jpeg 结尾的请求,但是所有 /p_w_picpaths/ 目录的请求将响应/p_w_picpaths/请求 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/p_w_picpaths/ this is p_w_picpaths 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 /p_w_picpaths/ 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 ^/p_w_picpaths/(.*\.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用来判断文件是否可执行
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; } }
3 . 访问记录 将permanent改成redirect是状态码变成302
[root@localhost ~]# curl xn3.lqb.com/301 Moved Permanently 301 Moved Permanently
nginx [root@localhost ~]# curl xn3.lqb.com/302 Found 302 Found
nginx
(三)Nginx if指令
1. 简介该指令用于检查一个条件是否符合,如果条件符合,则执行大括号内的语句。If指令不支持嵌套,不支持多个条件&&和||处理。
语法: if (condition) { }
应用段:server,location
2.实例。通过if和rewrite来实现浏览器的网页分离
[root@Monitor xn2]# mkdir /html/xn2/chrome [root@Monitor xn2]# mkdir /html/xn2/ie [root@Monitor xn2]# more chrome/index.html this is chrome [root@Monitor xn2]# more ie/index.html this is IE liu lanqi [root@Monitor server]# vim server.conf server { listen 80; server_name xn2.lqb.com; root /html/xn2/; location / { if ($http_user_agent ~ Chrome) { rewrite ^(.*)$ /chrome/$1 break; } if ($http_user_agent ~ Trident) { rewrite ^(.*)$ /ie/$1 break; } # root /html/xn2; index index.html; } error_page 404 /404.html; }