用于缓存 angular 个应用程序文件的 Nginx 配置
Nginx configuration to cache angular app files
我对 运行 一个 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"
不确定为什么在根路径为 /usr/share/nginx/html
时尝试从 /etc/nginx/html/
获取资源
您需要确保服务器部分中定义的 "root" 符合您的要求。或者在您的正则表达式位置 location ~* \.(jpg|jpeg|png|gif|swf|svg|ico|mp4|eot|ttf|otf|woff|woff2|css|js)$
下定义 "root"。如果没有定义根,nginx 可能会在服务器部分下恢复为全局定义。
我对 运行 一个 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"
不确定为什么在根路径为 /usr/share/nginx/html
/etc/nginx/html/
获取资源
您需要确保服务器部分中定义的 "root" 符合您的要求。或者在您的正则表达式位置 location ~* \.(jpg|jpeg|png|gif|swf|svg|ico|mp4|eot|ttf|otf|woff|woff2|css|js)$
下定义 "root"。如果没有定义根,nginx 可能会在服务器部分下恢复为全局定义。