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. Spark集群测试

    1. Spark Shell测试 Spark Shell是一个特别适合快速开发Spark原型程序的工具,可以帮助我们熟悉Scala语言.即使你对Scala不熟悉,仍然可以使用这一工具.Spark Sh ...

  2. Selenium自动化测试Python四:WebDriver封装

    WebDriver 封装 欢迎阅读WebDriver封装讲义.本篇讲义将会重点介绍Selenium WebDriver API的封装的概念和方法,以及使用封装进行自动化测试的设计. WebDriver ...

  3. 深入理解Spring的ImportSelector接口

    ImportSelector接口是至spring中导入外部配置的核心接口,在SpringBoot的自动化配置和@EnableXXX(功能性注解)都有它的存在,关于SpringBoot的分析可以参考:深 ...

  4. SQL SERVICE 拆分字符串的表值函数

    SQL代码 ALTER FUNCTION [dbo].[SplitToTable]( @SplitString nvarchar(max), @Separator nvarchar(10)=' ')R ...

  5. 改善android性能工具篇【zipalign】

    什么是Zipalign?      Zipalign是一个android平台上整理APK文件的工具,它首次被引入是在Android 1.6版本的SDK软件开发工具包中.它能够对打包的Android应用 ...

  6. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二):数据库设计

    数据库设计 系统主要包含用户(sys_user).组织(sys_dept).角色(sys_role).菜单(sys_menu).角色组织(sys_role_dept).角色菜单(sys_role_me ...

  7. Nginx缓存配置之手动清除缓存

    访问我的博客 前言 前文介绍了利用 nginx 的 nginx_ngx_cache_purge 模块来实现缓存功能,并设置了缓存时间为一天. 但是如果前端修改了页面,比如首页,由于 Nginx 缓存的 ...

  8. Java 容器源码分析之HashMap多线程并发问题分析

    并发问题的症状 多线程put后可能导致get死循环 从前我们的Java代码因为一些原因使用了HashMap这个东西,但是当时的程序是单线程的,一切都没有问题.后来,我们的程序性能有问题,所以需要变成多 ...

  9. 字符串匹配的KMP算法-16张图片看明白

    作者: 阮一峰 日期: 2013年5月 1日 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另 ...

  10. 大数据技术之_08_Hive学习_05_Hive实战之谷粒影音(ETL+TopN)+常见错误及解决方案

    第10章 Hive实战之谷粒影音10.1 需求描述10.2 项目10.2.1 数据结构10.2.2 ETL原始数据10.3 准备工作10.3.1 创建表10.3.2 导入ETL后的数据到原始表10.3 ...