Nginx location匹配
nginx 的location 作用,是根据用户访问的url,进行不同的处理方式
针对用户请求的网站url进行匹配,然后进行对应的处理
location在nginx.conf 的写法
location / {
# root关键词 是定义网页根目录,这个html是以nginx安装的路径为相对
root /www/vod;
# index关键词,定义nginx的首页文件名字,默认找index.html文件
index index.html index.htm;
}
location 相关语法
location [ = | ~ | ~* | ^~] url {
# 做出的相应处理
}
# nginx有关location的匹配,符号如下
匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符开头 ,不做任何处理 2
~* 支持正则的匹配模式 3
/blog/ 当访问192.168.1.100/blog/xxx 4
/ 通用匹配,不符合其他location规则,就到这里 5
表示nginx.conf 支持在虚拟主机中定义多个location,进行用户的请求url解析
Nginx 的location实战演练如下:
准备一个nginx的文件,来练习location语法
vim my_location.conf
server {
listen 83;
server_name _;
# 最低级匹配,不符合其他location就来这里
location / {
return 401;
}
# 优先级最高
location = / {
return 402;
}
# 以/blog开头的url 来这里 如符合其他location 则以其他优先
location /blog/ {
return 403;
}
# 匹配任何以/img/开头的请求 ,不匹配则正则
location ^~ /img/ {
return 404;
}
# 匹配任何以.gif 结尾的请求,支持正则
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}