用于缓存角度应用程序文件的 Nginx 配置

Mus*_*ela 4 nginx cache-control angularjs

我有以下配置来运行 angular.js 应用程序,它工作正常。

location / {
    root   /usr/share/nginx/html;
    index  index.html;
    expires -1;
    add_header Pragma "no-cache";
    add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    try_files $uri $uri/ /index.html =404;
}
Run Code Online (Sandbox Code Playgroud)

但是当我添加缓存时

location ~* \.(jpg|jpeg|png|gif|swf|svg|ico|mp4|eot|ttf|otf|woff|woff2|css|js)$ {
  add_header Cache-Control "max-age=86400, must-revalidate, s-maxage=2592000";
}
Run Code Online (Sandbox Code Playgroud)

文件不再可访问

[error] 5#5: *1 open() "/etc/nginx/html/scripts/vendor-1568b32f3e.js" failed (2: No such file or directory), client: 87.63.75.163, server: localhost, request: "GET /scripts/vendor-1568b32f3e.js HTTP/1.1", host: "myapp", referrer: "http://myapp/auth/login"
Run Code Online (Sandbox Code Playgroud)

不知道为什么/etc/nginx/html/在根路径为/usr/share/nginx/html

Sea*_*ean 5

您需要确保服务器部分中定义的“根”与您想要的匹配。或者在您的正则表达式 location 下定义“root” location ~* \.(jpg|jpeg|png|gif|swf|svg|ico|mp4|eot|ttf|otf|woff|woff2|css|js)$。如果没有定义根,nginx 可能会恢复到服务器部分下的全局定义。