WEB服务与NGINX(6)-location使用详解
1. location的详细用法
location [ = | ~ | ~* | ^~ ] uri { ... }
用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置。
uri前符号说明
- = 对URI做精确匹配;
- ^~对URI的最左边部分做匹配检查,不区分字符大小写
- ~对URI做正则表达式模式匹配,区分字符大小写
- !~ 对URI做正则表达式模式不匹配,区分字符大小写
- ~*对URI做正则表达式模式匹配,不区分字符大小写
- 不带符号匹配起始于此uri的所有的uri
- / 通用匹配。任何请求都会匹配到
- * 表示任意长度的任意字符
匹配优先级从高到低如下:
(location =) > (location 完整路径) > (location ^~ 路径) > (location ~/~* 路径) > (location 部分起始路径) > (/)
1.1 精确匹配
#示例:精确匹配doc目录下的一个文件名。
#1.nginx的配置文件如下:
[root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
	listen 80;
	server_name www.nginx01.com;
	location / {
		root /data/nginx/html/web1;
		index index.html;
	}
	location = /file1.txt {
		root /data/nginx/html/web1/doc;
		index index.html;
	}
}
#2.重启nginx服务
[root@nginx01 web1]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx01 web1]# systemctl reload nginx.service 
#3.新建doc目录和file1.txt文件
[root@nginx01 web1]# mkdir doc
[root@nginx01 web1]# echo "abc" > doc/file1.txt
#4.客户端测试
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.txt
abc
1.2 区分大小写
#1.nginx的配置如下:
[root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
	listen 80;
	server_name www.nginx01.com;
	location / {
		root /data/nginx/html/web1;
		index index.html;
	}
	location ~ /file1\.txt {
		root /data/nginx/html/web1/doc;
		index index.html;
	}
}
#2.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service
#3.在doc下新建文件
[root@nginx01 web1]# echo "qwe" > doc/file1.TXT
[root@nginx01 web1]# tree doc/
doc/
├── file1.txt
└── file1.TXT
#4.客户端如果访问File1.txt或file.TXT,则无法匹配到location ~ /file1.txt语句块
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.txt
abc
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.TXT
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
1.3 不区分大小写
对用户的请求的uri做模糊匹配,不区分大小写,无论是大写,小写还是大小写混合都会匹配,但是需要有对应的资源(大写,小写,大小写混合)才能访问成功。此模式通常用于匹配用户的静态资源。
#1.nginx的配置文件如下:
root@nginx01 web1]# vim /etc/nginx/conf.d/virtualhost.conf
server {
	listen 80;
	server_name www.nginx01.com;
	location / {
		root /data/nginx/html/web1;
		index index.html;
	}
	location ~* /file1\.txt {
		root /data/nginx/html/web1/doc;
		index index.html;
	}
}
#2.doc下的文件如下:
[root@nginx01 web1]# tree doc/
doc/
├── file1.txt
└── file1.TXT
#3.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service
#4.在客户端测试访问file1.txt和file1.TXT
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.TXT
qwe
[root@xuzhichao ~]# curl http://www.nginx01.com/file1.txt
abc
1.4 匹配URI开始
#1.nginx的配置文件如下:
[root@nginx01 ~]# vim /etc/nginx/conf.d/virtualhost.conf
server {
	listen 80;
	server_name www.nginx01.com;
	location / {
		root /data/nginx/html/web1;
		index index.html;
	}
	location ^~ /doc {
		root /data/nginx/html/web1;
		index index.html;
	}
	location /doc1 {
		alias /data/nginx/html/web1;
		index index.html;
	}
}
#2.web1站点的目录结构如下:
[root@nginx01 web1]# cat  /data/nginx/html/web1/index.html
www.nginx01.com
[root@nginx01 web1]# cat  /data/nginx/html/web1/doc/index.html
doc.index
#3.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service
#4.客户端测试
[root@xuzhichao ~]# curl http://www.nginx01.com/doc1/
www.nginx01.com
[root@xuzhichao ~]# curl http://www.nginx01.com/doc/
doc.index
1.5 测试location的优先级
#1.nginx的配置如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/virtualhost.conf
server {
	listen 80;
	server_name www.nginx01.com;
	location = / {
		default_type text/html;
		return 200 'location = /\n';
	}
	location / {
		default_type text/html;
		return 200 'location /\n';
	}
	location /doc/dir1 {
		default_type text/html;
		return 200 'location /doc/dir1\n';
	}
	location ^~ /images {
		default_type text/html;
		return 200 'location ^~ /images\n';
	}
	location /doc {
		default_type text/html;
		return 200 'location /doc\n';
	}
	location ~* \.(jpg|gif|mp4)$ {
		default_type text/html;
		return 200 'location ~* \.(jpg|gif|mp4)\n';
	}
}
#2.重启nginx服务
[root@nginx01 web1]# systemctl reload nginx.service
#3.使用客户端访问不同的uri测试location的优先级
[root@xuzhichao ~]# curl http://www.nginx01.com/
location = /
[root@xuzhichao ~]# curl http://www.nginx01.com/index.html
location /
[root@xuzhichao ~]# curl http://www.nginx01.com/doc
location /doc
[root@xuzhichao ~]# curl http://www.nginx01.com/doc/dir1
location /doc/dir1
[root@xuzhichao ~]# curl http://www.nginx01.com/doc/1.txt
location /doc
[root@xuzhichao ~]# curl http://www.nginx01.com/images/1.jpg
location ^~ /images
[root@xuzhichao ~]# curl http://www.nginx01.com/doc/1.jpg
location ~* \.(jpg|gif|mp4)
[root@xuzhichao ~]# curl http://www.nginx01.com/doc/dir1/1.jpg
location ~* \.(jpg|gif|mp4)
1.6 location的生产使用示例
#1.直接匹配网站根会加速nginx的访问处理
location / {
		......;
}
#2.访问静态资源,不区分大小写
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
		......;
}
#3.精准匹配
location = /nginx_status {
		......;
}
#4.区分大小写,匹配php资源
location ~ \.php$ {
		......;
}
#5.多应用配置
location ~* /app1 {
		......;
}
location ~* /app2 {
		......;
}
WEB服务与NGINX(6)-location使用详解的更多相关文章
- nginx之location配置详解及案例
		语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码, ... 
