Nginx配置缓存Angular应用程序文件

4

我有以下配置来运行一个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;
}

但是当我添加缓存时

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";
}

文件不再可访问。
[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"

不确定为什么尝试从/etc/nginx/html/获取资源,而根路径是/usr/share/nginx/html

1个回答

5

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


基本上,在服务器块中定义根,而不是在位置块中定义。 - danday74

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接