我遇到了 nginx/pagespeed 模块的问题。首次页面加载时,图像缓存时间默认为 5 分钟。
我的 nginx.conf 中有:
##
# PageSpeed
##
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
pagespeed Domain https://example.com;
pagespeed Domain *.example.com;
pagespeed MapOriginDomain "http://localhost" "https://example.com";
pagespeed LoadFromFile "https://example.com" "/usr/share/nginx/www/";
pagespeed LoadFromFileRuleMatch Disallow \.php$;
pagespeed XHeaderValue "Powered By ngx_pagespeed";
pagespeed ListOutstandingUrlsOnError on;
pagespeed EnableFilters extend_cache;
# image related
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters convert_gif_to_png;
pagespeed EnableFilters convert_jpeg_to_progressive;
pagespeed EnableFilters strip_image_color_profile;
pagespeed EnableFilters strip_image_meta_data;
pagespeed EnableFilters jpeg_subsampling;
pagespeed EnableFilters lazyload_images;
# code related
pagespeed EnableFilters collapse_whitespace;
# DNS related
pagespeed EnableFilters insert_dns_prefetch;
# JS related
pagespeed UseExperimentalJsMinifier on;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters combine_javascript;
# css related
pagespeed EnableFilters rewrite_css;
pagespeed EnableFilters prioritize_critical_css;
pagespeed EnableFilters combine_css;
pagespeed EnableFilters outline_css;
pagespeed EnableFilters flatten_css_imports;
如何修改默认的 5 分钟缓存时间?如何通过 pagespeed 模块在第一次页面加载时传递图像?
这没有帮助:
location ~* \.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
try_files $uri =404;
}
location ~* \.(pdf|js|css|swf)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
try_files $uri =404;
}
答案1
从文档
当 PageSpeed 通过 HTTP 或 HTTPS 获取资源时,它会检查 Expires 和 Cache-Control 标头,以确定更新缓存的频率。当这些标头未指定超时时,将使用 5 分钟的默认超时。要覆盖此设置,请指定:
pagespeed ImplicitCacheTtlMs implicit_cache_ttl_in_milliseconds;