目录
五、Nginx Location
1.前言
通常网站的部分页面,需要特殊设置。比如,/1.html页面,需要用户访问控制(如allow all)
location = /1.txt {
allow all;
}
那部分页面该如何表达呢?
就是位置Location URL {module}.其中URL的表达方式中使用的正则表达式。常会有冲突的情况,请通过下面的实验,了解常见的冲突符号,并掌握其中的优先级。
2.语法规则
location [= | ~ | ~* | !~ | !~* | ^~ ] /uri/ {
module;
module;
}
3.Location优先级
= 大于 ^~ 大于 ~ | ~* | !~ | !~* 大于 /
精确匹配 > 字符开头 > 正则匹配 > 通配符
4.Location示例
目的:通过不同的表达式,观察表达式间的优先级
systemctl stop firewalld setenforce 0 cd /usr/share/nginx/html/ touch a.html b.html c.html echo a > a.html echo b > b.html echo c > c.html vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; root /usr/share/nginx/html; location =/ {index a.html;} //测试完a页面要把这里注释掉再测试b,测试完b要把b也注释掉 location ~/ {index b.html;} location / {index c.html;} #access_log /var/log/nginx/host.access.log main; #location / { #root /usr/share/nginx/html; #index index.html index.htm; #} //这一段loaction要注释掉 systemctl restart nginx
访问页面。观察显示的内容。理解正则符号间的优先级。