1、location的作用:

  location指令的作用是根据用户的请求的URL来执行不同的应用

2、location的语法:

location   [ = |  ~  |  ~*  | ^~ ]  uri {  ....... }
  • location:指令
  • [ = | ~ | ~* | ^~ ] :匹配的标识(在这里面 ‘~’ 用于区分大小写,‘~*’ 不区分大小写,'^~' 是在做完常规检查后不检查正则匹配)

  • uri:匹配的网站地址
  • {......}:匹配URI后要执行的配置段

3、官方示例:

locarion = / {
[ configuration A ]
} # 用户访问 / 的时候,匹配configuration A
locarion / {
[ configuration B ]
} # 用户访问 /index.html 的时候,匹配configuration B
locarion /documents/ {
[ configuration C ]
} # 用户访问 /documents/documents.html 的时候,匹配configuration C
locarion ^~ /images/ {
[ configuration D ]
} # 用户访问 /images/1.gif 的时候,匹配configuration D
locarion ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
} # 用户访问 /documents/1.gif 的时候,匹配configuration E

4、实战:

  以www.brian.com虚拟主机为例,修改brian.conf配置文件:

[root@Nginx www_date]# cat brian.conf
server {
listen 80;
server_name www.brian.com brian.com;
root html/brian;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {        
return 404;        # 匹配任何以/images/开头的查询
}
location ~* \.(gif|jpg|jpeg)$ {
return 405;         # 匹配任何以 gif、jpg、jpeg结尾的请求
}
access_log logs/brian.log main gzip buffer=128k flush=5s;
}

  检查语法:

[root@Nginx conf]# ../sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful

  平滑重启:

[root@Nginx conf]# ../sbin/nginx -s reload

  Linux客户端测试:

[root@Nginx conf]# curl www.brian.com/                   # 对应location = /
<html>
<head><title>402 Payment Required</title></head>      # 返回402
<body bgcolor="white">
<center><h1>402 Payment Required</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/index.html      # 对应location /
<html>
<head><title>401 Authorization Required</title></head> # 返回401
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/documents/ #对应location /documents/
<html>
<head><title>403 Forbidden</title></head>           # 返回403
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/images/1.gif     # 对应location ^~ /images/
<html>
<head><title>404 Not Found</title></head>            # 返回404
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/documents/1.gif # 对应location ~* \.(gif|jpg|jpeg)$
<html>
<head><title>405 Not Allowed</title></head>          # 返回405
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>

  

Nginx的location剖析的更多相关文章

  1. 菜鸟nginx源码剖析

    菜鸟nginx源码剖析 配置与部署篇(一) 手把手配置nginx "I love you" TCMalloc 对MYSQL 性能 优化的分析 菜鸟nginx源码剖析系列文章解读 A ...

  2. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  3. Nginx之location 匹配规则详解

    有些童鞋的误区 1. location 的匹配顺序是“先匹配正则,再匹配普通”. 矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”.我这么说,大家一定会反驳我,因为按“先匹配普通, ...

  4. nginx 中location和root

    nginx 中location和root,你确定真的明白他们关系? 2016-01-17 14:48 3774人阅读 评论(1) 收藏 举报  分类: linux(17)  版权声明:本文为博主原创文 ...

  5. Nginx 的 Location 配置指令块

    最近一段时间在学习 Nginx ,以前一直对 Nginx 的 Location 配置很头大,最近终于弄出点眉目.总结如下:nginx 配置文件,自下到上分为三种层次分明的结构: |    http b ...

  6. 菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)

    俗话说的好,牵牛要牵牛鼻子 驾车顶牛,处理复杂的东西,只要抓住重点,才能理清脉络,不至于深陷其中,不能自拔.对复杂的nginx而言,main函数就是“牛之鼻”,只要能理清main函数,就一定能理解其中 ...

  7. 快速掌握Nginx(二) —— Nginx的Location和Rewrite

    1 location详解 1.location匹配规则 Nginx中location的作用是根据Url来决定怎么处理用户请求(转发请求给其他服务器处理或者查找本地文件进行处理).location支持正 ...

  8. nginx之location的匹配规则

    nginx之location的匹配规则 一.语法规则 location [=|~|~*|^~] /uri/ { - } 符号 含义 = 开头表示精确匹配 ^~ 开头表示 uri 以某个常规字符串开头 ...

  9. Nginx的location匹配规则

    一 Nginx的location语法 location [=|~|~*|^~] /uri/ { … } =         严格匹配.如果请求匹配这个location,那么将停止搜索并立即处理此请求 ...

随机推荐

  1. Flask-SQLAlchemy插件

    一,初始化 两种方式: db = SQLAlchemy() def create_app(): app = Flask(__name__) db.init_app(app) return app ap ...

  2. flask加vue 动画 加载更多

    曾经使用flask_paginate(地址:https://blog.csdn.net/qq_42239520/article/details/80378095)进行分页,现在又想新的想法,怎么才能和 ...

  3. cpu负载的探讨 (转)

    文章出处:http://blog.chinaunix.net/uid-12693781-id-368837.html 摘要:确定cpu的负载的定义,帮助管理员设置cpu负载阀值,推测可能的导致cpu负 ...

  4. ASP.NET MVC网站使用新浪微博账号登录

    首先到http://open.weibo.com/development 注册一个开发者账号. 然后可以点微连接--网站接入 会分配App Key 和App Secret 然后点高级信息 在这里设置回 ...

  5. TensorFlow object detection API应用--配置

    目标检测在图形识别的基础上有了更进一步的应用,但是代码也更加繁琐,TensorFlow专门为此开设了一个object detection API,接下来看看怎么使用它. object detectio ...

  6. php获取 POST请求的数据

    普通键值对的数据: $_POST[‘username’]; // 获取 username的信息: $_REQUEST; //则会获取 整个请求中的键值对,返回结果为数组: 如果是,流数据,则需要使用: ...

  7. Web开发者应知的URL编码知识

    原文出处:http://blog.jobbole.com/42246/ 本文首先阐述了人们关于统一资源定位符(URL)编码的普遍的误读,其后通过阐明HTTP场景下的URL encoding 来引出我们 ...

  8. 常用的oh-my-zsh插件

    每次换电脑,需要重新配置开发环境是件很麻烦的事情,作为一个有洁癖的人又不想用Time Machine.记忆力大不如以前,很多插件又忘了装.正好下个月又需要给团队小伙伴讲讲提升效率这件事要讲到oh-my ...

  9. c++语言对c的扩充

    1.命名空间的使用 参见下列链接:http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html 需要注意的地方:如果使用了命名空间s ...

  10. Winform下判断文件和文件夹是否存在

    //选择文件夹 FolderBrowserDialog dia = new FolderBrowserDialog(); if (dia.ShowDialog() == System.Windows. ...