ngx_pagespeed作为google开发的nginx第三方模块通过各种过滤指令可以自动的优化网站。这篇文章贴出自用的配置。具体的模块介绍和每个“过滤器”的用法解释,参考官网的文档
(https://www.modpagespeed.com/doc/ 和示例说明(https://www.modpagespeed.com/examples/)
pagespeed on; pagespeed HttpCacheCompressionLevel 9; pagespeed PrivateNotVaryForIE off; pagespeed EnableFilters in_place_optimize_for_browser; pagespeed RespectVary on; pagespeed EnableFilters make_google_analytics_async; pagespeed LowercaseHtmlNames on; pagespeed LazyloadImagesAfterOnload on; pagespeed EnableFilters inline_preview_images; pagespeed EnableFilters resize_mobile_images; #pagespeed RedisServer "localhost"; pagespeed EnableFilters hint_preload_subresources; pagespeed ReportUnloadTime on; pagespeed FetchHttps enable; pagespeed LoadFromFile "https://chaihongjun.me/public/uploads/" "/alidata1/www/web/chaihongjun.me/public/uploads/"; pagespeed LoadFromFile "https://chaihongjun.me/public/static/" "/alidata1/www/web/chaihongjun.me/public/static/"; pagespeed SslCertDirectory /usr/local/nginx/conf/ssl/chaihongjun.me; pagespeed SslCertFile /usr/local/nginx/conf/ssl/chaihongjun.me/fullchain.cer; pagespeed Disallow "*/response/*"; pagespeed DisableFilters defer_javascript; pagespeed FileCachePath /dev/shm/ngx_pagespeed_cache/chaihongjun.me; pagespeed FileCacheSizeKb 307200; pagespeed FileCacheCleanIntervalMs 3600000; pagespeed FileCacheInodeLimit 500000; pagespeed MemcachedThreads 1; pagespeed MemcachedServers "127.0.0.1:11211"; #pagespeed RedisServer "127.0.0.1:6379"; pagespeed EnableFilters prioritize_critical_css; pagespeed EnableFilters flatten_css_imports; pagespeed CssFlattenMaxBytes 5120; pagespeed EnableFilters add_head; pagespeed DisableFilters add_instrumentation; pagespeed EnableFilters combine_heads; pagespeed EnableFilters convert_meta_tags; pagespeed EnableFilters dedup_inlined_images; pagespeed EnableFilters inline_images; pagespeed EnableFilters recompress_images; pagespeed EnableFilters jpeg_subsampling; pagespeed EnableFilters convert_to_webp_animated; pagespeed EnableFilters convert_gif_to_png; pagespeed EnableFilters convert_png_to_jpeg; pagespeed EnableFilters convert_jpeg_to_progressive; pagespeed EnableFilters convert_jpeg_to_webp; pagespeed EnableFilters convert_to_webp_lossless; pagespeed EnableFilters inline_import_to_link; pagespeed EnableFilters local_storage_cache; pagespeed EnableFilters responsive_images,resize_images; pagespeed ImageResolutionLimitBytes 128000000; pagespeed EnableFilters move_css_above_scripts; pagespeed EnableFilters move_css_to_head; pagespeed EnableFilters remove_quotes; pagespeed RewriteLevel PassThrough; pagespeed EnableFilters collapse_whitespace; pagespeed EnableFilters canonicalize_javascript_libraries; pagespeed EnableFilters combine_css; pagespeed EnableFilters combine_javascript; pagespeed EnableFilters elide_attributes; pagespeed EnableFilters extend_cache; pagespeed EnableFilters lazyload_images; pagespeed EnableFilters responsive_images_zoom; pagespeed EnableFilters rewrite_css,sprite_images,rewrite_style_attributes_with_url; pagespeed EnableFilters trim_urls; pagespeed EnableFilters rewrite_javascript; pagespeed UseExperimentalJsMinifier on; pagespeed EnableFilters rewrite_images; pagespeed EnableFilters strip_image_meta_data; pagespeed EnableFilters insert_dns_prefetch; pagespeed EnableFilters remove_comments; pagespeed RetainComment " make*"; pagespeed Disallow "*/lbs/*"; pagespeed XHeaderValue "Proudly Presented by Google"; pagespeed PreserveUrlRelativity off; pagespeed Statistics on; pagespeed StatisticsLogging on; pagespeed LogDir /alidata1/pagespeed_logs; pagespeed StatisticsPath /ngx_pagespeed_statistics; pagespeed MessagesPath /ngx_pagespeed_message; pagespeed ConsolePath /pagespeed_console; pagespeed AdminPath /pagespeed_admin; pagespeed EnableCachePurge on; pagespeed PurgeMethod PURGE; location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { } location /ngx_pagespeed_statistics { } location /ngx_pagespeed_message { } location /pagespeed_console { } location ~ ^/pagespeed_admin { }
查看缓存情况以及管理缓存的入口:
你的网站域名/pagespeed_admin