Nginx配置文件之location的匹配解析
server {
listen 89;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 90;
server_name localhost;
location / {
root /home/foodie-shop;
index index.html;
}
}
默认的匹配 “/
”
server {
listen 89;
server_name localhost;
# 默认匹配, 会匹配到 root 指定的路径
location / {
root html;
index index.html index.htm;
}
}
精确的匹配, 使用=
资源路径: /home/beyond/img/face1.png
server {
listen 91;
server_name localhost;
# 精确匹配, 会匹配到 root 指定的路径
location = / {
root html;
index index.html index.htm;
}
# 精确匹配, 只能精准的匹配到 /beyond/img/face1.png
location = /beyond/img/face1.png {
root /home;
}
}
使用正则匹配 ~* 正则
和 ^~
以某个路径开头
* 表示不区分大小写
server {
listen 91;
server_name localhost;
# 正则匹配, 会匹配到 root 指定的路径
location ~* \.(GIF|png|bmp|jpg|jpeg) {
root /home;
}
}
server {
listen 91;
server_name localhost;
# 非正则匹配, 会匹配到 root 指定的路径下的以/beyond/img/路径开头的所有文件
location ^~ /beyond/img {
root /home;
}
}