网站首页 > 技术教程 正文
location的作用
location指令的作用是根据用户请求URI来执行不同的应用,location会根据用户请求网站URL进行匹配定位到某个location区块. 如果匹配成功将会处理location块的规则.
location的语法规则如下
location = /uri
┬ ┬ ┬
│ │ │
│ │ │
│ │ │
│ │ │
│ │ └─────────────── 前缀|正则
│ └──────────────────── 可选的修饰符(用于匹配模式及优先级)
└───────────────────────── 必须
修饰符说明及优先级顺序
优先级表示优先匹配,可理解为编程当中的switch语法.
以下从上到下,第一个优先级最高.
= | location = /uri
^~ | location ^~ /uri
~ | location ~ pattern
~* | location ~* pattern
/uri | location /uri
@ | location @err
1, = ,精确匹配,表示严格相等
location = /static {
default_type text/html;
return 200 "hello world";
}
# http://localhost/static [成功]
# http://localhost/Static [失败]
2, ^~ ,匹配以URI开头
location ^~ /static {
default_type text/html;
return 200 "hello world";
}
# http://localhost/static/1.txt [成功]
# http://localhost/public/1.txt [失败]
3, ~ ,对大小写敏感, 使用正则 注意:某些操作系统对大小写敏感是不生效的,比如windows操作系统
location ~ ^/static$ {
default_type text/html;
return 200 "hello world";
}
# http://localhost/static [成功]
# http://localhost/static?v=1 [成功] 忽略查询字符串
# http://localhost/STATic [失败]
# http://localhost/static/ [失败] 多了斜杠
4, ~* ,与~相反,忽略大小写敏感, 使用正则
location ~* ^/static$ {
default_type text/html;
return 200 "hello world";
}
# http://localhost/static [成功]
# http://localhost/static?v=1 [成功] 忽略查询字符串
# http://localhost/STATic [成功]
# http://localhost/static/ [失败] 多了斜杠
5, /uri ,匹配以/uri开头的地址
location /static {
default_type text/html;
return 200 "hello world";
}
# http://localhost/static [成功]
# http://localhost/STATIC?v=1 [成功]
# http://localhost/static/1 [成功]
# http://localhost/static/1.txt [成功]
6, @ 用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被nginx内部配置指令所访问,比如 try_files or error_page
location / {
root /root/;
error_page 404 @err;
}
location @err {
# 规则
default_type text/html;
return 200 "err";
}
# 如果 http://localhost/1.txt 404,将会跳转到@err并输出err
理解优先级
注意:优先级不分编辑location前后顺序
server {
listen 80;
server_name localhost;
location ~ .*\.(html|htm|gif|jpg|pdf|jpeg|bmp|png|ico|txt|js|css)$ {
default_type text/html;
return 200 "~";
}
location ^~ /static {
default_type text/html;
return 200 "^~";
}
location / {
root /Users/xiejiahe/Documents/nginx/nginx/9000/;
error_page 404 @err;
}
location = /static {
default_type text/html;
return 200 "=";
}
location ~* ^/static$ {
default_type text/html;
return 200 "~*";
}
location @err {
default_type text/html;
return 200 "err";
}
}
- 输出了 = ,因为 = 表示严格相等,而且优先级是最高
- =
- 输出 ^~ , 匹配前缀为 /static
- ^~
The End
猜你喜欢
- 2024-10-12 Nginx配置文件中location的优先级答疑(2)
- 2024-10-12 Nginx的server块和location块的简单说明
- 2024-10-12 nginx location在配置中的优先级(nginx配置location总结及rewrite规则写法)
- 2024-10-12 一分钟搞清楚:Nginx之Location优先级
- 2024-09-25 Nginx无法获取带下划线的请求头数据问题
- 2024-09-25 Nginx性能优化、定位调试、请求与限制、文件操作优化等应用
- 2024-09-25 Nginx学习笔记(15) proxy_pass用法常见误区
- 2024-09-25 Nginx简介以及常见配置(nginx简单配置)
- 2024-09-25 nginx中location的常用配置如下:(nginx中location的作用)
- 2024-09-25 Nginx配置文件中location的优先级答疑
你 发表评论:
欢迎- 最近发表
-
- Win10 TH2正式版官方ESD映像转换ISO镜像方法详解
- 使用iso镜像升级到Windows 10的步骤
- macOS Ventura 13.2 (22D49) Boot ISO 原版可引导镜像
- 安利一个用ISO镜像文件制作引导U盘的的小工具RUFUS
- CentOS 7使用ISO镜像配置本地yum源
- 用于x86平台的安卓9.0 ISO镜像发布下载:通吃I/A/N、完全免费
- AlmaLinux 9.6发布:升级工具、初步支持IBM Power虚拟化技术
- Rufus写入工具简洁介绍与教程(写入模式)
- 新硬件也能安装使用了,Edge版Linux Mint 21.3镜像发布
- 开源工程师:Ubuntu应该抛弃32位ISO镜像
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)