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,那么将停止搜索并立即处理此请求 ...
随机推荐
- SQL Server 跨域访问
# SQL Server 跨服务器访问数据 参考链接: [sp_addlinkedserver](https://msdn.microsoft.com/zh-cn/library/ms190479.a ...
- Ehcache的视频-如何用Ehcache提升你的Java应用性能
Java应用最广的缓存(分布式缓存)Ehcache的Youtube介绍视频 (需-翻-墙)
- 【转】获取到元素的 offsetLeft 、offsetTop属性不正常的解决方法。
原地址:http://hi.baidu.com/huidust520/item/85da006981a6c635ad3e834e 我在工作中遇到个问题: 在ie7下和360浏览器下获取到的 offs ...
- JavaScript -- Window-Name
-----027-Window-Name.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=& ...
- Vue + Element UI 实现权限管理系统 前端篇(八):管理应用状态
使用 Vuex 管理应用状态 1. 引入背景 像先前我们是有导航菜单栏收缩和展开功能的,但是因为组件封装的原因,隐藏按钮在头部组件,而导航菜单在导航菜单组件,这样就涉及到了组件收缩状态的共享问题.收缩 ...
- 20-hadoop-pagerank的计算
转: http://www.cnblogs.com/rubinorth/p/5799848.html 参考尚学堂视频 1, 概念( 来自百度百科) PageRank是Google专有的算法,用于衡量特 ...
- springMVC容器和Spring容器
前段时间有人问我,为什么一定要在web.xml中配置spring的listener呢? <listener> <description>spring监听器</descri ...
- Docker快速入门(一)
Docker是Go语言开发实现的容器.2013年发布至今,备受推崇.相关文档.学习资料十分详尽.近期公司docker项目要推进,得重新学习一下.博客以笔记. 1 容器诞生背景及优势 (1)软件开发和运 ...
- mongodb备份策略
概述 数据库的备份非常非常非常重要!!!否则出问题连哭的机会有没有(欲哭无泪)今天主要是做一个mongodb的数据库备份. 1.关于备份 备份其实很简单,这里选择的是对mongodb中的某个库进行全备 ...
- mac 上位Idea 配置Project SDK
问题 刚开始没有Project SDK 方法 添加SDK: /Library/Java/JavaVirtualMachines/jdkXYZ_VERSION.jdk/Contents/Home/jre ...