Nginx的location剖析
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剖析的更多相关文章
- 菜鸟nginx源码剖析
菜鸟nginx源码剖析 配置与部署篇(一) 手把手配置nginx "I love you" TCMalloc 对MYSQL 性能 优化的分析 菜鸟nginx源码剖析系列文章解读 A ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...
- Nginx之location 匹配规则详解
有些童鞋的误区 1. location 的匹配顺序是“先匹配正则,再匹配普通”. 矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”.我这么说,大家一定会反驳我,因为按“先匹配普通, ...
- nginx 中location和root
nginx 中location和root,你确定真的明白他们关系? 2016-01-17 14:48 3774人阅读 评论(1) 收藏 举报 分类: linux(17) 版权声明:本文为博主原创文 ...
- Nginx 的 Location 配置指令块
最近一段时间在学习 Nginx ,以前一直对 Nginx 的 Location 配置很头大,最近终于弄出点眉目.总结如下:nginx 配置文件,自下到上分为三种层次分明的结构: | http b ...
- 菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)
俗话说的好,牵牛要牵牛鼻子 驾车顶牛,处理复杂的东西,只要抓住重点,才能理清脉络,不至于深陷其中,不能自拔.对复杂的nginx而言,main函数就是“牛之鼻”,只要能理清main函数,就一定能理解其中 ...
- 快速掌握Nginx(二) —— Nginx的Location和Rewrite
1 location详解 1.location匹配规则 Nginx中location的作用是根据Url来决定怎么处理用户请求(转发请求给其他服务器处理或者查找本地文件进行处理).location支持正 ...
- nginx之location的匹配规则
nginx之location的匹配规则 一.语法规则 location [=|~|~*|^~] /uri/ { - } 符号 含义 = 开头表示精确匹配 ^~ 开头表示 uri 以某个常规字符串开头 ...
- Nginx的location匹配规则
一 Nginx的location语法 location [=|~|~*|^~] /uri/ { … } = 严格匹配.如果请求匹配这个location,那么将停止搜索并立即处理此请求 ...
随机推荐
- MySQL笔记(3)---文件
1.前言 第二章简单记录了一下InnoDB存储引擎的一个基本内容,介绍了保证高效插入的Insert Buffer,change Buffer和确保数据安全的write ahead log以及doubl ...
- charles重复发包工具/repeat
重复发包工具/repeat Charles 让你选择一个请求并重复,在测试后端接口的时候非常有用: Charles将请求重新发送到服务器,并将响应显示为新请求. 如果您进行后端更改并希望测试它们,用了 ...
- oc中的枚举
如果一个变量只有几种可能的值,比如星期有几天,一年有几个季节等.这个时候可以用枚举变量. 先定义类型再定义变量,如:enum siji{chun,xia,qiu,dong} 也可以定义匿名:enum{ ...
- Android H5调起原生微信或支付宝支付
Android H5调起原生微信或支付宝支付 WebView调用原生微信或支付宝回调:其原理就是在shouldOverrideUrlLoading(final WebView view, String ...
- p132代码解析
1. long before = System.currentTimeMillis(); long after = System.currentTimeMillis(); //该两句代码规定了一个do ...
- 浅尝Vue.js组件(一)
本篇目录: 组件名 组件注册 全局注册 基础组件的自动化全局注册 局部注册 在模块系统中局部注册 Prop 单向数据流 Prop验证 类型检查 非Prop特性 替换/合并已有的特性 禁用特性继承 自定 ...
- 如何设置httpd-mpm-conf的参数
原文链接:http://blog.sina.com.cn/s/blog_626998030102wohs.html 首先确定apache是使用哪种工作模式是prefork模式还是worker模式查看方 ...
- C#中将String类型保存到本地文件的方法
今天刚刚遇到,要在WinForm中把一个xml保存到本地, 由于之前是学习java的,一时间还真不知道怎么写, 没想到C#居然那么方便,就3句代码就实现了我要的功能: StreamWriter sw ...
- Restful API 设计参考原则
在项目中,需要为后台服务撰写API.刚开始接触的时候,并没有考虑太多,就想提供URL,服务端通过该URL进行查询.创建.更新等操作即可.但再对相关规范进行了解后,才发现,API的设计并没有那么简单,远 ...
- 了解MySQL联表查询中的驱动表,优化查询,以小表驱动大表
一.为什么要用小表驱动大表 1.驱动表的定义 当进行多表连接查询时, [驱动表] 的定义为: 1)指定了联接条件时,满足查询条件的记录行数少的表为[驱动表] 2)未指定联接条件时,行数少的表为[驱动表 ...