nginx location配置详细解释

http://outofmemory.cn/code-snippet/742/nginx-location-configuration-xiangxi-explain

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

  • = 开头表示精确匹配

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

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

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

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

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

多个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/ 将匹配规则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/
}

nginx的其他配置信息介绍

三、ReWrite语法

last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301

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

-f!-f用来判断是否存在文件
-d!-d用来判断是否存在目录
-e!-e用来判断是否存在文件或目录
-x!-x用来判断文件是否可执行

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

例:http://localhost:88/test1/test2/test.php

$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\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 redirect;
    }
}

五、防盗链

location ~* \.(gif|jpg|swf)$ {
    valid_referers none blocked start.igrow.cn sta.igrow.cn;
    if ($invalid_referer) {
        rewrite ^/ 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;
}

附:一些可用的全局变量

$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query

[转帖]nginx location配置详细解释的更多相关文章

  1. Nginx location配置详细解释

    nginx location配置详细解释 语法规则: location [=|~|~*|^~] /uri/ { - } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 ur ...

  2. Nginx Location配置总结

    Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...

  3. nginx location配置

    nginx location配置   location在nginx中起着重要作用,对nginx接收到的请求字符串进行处理,如地址定向.数据缓存.应答控制.代理转发等location语法location ...

  4. nginx location 配置详解 【转载,整理】

    http://www.nginx.cn/115.html NGINX location 配置参考:http://www.cnblogs.com/zlingh/p/6288994.html https: ...

  5. Nginx location 配置用法及正则例子

    Nginx location 配置语法     1. location [ = | ~ | ~* | ^~ ] uri { ... }     2. location @name { ... }   ...

  6. nginx location配置与rewrite配置

    注:原文出处 www.linuxidc.com/Linux/2015-06/119398.htm 1. location正则写法 一个示例: location =/{ # 精确匹配 / ,主机名后面不 ...

  7. Nginx Location配置语法介绍、优先级说明

    nginx 语法规则:location   [=|~|~*|^~|!~|!~*]    /uri/   { … } location匹配的是$document_uri,$document_uri 会随 ...

  8. Nginx Location配置总结及基础最佳实践

    参考来源: http://blog.zol.com.cn/1067/article_1066186.html,http://flandycheng.blog.51cto.com/855176/2801 ...

  9. nginx Location配置总结(转)

    本文部分转自:http://cssor.com/nginx-location-configuration.html 一. 开头 语法规则: location [=|~|~*|^~] /uri/ { … ...

随机推荐

  1. python 比较运算符和逻辑运算符

    <1> 比较(即关系)运算符 python中的比较运算符如下表 运算符 描述 示例 == 检查两个操作数的值是否相等,如果是则条件变为真. 如a=3,b=3则(a == b) 为 true ...

  2. Java学习日记基础篇(三-下)——流程控制之循环控制

    循环控制 for循环 语法: for(循环初值;循环条件;步长) { 语句; //循环体 } 例子: import java.io.*; public class Demo4 { public sta ...

  3. STL算法之find

    定义 template <class InputIterator, class T> InputIterator find (InputIterator first, InputItera ...

  4. python中的with的用法,上下文管理器

    with是从Python2.5引入的一个新的语法,它是一种上下文管理协议,目的在于从流程图中把 try,except 和finally 关键字和 资源分配释放相关代码统统去掉,简化try….excep ...

  5. php实现excel单元格合并,字体加粗居中等操作

    使用的是phpexcel,基本用的原生语法,所见即所得,直接复制下面代码,即可: // 引用phpexcel类 $this->load->library('PHPExcel'); // 创 ...

  6. Docs-.NET-C#-指南-语言参考-预处理器指令:#undef(C# 参考)

    ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#undef(C# 参考) 1.返回顶部 1. #undef(C# 参考) 2018/06/30 #undef 允许你定义一个符 ...

  7. 清空表且id为0

    sql命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table

  8. js往标签下插入标签的方法

    js="document.getElementsByClassName('CodeMirror-line')[0].innerHTML = 'xxxxxx'"dr.execute_ ...

  9. LeetCode_118. Pascal's Triangle

    118. Pascal's Triangle Easy Given a non-negative integer numRows, generate the first numRows of Pasc ...

  10. Tips for TMUX

    常用命令 tmux ls # 显示后台session列表 tmux new -t [name] # 新建session tmux a -t [name] # 进入session tmux kill-s ...