@openxy
2016-04-03T09:25:56.000000Z
字数 4546
阅读 2740
nginx 配置文件,自下到上分为三种层次分明的结构:
| http block the protocol level
| server block the server level
V location block the requested URI
1、“=” 开头表示精确匹配
2、“^~” 开头表示uri以某个常规字符串开头,理解为匹配url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
3、“~” 开头表示区分大小写的正则匹配
4、“~*” 开头表示不区分大小写的正则匹配
5、“!~”和“!~*”分别为区分大小写不匹配及不区分大小写不匹配 的正则
6、“/” 通用匹配,任何请求都会匹配到。
1 =
这会完全匹配指定的 pattern ,且这里的 pattern 被限制成简单的字符串,也就是说这里不能使用正则表达式。
server {server_name website.com;location = /abcd {[…]}}http://website.com/abcd # 正好完全匹配http://website.com/ABCD # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配http://website.com/abcd?param1=m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1=m2http://website.com/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配http://website.com/abcde # 不匹配,因为不是完全匹配
2 (None)
可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式。
server {server_name website.com;location /abcd {[…]}}http://website.com/abcd # 正好完全匹配http://website.com/ABCD # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配http://website.com/abcd?param1=m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1=m2http://website.com/abcd/ # 末尾存在反斜杠(trailing slash)也属于匹配范围内http://website.com/abcde # 仍然匹配,因为 URI 是以 pattern 开头的
3 ~
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式
server {server_name website.com;location ~ ^/abcd$ {[…]}}http://website.com/abcd # 完全匹配http://website.com/ABCD # 不匹配,~ 对大小写是敏感的http://website.com/abcd?param1=m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1=m2http://website.com/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$http://website.com/abcde # 不匹配正则表达式 ^/abcd$注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。
4 ~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
server {server_name website.com;location ~* ^/abcd$ {[…]}}http://website.com/abcd # 完全匹配http://website.com/ABCD # 匹配,这就是它不区分大小写的特性http://website.com/abcd?param1=m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1=m2http://website.com/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$http://website.com/abcde # 不匹配正则表达式 ^/abcd$
5 ^~
匹配情况类似 2. (None) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)
6 @
用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page
若定义有多个 Location 块时,则当 Nginx 收到一个请求时:
1. 按何种顺序依次与多个location块比较?哪些location会被选入?即匹配逻辑
2. 当匹配有多个结果时,选择哪一个为最终结果?即优先级
总结一句话: “正则 location 匹配让步普通 location 的严格精确匹配结果;但覆盖普通 location 的最大前缀匹配结果”
1. =
2. (None) 如果 pattern 完全匹配 URI(不是只匹配 URI 的头部)
3. ^~
4. ~ 或 ~*
5. (None) pattern 匹配 URI 的头部
location = /{#规则A}location = /login {#规则B}location ^~ /static/ {#规则C}location ~ \.(gif|jpg|png|js|css)$ {#规则D}location ~* \.png$ {#规则E}location !~ \.xhtml$ {#规则F}location !~* \.xhtml$ {#规则G}location / {#规则H}
访问根目录/,比如http://localhost/ 将匹配规则A访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H访问 http://localhost/static/a.html 将匹配规则C访问 http://localhost/a.gif, http://localhost/b.jpg将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png则优先匹配到 规则C访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。访问 http://localhost/category/id/1111则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
实际工作中,一般至少定义三个匹配规则:
#精确匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理。#这里是直接转发给后端应用服务器了,也可以是一个静态首页# 第一个必选规则location = / {proxy_pass http://tomcat:8080/index}#第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项# 以下两种配置模式任选其一,目录匹配或后缀匹配:location ^~ /static/ {root/webroot/static/;}location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {root /webroot/res/;}#第三个规则就是通用规则,用来转发动态请求到后端应用服务器#非静态文件请求就默认是动态请求,自己根据实际把握#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了location /{proxy_pass http://tomcat:8080/}
# 目录权限location ~ ^/(cron|templates)/ {deny all;break;}# 泛化目录权限location ~ ^/data {deny all;}# 文件权限location ~ /data/sql/data.sql {deny all;}# 给favicon.ico和robots.txt设置过期时间;这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志location ~(favicon.ico) {log_not_found off;expires 99d;break;}location ~(robots.txt) {log_not_found off;expires 7d;break;}# 设定某个文件的过期时间;这里为600秒,并不记录访问日志location ^~ /html/scripts/loadhead_1.js {access_log off;root /www;expires 600;break;}## 根据文件类型设置过期时间location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {if (-f $request_filename) {expires 1h;break;}}
# /data/1.xls => root_dir + '/export/json/1.json'location ~ ^/data/(\d+){alias /export/json/$1.json}