如何在nginx上启用浏览器缓存?

问题描述 投票:0回答:1

[我尝试使用PageSpeed Insights分析网络速度,而Google告诉我我需要启用缓存。

所以我添加到了.htaccess:

<IfModule mod_expires.c>
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|svg|webp|js|css|swf|gz|json)$">
ExpiresActive On
ExpiresDefault A604800
</FilesMatch>
</IfModule>

<IfModule mod_headers.c>
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|svg|webp|js|css|swf|gz|json)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
</IfModule>

或:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access 14 days"
ExpiresByType text/css "access 7 days"
ExpiresByType text/html "access 2 days"
ExpiresByType image/png "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType text/x-javascript "access 1 year"
ExpiresByType application/pdf "access 1 week"
ExpiresByType image/x-icon "access 2 months"
ExpiresByType application/x-shockwave-flash "access 7 months"
</IfModule>

或:

<IfModule mod_expires.c>
<Filesmatch "\.(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$">
  ExpiresActive on
  ExpiresDefault "access plus 30 days"
</Filesmatch>
</IfModule>

但是他们没有帮助。


我也尝试使用plesk其他nginx指令:

location ~* \.(?:jpg|jpeg|gif|png|webp|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|js|css|json)$ {
  etag on;
  expires 1M;
  if_modified_since exact;
  add_header Pragma "public";
  add_header Cache-Control "public, max-age=86400,no-transform";
}

并且Chrome DevTools网络显示:

Chrome DevTools Network

但是PageSpeed总是告诉“ 使用有效的缓存策略服务静态资产”。


为什么PageSpeed无法获得设置?缓存不起作用?如何启用它?

.htaccess nginx caching browser-cache pagespeed
1个回答
0
投票

因为您在nginx .htaccess上不起作用,所以您需要使用显示的'plesk Additional nginx指令'部分。

© www.soinside.com 2019 - 2024. All rights reserved.