- nginx与location语法详解
		Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 ^~ 以某个字符串开头 ~ 区分大小写的正则匹配 ~* 不区分大小写的正则匹配 !~ 区分大小写不匹配的正则 !~* 不区分大小 ... 
- nginx 与location语法详解
		Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 ... 
- nginx的location配置详解
		语法规则: location [=|~|~*|^~] /uri/ { … } =开头表示精确匹配 ^~开头表示uri以某个常规字符串开头,理解为匹配url路径即可.nginx不对url做编码,因此请求 ... 
- 【转】nginx服务器安装及配置文件详解
		原文:http://seanlook.com/2015/05/17/nginx-install-and-config/ nginx服务器安装及配置文件详解 nginx在工作中已经有好几个环境在使用了, ... 
- [转帖]nginx服务器安装及配置文件详解
		nginx服务器安装及配置文件详解 http://seanlook.com/2015/05/17/nginx-install-and-config/ 发表于 2015-05-17 | 更新于: 2 ... 
- Nginx安装及配置详解【转】
		nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ... 
- [转帖]Nginx安装及配置详解 From https://www.cnblogs.com/zhouxinfei/p/7862285.html
		Nginx安装及配置详解 nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP ... 
- 【第六课】Nginx常用配置下详解
		目录 Nginx常用配置下详解 1.Nginx虚拟主机 2.部署wordpress开源博客 3.部署discuz开源论坛 4.域名重定向 5.Nginx用户认证 6.Nginx访问日志配置 7.Ngi ... 
- Nginx简介及配置文件详解
		http://blog.csdn.net/hzsunshine/article/details/63687054 一 Nginx简介 Nginx是一款开源代码的高性能HTTP服务器和反向代理服务 ... 
随机推荐
- 【已解决】git reset命令误删本地文件怎么恢复
			执行 git reflog 命令可以看到曾经执行过的操作,还有版本序号. 执行 git reset --hard HEAD@{[填那个序号]} 就可以恢复本地删除的文件了! 
- 【Java】快速排序
			代码: 1 public static void quickSort(int[] arr) { 2 if (arr == null || arr.length < 2) { 3 return; ... 
- https安全性 带给im 消息加密的启发
			大家好,我是蓝胖子,在之前# MYSQL 是如何保证binlog 和redo log同时提交的?这篇文章里,我们可以从mysql的设计中学会如何让两个服务的调用逻辑达到最终一致性,这也是分布式事务实现 ... 
- Python企业面试题2 —— 基础篇
			1. re 的match和search区别? re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. re.search 扫描整个字符串并返 ... 
- #KD-Tree#洛谷 3710 方方方的数据结构
			题目 区间加,区间乘,单点查询,撤销修改 分析 由于可以离线,不妨把下标看成第一维,时间看成第二维,那么修改操作相当于在一个矩形上加或者乘, 不妨把查询的节点看作是二维平面上的点,这样实际上就可以用 ... 
- #线段树分治,线性基,并查集#CF938G Shortest Path Queries
			题目 给出一个连通带权无向图,边有边权,要求支持 \(q\) 个操作: \(x\) \(y\) \(d\) 在原图中加入一条 \(x\) 到 \(y\) 权值为 \(b\) 的边 \(x\) \(y\ ... 
- OpenHarmony装饰指定自定义组件:@BuilderParam装饰器
			当开发者创建了自定义组件,并想对该组件添加特定功能时,例如在自定义组件中添加一个点击跳转操作.若直接在组件内嵌入事件方法,将会导致所有引入该自定义组件的地方均增加了该功能.为解决此问题,ArkUI ... 
- 深入了解 Python MongoDB 操作:排序、删除、更新、结果限制全面解析
			Python MongoDB 排序 对结果进行排序 使用 sort() 方法对结果进行升序或降序排序. sort() 方法接受一个参数用于"字段名",一个参数用于"方向& ... 
- Linux:vscode扩展无法下载,报错:Error while fetching extensions : XHR failed
			在Linux系统上下载安装好vscode以后,发现扩展里面无法下载安装,报错:Error while fetching extensions : XHR failed 解决办法:修改 hosts 文件 ... 
- Python设计模式----2.工厂模式
			工厂方法模式是简单工厂模式的衍生,解决了许多简单工厂模式的问题 首先完全实现'开-闭 原则',实现了可扩展.其次更复杂的层次结构,可以应用于产品结果复杂的场合. 工厂方法模式的对简单工厂模式进行了抽象 ... 
