天天看点

nginx location 配置详细解释

语法规则: location [=|~|~*|^~] /uri/ { … }

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

例子,有如下匹配规则:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<code>location = / {</code>

<code>   </code><code>#规则A</code>

<code>}</code>

<code>location = </code><code>/login</code> <code>{</code>

<code>   </code><code>#规则B</code>

<code>location ^~ </code><code>/static/</code> <code>{</code>

<code>   </code><code>#规则C</code>

<code>location ~ \.(gif|jpg|png|js|css)$ {</code>

<code>   </code><code>#规则D</code>

<code>location ~* \.png$ {</code>

<code>   </code><code>#规则E</code>

<code>location !~ \.xhtml$ {</code>

<code>   </code><code>#规则F</code>

<code>location !~* \.xhtml$ {</code>

<code>   </code><code>#规则G</code>

<code>location / {</code>

<code>   </code><code>#规则H</code>

那么产生的效果如下:

访问根目录/, 比如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作为方向代理服务器存在。

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:

<code>#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。</code>

<code>#这里是直接转发给后端应用服务器了,也可以是一个静态首页</code>

<code># 第一个必选规则</code>

<code>    </code><code>proxy_pass http:</code><code>//tomcat</code><code>:8080</code><code>/index</code>

<code># 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项</code>

<code># 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用</code>

<code>    </code><code>root </code><code>/webroot/static/</code><code>;</code>

<code>location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {</code>

<code>    </code><code>root </code><code>/webroot/res/</code><code>;</code>

<code>#第三个规则就是通用规则,用来转发动态请求到后端应用服务器</code>

<code>#非静态文件请求就默认是动态请求,自己根据实际把握</code>

<code>#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了</code>

<code>    </code><code>proxy_pass http:</code><code>//tomcat</code><code>:8080/</code>

nginx的其他配置信息介绍

三、ReWrite语法

last – 基本上都用这个Flag。

break – 中止Rewirte,不在继续匹配

redirect – 返回临时重定向的HTTP状态302

permanent – 返回永久重定向的HTTP状态301

1、下面是可以用来判断的表达式:

-f和!-f用来判断是否存在文件

-d和!-d用来判断是否存在目录

-e和!-e用来判断是否存在文件或目录

-x和!-x用来判断文件是否可执行

2、下面是可以用作判断的全局变量

<code>$host:localhost</code>

<code>$server_port:88</code>

<code>$request_uri:http:</code><code>//localhost</code><code>:88</code><code>/test1/test2/test</code><code>.php</code>

<code>$document_uri:</code><code>/test1/test2/test</code><code>.php</code>

<code>$document_root:D:\nginx</code><code>/html</code>

<code>$request_filename:D:\nginx</code><code>/html/test1/test2/test</code><code>.php</code>

四、Redirect语法

<code>server {</code>

<code>    </code><code>listen 80;</code>

<code>    </code><code>server_name start.igrow.cn;</code>

<code>    </code><code>index index.html index.php;</code>

<code>    </code><code>root html;</code>

<code>    </code><code>if</code> <code>($http_host !~ </code><code>"^star\.igrow\.cn$"</code> <code>{</code>

<code>        </code><code>rewrite ^(.*) http:</code><code>//star</code><code>.igrow.cn$1 redirect;</code>

<code>    </code><code>}</code>

五、防盗链

<code>location ~* \.(gif|jpg|swf)$ {</code>

<code>    </code><code>valid_referers none blocked start.igrow.cn sta.igrow.cn;</code>

<code>    </code><code>if</code> <code>($invalid_referer) {</code>

<code>        </code><code>rewrite ^/ http:</code><code>//</code><code>$host</code><code>/logo</code><code>.png;</code>

六、根据文件类型设置过期时间

<code>location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {</code>

<code>    </code><code>if</code> <code>(-f $request_filename) {</code>

<code>        </code><code>expires 1h;</code>

<code>        </code><code>break</code><code>;</code>

七、禁止访问某个目录

<code>location ~* \.(txt|doc)${</code>

<code>root </code><code>/data/www/wwwroot/linuxtone/test</code><code>;</code>

<code>deny all;</code>

附:一些可用的全局变量

<code>$args</code>

<code>$content_length</code>

<code>$content_type</code>

<code>$document_root</code>

<code>$document_uri</code>

<code>$host</code>

<code>$http_user_agent</code>

<code>$http_cookie</code>

<code>$limit_rate</code>

<code>$request_body_file</code>

<code>$request_method</code>

<code>$remote_addr</code>

<code>$remote_port</code>

<code>$remote_user</code>

<code>$request_filename</code>

<code>$request_uri</code>

<code>$query</code>

本文转自 tanzhenchao 51CTO博客,原文链接:http://blog.51cto.com/cmdschool/1704229,如需转载请自行联系原作者