转载:https://segmentfault.com/a/1190000002797606php
http://www.cnblogs.com/lidabo/p/4169396.htmlcss
nginx 配置文件,自下到上分为三种井井有条的结构:
| http block the protocol level
| server block the server level
V location block the requested URIhtml
Nginx 容许用户定义 Location block ,并指定一个匹配模式(pattern)匹配特定的 URI。除了简单的字符串(好比文件系统路径),还容许使用更为复杂的匹配模式(pattern)。
Location block 的基本语法形式是:nginx
location [=|~|~*|^~|@] pattern { ... }
[=|~|~*|^~|@] 被称做 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)。web
location = / { # 精确匹配 / ,主机名后面不能带任何字符串
[ configuration A ] } location / { # 由于全部的地址都以 / 开头,因此这条规则将匹配到全部请求
# 可是正则和最长字符串会优先匹配
[ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合之后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration C ] } location ~ /documents/Abc { # 匹配任何以 /documents/ 开头的地址,匹配符合之后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration CC ] } location ^~ /images/ { # 匹配任何以 /images/ 开头的地址,匹配符合之后,中止往下搜索正则,采用这一条。
[ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配全部以 gif,jpg或jpeg 结尾的请求
# 然而,全部请求 /images/ 下的图片会被 config D 处理,由于 ^~ 到达不了这一条正则
[ configuration E ] } location /images/ { # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ configuration F ] } location /images/abc { # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ configuration G ] } location ~ /images/abc/ { # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
[ configuration H ] } location ~* /js/.*/\.js
顺序 no优先级: (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)正则表达式
上面的匹配结果 按照上面的location写法,如下的匹配示例成立:segmentfault
这会彻底匹配指定的 pattern ,且这里的 pattern 被限制成简单的字符串,也就是说这里不能使用正则表达式。后端
server { server_name website.com; location = /abcd { […] } }
匹配状况:tomcat
能够不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种状况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式。服务器
server { server_name website.com; location /abcd { […] } }
匹配状况:
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式
server { server_name website.com; location ~ ^/abcd$ { […] } }
匹配状况:
注意:对于一些对大小写不敏感的系统,好比 Windows ,~ 和 ~* 都是不起做用的,这主要是操做系统的缘由。
与 ~ 相似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
server { server_name website.com; location ~* ^/abcd$ { […] } }
匹配状况:
因此实际使用中,我的以为至少有三个匹配规则定义,以下: #直接匹配网站根,经过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。 #这里是直接转发给后端应用服务器了,也能够是一个静态首页 # 第一个必选规则
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/
}
若是咱们访问:http://127.0.0.1:8080/image/aiai.png
此时, “/” 与”/image/aiai.png” 匹配,同时,”image”正则 与”image/logo.png”也能匹配,谁发挥做用?
答案:正则表达式的成果将会使用!
location / { root D:\wnmp\www\html; index index.html index.htm index.php; } location ~ image { root D:\wnmp\www; //在这里要注意了,若是location 正则写image了则的 root 路径中不能够在次写image index index.html; }
图片真正会访问:D:\wnmp\www\image\aiai.png (而不是:D:\wnmp\www\html\image\aiai.png)
咱们访问 http://127.0.0.1:8080/foo
location / { root /usr/local/nginx/html; index index.html index.htm; } location /foo { root /var/www/html; index index.html; }
对于uri “/foo”, 两个location的patt,都能匹配他们,即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,此时, 真正访问 /var/www/html/index.html 缘由:’/foo’匹配的更长,所以使用之:
location ~ /hls123/(\d+).m3u8$ { #设置nginx变量 set $a $1; echo $a "::a = : ${a}"; }
curl "http://localhost/hls123/4001489370813.m3u8" 4001489370813 ::a = : 4001489370813
以上的这种是能够是接受参数的 $1 就是这参数
location ~ \/.+\/.+\.(m3u8|ts) { #设置nginx变量 if ($uri ~ \/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(|-).*\.(m3u8|ts)) { set $app_name $1; set $a $2; } echo "::document = : ${document_uri}"; echo "::uri = : ${uri}"; echo "::app_name = : ${app_name}"; echo "::stream_name = : ${a}"; #set $stream_id ""; #default_type 'text/html'; #lua_code_cache off; #rewrite_by_lua_file /home/www/lua-tinywan/set_by_file.lua; #echo "stream_id :" $stream_id; #proxy_pass $stream_id; }
curl "http://localhost/hls123/4001489370813.m3u8" ::document = : /hls123/4001489370813.m3u8 ::uri = : /hls123/4001489370813.m3u8 ::app_name = : hls123 ::stream_name = : 4001489370813
可使用上面的这个方法获取须要的参数(不会)