首页 > 解决方案 > NGINX 仅缓存特定目录中的图像、css、字体

问题描述

location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico)$ {
     expires 15d;
     add_header Cache-Control "public, no-transform";
}

我有上面的代码,应该将这些文件缓存 15 天。如何修复它,以便仅缓存来自 /js、/css、/img 的文件而不是全部?

标签: nginx

解决方案


将位置规则更改为:

location ~ ^/(js|css|img)/*\.(js|css|png|jpg|jpeg|gif|svg|ico)$ {
     expires 15d;
     add_header Cache-Control "public, no-transform";
}

那应该工作


推荐阅读