Nginx 多个 location 的匹配规则解释
背景
记录一下配置时候的坑,想做到如下的目的。对默认的 index.html 不做任何的处理,只显示,但对下面的分享文件夹做目录美化,让他更好看,

配置如下
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /share {
root /usr/share/nginx/html;
index index.html index.htm;
charset utf-8;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
add_after_body /autoindex/footer.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
charset utf-8;
}
}
有两个 location 那我访问的时候采用哪一个配置。一句话,采用匹配字符最多的,举个例子
http://ip:端口/share/ 能匹配到/share, 那么肯定是采用了 location /share 下面的配置,
http://ip:端口/image/ 那就只匹配上了一个/ 自然采用 location / 下面的配置,
http://ip:端口/share/算法书籍 还是采用了 location /share 下面的配置,
http://ip:端口/logo.png 还是只能只匹配上了一个/ 自然采用 location / 下面的配置,
注意:匹配的时候是从端口后面开始匹配的,两个的是这样,多个的也类似
浙公网安备 33010602011771号