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,那么将停止搜索并立即处理此请求 ...
随机推荐
- 01-01java概述 doc命令、jdk\jre下载安装、path、classpath配置、开发中常见小问题
1:计算机概述(了解) (1)计算机 (2)计算机硬件 (3)计算机软件 系统软件:window,linux,mac 应用软件:qq,yy,飞秋 (4)软件开发(理解) 软件:是由数据和指令组成的.( ...
- centos7.2 部署zabbix 3.2.7
centos7.2 部署zabbix 3.2.7[zabbix@zabbixServer ~]$ cat /etc/redhat-release CentOS Linux release 7.2.15 ...
- opencv实现canopy算法
#include "stdafx.h" using namespace cv; int main(int argc, char** argv) { Mat img=imread(& ...
- Java总结:字符串详解
更新时间:2018-1-6 21:20:39 更多请查看在线文集:http://android.52fhy.com/java/index.html String 字符串创建 String str1=& ...
- canvas实现涂鸦板
实现思路:监听鼠标按下.移动.松开事件,将鼠标按下的值赋值给moveTo的x和y值,作为起始位置.在移动事件中,将鼠标距离可视区x和y值赋给lineTo,再将路径闭合.以下是具体的代码 <!DO ...
- Linux-(inotify-tools&rsync)
inotifywait命令 mac中的是:fswatch,fsevents-tools. 1.命令格式: inotifywait [参数] [events] [targetDir] 2.命令功能: 平 ...
- 谈谈AsmJit
0x01 基本介绍 AsmJit是一个完整的JIT(just In Time, 运行时刻)的针对C++语言的汇编器,可以生成兼容x86和x64架构的原生代码,不仅支持整个x86/x64的指令集(包括 ...
- GOROOT、GOPATH和project目录说明
go env环境查看 用go env 可查看当前go环境变量. $ go env GOARCH="amd64" GOBIN="" GOEXE="&qu ...
- 描述linux系统从开机到登陆界面的启动过程
简述:1.开机BIOS自检2.MBR引导3.grub引导菜单4.加载内核kernel5.启动init进程6.读取inittab文件,执行rc.sysinit,rc等脚本7.启动mingetty,进入系 ...
- C#Redis集合set
快过年了,任务也没那么多了,可以有时间了解下其他的内容,今天看到一个博客关于weex的,觉得还挺实用的,等有空了可以了解了解.不过还是把今年的目标要完成.今天继续redis. 一.前戏 在Redis中 ...