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 / 下面的配置,

        注意:匹配的時候是從端口后面開始匹配的,兩個的是這樣,多個的也類似


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM