Nginx Location语法规则 爱被打了一巴掌 2022-09-24 06:25 136阅读 0赞 语法规则: location \[=|~|~\*|^~\] /uri/ \{ … \} 其中“~ ”和“~\* ”前缀表示正则location ,“~ ”区分大小写,“~\* ”不区分大小写;其他前缀(包括:“=”,“^~ ”和“@ ”)和无任何前缀的都属于普通location 。 = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。 ~ 开头表示区分大小写的正则匹配 ~\* 开头表示不区分大小写的正则匹配 !~和!~\*分别为区分大小写不匹配及不区分大小写不匹配 的正则 / 通用匹配,任何请求都会匹配到。 总结一句话: “正则 location 匹配让步普通 location 的严格精确匹配结果;但覆盖普通 location 的最大前缀匹配结果” 首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。 例子,有如下匹配规则: 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/][http_localhost] 将匹配规则A \#因为A是精确匹配. 访问 [http://localhost/login][http_localhost_login] 将匹配规则B,[http://localhost/register][http_localhost_register] 则匹配规则H 访问 [http://localhost/static/a.html][http_localhost_static_a.html] 将匹配规则C 访问 [http://localhost/a.gif][http_localhost_a.gif], [http://localhost/b.jpg][http_localhost_b.jpg] 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而[http://localhost/static/c.png][http_localhost_static_c.png] 则优先匹配到 规则C 访问 [http://localhost/a.PNG][http_localhost_a.PNG] 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。 访问 [http://localhost/a.xhtml][http_localhost_a.xhtml] 不会匹配规则F和规则G,[http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。][http_localhost_a.XHTML_G_F_G] 访问 [http://localhost/category/id/1111][http_localhost_category_id_1111] 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。 所以实际使用中,个人觉得至少有三个匹配规则定义,如下: \#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。 \#这里是直接转发给后端应用服务器了,也可以是一个静态首页 \# 第一个必选规则 location = / \{ proxy\_pass [http://tomcat:8080/index][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_tomcat_8080] \} 未试验过的其他信息: 三、ReWrite语法 last – 基本上都用这个Flag。 break – 中止Rewirte,不在继续匹配 redirect – 返回临时重定向的HTTP状态302 permanent – 返回永久重定向的HTTP状态301 1、下面是可以用来判断的表达式: \-f和!-f用来判断是否存在文件 \-d和!-d用来判断是否存在目录 \-e和!-e用来判断是否存在文件或目录 \-x和!-x用来判断文件是否可执行 2、下面是可以用作判断的全局变量 例:[http://www.test.com:88/test1/test2/test.php][http_www.test.com_88_test1_test2_test.php] $host:[www.test.com][http_www.test.com_88_test1_test2_test.php] $server\_port:88 $request\_uri:[/test1/test2/test.php][http_www.test.com_88_test1_test2_test.php] $document\_uri:/test1/test2/test.php $document\_root:/nginx/html \#网站根目录 $request\_filename:/nginx/html/test1/test2/test.php 四、Redirect语法 server \{ listen 80; server\_name start.igrow.cn; index index.html index.php; root html; if ($http\_host !~ “^star\\.igrow\\.cn$" \{ rewrite ^(.\*) [http://star.igrow.cn$1][http_star.igrow.cn_1] redirect; \} \} 五、防盗链location ~\* \\.(gif|jpg|swf)$ \{ valid\_referers none blocked start.igrow.cn sta.igrow.cn; if ($invalid\_referer) \{ rewrite ^/ [http://$host/logo.png;][http_host_logo.png] \} \} 六、根据文件类型设置过期时间 location ~\* \\.(js|css|jpg|jpeg|gif|png|swf)$ \{ if (-f $request\_filename) \{ expires 1h; break; \} \} 七、禁止访问某个目录 location ~\* \\.(txt|doc)$\{ root /data/www/wwwroot/linuxtone/test; deny all; \} rewrite ^(\[0-9\]+)\\.blog.(\[a-z\]+).ankang06.org http://$2.blog.ankang06.org/user/index/?uid=$1 \#上面这个rewrite例子上面的彩色字符需要注意!!! $1 代表第一个()内的正则匹配到的内容(0-9数字), $2 代表第二个正则匹配到的内容(a-z字符)!! \++ 一些可用的全局变量 $arg\_PARAMETER \#这个变量包含GET请求中,如果有变量PARAMETER时的值。 $args \#这个变量等于请求行中(GET请求)的参数,例如foo=123&bar=blahblah; $binary\_remote\_addr \#二进制的客户地址。 $body\_bytes\_sent \#响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。 $content\_length \#请求头中的Content-length字段。 $content\_type \#请求头中的Content-Type字段。 $cookie\_COOKIE \#cookie COOKIE变量的值 $document\_root \#当前请求在root指令中指定的值。 $document\_uri \#与$uri相同。 $host \#请求主机头字段,否则为服务器名称。 $hostname \#Set to the machine’s hostname as returned by gethostname $http\_HEADER $is\_args \#如果有$args参数,这个变量等于”?”,否则等于”",空值。 $http\_user\_agent \#客户端agent信息 $http\_cookie \#客户端cookie信息 $limit\_rate \#这个变量可以限制连接速率。 $query\_string \#与$args相同。 $request\_body\_file \#客户端请求主体信息的临时文件名。 $request\_method \#客户端请求的动作,通常为GET或POST。 $remote\_addr \#客户端的IP地址。 $remote\_port \#客户端的端口。 $remote\_user \#已经经过Auth Basic Module验证的用户名。 $request\_completion \#如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。 $request\_method \#GET或POST $request\_filename \#当前请求的文件路径,由root或alias指令与URI请求生成。 $request\_uri \#包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。 $scheme \#HTTP方法(如http,https)。 $server\_protocol \#请求使用的协议,通常是HTTP/1.0或HTTP/1.1。 $server\_addr \#服务器地址,在完成一次系统调用后可以确定这个值。 $server\_name \#服务器名称。 $server\_port \#请求到达服务器的端口号。 $uri \#不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。该值有可能和$request\_uri 不一致。$request\_uri是浏览器发过来的值。该值是rewrite后的值。例如做了internal redirects后。 解释更详细的网址: [http://eyesmore.iteye.com/blog/1141660][http_eyesmore.iteye.com_blog_1141660] nginx rewrite规则 1带有问号的目标地址 www.ankang06.com/user/index/?uid=1118 => [http://1118.blog.ankang06.com][http_1118.blog.ankang06.com] if ($query\_string ~\* uid=(\[0-9\]\*)$) \{ rewrite ^/user/index/(.\*)$ http://$id.blog.ankang06.com/? permanent; \} [http_localhost]: http://localhost/ [http_localhost_login]: http://localhost/login [http_localhost_register]: http://localhost/register [http_localhost_static_a.html]: http://localhost/static/a.html [http_localhost_a.gif]: http://localhost/a.gif [http_localhost_b.jpg]: http://localhost/b.jpg [http_localhost_static_c.png]: http://localhost/static/c.png [http_localhost_a.PNG]: http://localhost/a.PNG [http_localhost_a.xhtml]: http://localhost/a.xhtml [http_localhost_a.XHTML_G_F_G]: http://localhost/a.XHTML%E4%B8%8D%E4%BC%9A%E5%8C%B9%E9%85%8D%E8%A7%84%E5%88%99G%EF%BC%8C%E5%9B%A0%E4%B8%BA%E4%B8%8D%E5%8C%BA%E5%88%86%E5%A4%A7%E5%B0%8F%E5%86%99%E3%80%82%E8%A7%84%E5%88%99F%EF%BC%8C%E8%A7%84%E5%88%99G%E5%B1%9E%E4%BA%8E%E6%8E%92%E9%99%A4%E6%B3%95%EF%BC%8C%E7%AC%A6%E5%90%88%E5%8C%B9%E9%85%8D%E8%A7%84%E5%88%99%E4%BD%86%E6%98%AF%E4%B8%8D%E4%BC%9A%E5%8C%B9%E9%85%8D%E5%88%B0%EF%BC%8C%E6%89%80%E4%BB%A5%E6%83%B3%E6%83%B3%E7%9C%8B%E5%AE%9E%E9%99%85%E5%BA%94%E7%94%A8%E4%B8%AD%E5%93%AA%E9%87%8C%E4%BC%9A%E7%94%A8%E5%88%B0%E3%80%82 [http_localhost_category_id_1111]: http://localhost/category/id/1111 [http_tomcat_8080_index]: http://tomcat:8080/index [http_tomcat_8080]: http://tomcat:8080/ [http_www.test.com_88_test1_test2_test.php]: http://localhost:88/test1/test2/test.php [http_star.igrow.cn_1]: http://denglz.blog.51cto.com/ [http_host_logo.png]: http://denglz.blog.51cto.com/logo.png; [http_eyesmore.iteye.com_blog_1141660]: http://eyesmore.iteye.com/blog/1141660 [http_1118.blog.ankang06.com]: http://18.blog.ankang06.com/
还没有评论,来说两句吧...