沙滩星空的博客沙滩星空的博客

nginx配置静态资源目录映射

root 和 alias

  • 使用 alias 时,目录名后面一定要加斜杠 /
  • alias只能位于 location 块中。

静态资源路径: /var/www/static/

alias 方式:

location /static/ {
    alias  /var/www/static/;
}

alias语句中, /var/www/static/ 不能改成 /var/www/static

root 方式:

location /static/ {
    root  /var/www/;
}

root  /var/www/;
location /static/ {
    expires 30d;
    access_log off;
}

location ^~ /apidoc/ {
    index index.php index.html error/index.html;
    root  "D:/phpstudy_pro/WWW/football_test";
}

location 语句块

修饰符

= 完全精确匹配。包括左右斜杠,必须完全匹配。
^~ 前缀匹配
~ 区分大小写的正则匹配
~* 不区分大小写的正则匹配

顺序及优先级

完全精确匹配 > ^~ 前缀匹配 > 正则匹配

示例

location /static 匹配的URL为 /static 和 /static/
location /static/ 匹配URL /static/

server { 
   server_name website.com;   
   location ~ ^/abcd$ { 
   […]    
    }
}

访问 apple-app-site-association 结尾的 url

  location ~* apple-app-site-association$ {
    default_type application/json;
    alias /wwwroot/youproject/public/apple-app-site-association;
  }

直接返回文本

    location / {
            default_type    text/plain;
            return 502 "服务正在升级,请稍后再试……";
    }

    location / {
            default_type    text/html;
            return 502 "服务正在升级,请稍后再试……";
    }

    location / {
            default_type    application/json;
            return 502 '{"status":502,"msg":"服务正在升级,请稍后再试……"}';
    }

若想直接响指定文件的内容,则不需要使用 return 语句。


关于配置通用链接Universal Link 及 怎么理解通用链接Universal Link https://www.jianshu.com/p/e2fcbb61c71c
nginx配置静态资源目录映射 http://www.mesoftware.cn/article/2c9008d06c609716016cadb6a90e075a
彻底弄懂 Nginx location 匹配 https://www.jianshu.com/p/973ea153ccb9

未经允许不得转载:沙滩星空的博客 » nginx配置静态资源目录映射

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址