让Nginx路径中的子目录匹配文件夹的另一种写法

其实相当于对路径做一种通配符,根据路径名访问相应的文件夹。直接看高潮部分如下。。

    location /static {
        root /var/www/usmt;
        index index.html board.html 03-multiple-grids.html;
        set $real_script '';
        if ( $fastcgi_script_name ~ /static(.*) ){  # 根据location的匹配规则,$1不会为空
            set $real_script $1;
        }
        # 访问时会把截取的地址拼接上location的字符串
        try_files $real_script $real_script/;
    }

访问static时,返回static文件夹的内容;访问staticxxx文件夹时,返回staticxxx文件夹下的内容。

原文地址:https://www.cnblogs.com/dylanchu/p/10268499.html