ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升web性能的实践应用到网页和相关的资源(CSS、JS和图片)上,无需你修改内容和流程。 功能包括: 图片优化: stripping meta-data, dynamic resizing, recompression 小资源的内联 延长缓存扩展 HTML 重写 推迟图片和 JS 的加载 CSS & JavaScript 压缩、合并等 查看版本:https://github.com/pagespeed/ngx_pagespeed/releases 一、安装ngx_pagespeed 代码: cd /usr/local/ wget https://github.com/pagespeed/ngx_pagespeed/archive/latest-stable.zip unzip latest-stable.zip cd ngx_pagespeed-latest-stable/ wget https://dl.google.com/dl/page-speed/psol/1.12.34.3.tar.gz tar -xzvf 1.12.34.3.tar.gz 二、编译nginx 见http://www.xf4.org/threads/95203/#post-97911 代码: ./configure --add-module=/usr/local/nginx/modules/ngx_pagespeed-latest-stable make make install 三、配置ngx_pagespeed 代码: mkdir /tmp/ngx_pagespeed_cache chmod -R 777 /tmp/ngx_pagespeed_cache 编辑nginx.conf在server区加入 代码: pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; include pagespeed.conf; include ngx_pagespeed.inc; vi /usr/local/nginx/conf/ngx_pagespeed.inc 代码: location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/ngx_pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { } location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; } location /ngx_pagespeed_message { allow 127.0.0.1; deny all; } location /pagespeed_console { allow 127.0.0.1; deny all; } if ($request_uri ~ "(ngx_pagespeed_([^.]+)/(.*)?)") { break; } vi /usr/local/nginx/conf/pagespeed.conf 代码: pagespeed on; pagespeed UseNativeFetcher on; resolver 114.114.114.114; pagespeed Disallow "*.php*"; pagespeed FetcherTimeoutMs 300; pagespeed RewriteDeadlinePerFlushMs 300; pagespeed FetchWithGzip on; pagespeed ForceCaching on; #needs to exist and be writable by nginx pagespeed FileCachePath /var/cache/nginx/ngx_pagespeed_cache/; pagespeed FileCacheSizeKb 102400; pagespeed FileCacheCleanIntervalMs 360000; pagespeed FileCacheInodeLimit 500000; pagespeed DefaultSharedMemoryCacheKB 50000; pagespeed LRUCacheKbPerProcess 8192; pagespeed LRUCacheByteLimit 16384; pagespeed BlockingRewriteKey psatest; #Rewriting Level pagespeed RewriteLevel CoreFilters; #pagespeed RewriteLevel PassThrough; pagespeed EnableFilters elide_attributes,rewrite_domains; pagespeed EnableFilters rewrite_images,inline_preview_images,lazyload_images; pagespeed EnableFilters resize_mobile_images,sprite_images; pagespeed EnableFilters convert_jpeg_to_webp,convert_to_webp_lossless; pagespeed EnableFilters resize_rendered_image_dimensions; pagespeed EnableFilters insert_image_dimensions; pagespeed EnableFilters dedup_inlined_images; pagespeed EnableFilters canonicalize_javascript_libraries,inline_google_font_css; pagespeed EnableFilters inline_import_to_link,decode_rewritten_urls; pagespeed EnableFilters fallback_rewrite_css_urls,flatten_css_imports; pagespeed EnableFilters inline_css,move_css_above_scripts,move_css_to_head; pagespeed EnableFilters outline_css,combine_css,rewrite_css; #pagespeed EnableFilters prioritize_critical_css; pagespeed EnableFilters rewrite_style_attributes_with_url,rewrite_style_attributes; pagespeed EnableFilters combine_javascript,defer_javascript; pagespeed EnableFilters inline_javascript; pagespeed EnableFilters outline_javascript,rewrite_javascript; pagespeed EnableFilters local_storage_cache,extend_cache; pagespeed EnableFilters combine_heads,insert_ga,trim_urls,collapse_whitespace,remove_comments,remove_quotes,convert_meta_tags,insert_dns_prefetch,make_google_analytics_async,add_head; #pagespeed EnableFilters add_instrumentation; pagespeed EnableFilters pedantic; pagespeed CombineAcrossPaths off; pagespeed LazyloadImagesAfterOnload on; #pagespeed LazyloadImagesBlankUrl "http://www.gstatic.com/psa/static/1.gif"; pagespeed MaxSegmentLength 256; pagespeed CssFlattenMaxBytes 5120; pagespeed CssImageInlineMaxBytes 3000; pagespeed CssInlineMaxBytes 10485760; pagespeed CssOutlineMinBytes 51200; pagespeed MaxCombinedCssBytes -1; pagespeed ImageInlineMaxBytes 10485760; pagespeed ImageLimitOptimizedPercent 100; pagespeed ImageLimitResizeAreaPercent 100; pagespeed ImageRecompressionQuality 75; pagespeed ImageResolutionLimitBytes 32000000; pagespeed JpegRecompressionQuality 80; pagespeed JpegRecompressionQualityForSmallScreens 70; #pagespeed JpegNumProgressiveScans 8; #pagespeed JpegNumProgressiveScansForSmallScreens 10; #pagespeed WebpRecompressionQuality 80; #pagespeed WebpRecompressionQualityForSmallScreens 70; pagespeed JsInlineMaxBytes 10485760; pagespeed JsOutlineMinBytes 51200; pagespeed MaxCombinedJsBytes 10485760; pagespeed MaxInlinedPreviewImagesIndex -1; pagespeed MinImageSizeLowResolutionBytes 3072; pagespeed RetainComment " google_ad_section*"; pagespeed RewriteRandomDropPercentage 0; pagespeed ImageMaxRewritesAtOnce 0; pagespeed ProgressiveJpegMinBytes 5120; pagespeed CriticalImagesBeaconEnabled false; pagespeed AvoidRenamingIntrospectiveJavascript on; #pagespeed AddOptionsToUrls on; #Respecting Vary Headers pagespeed RespectVary on; #Lower-casing HTML element and attribute names pagespeed LowercaseHtmlNames on; #Preserving HTML caching headers pagespeed ModifyCachingHeaders on; #Specifying the value for the PageSpeed header pagespeed XHeaderValue "Powered By Pagespeed"; #Respecting X-Forwarded-Proto pagespeed RespectXForwardedProto on; #pagespeed RunExperiment on; #pagespeed AnalyticsID UA-12345678-1; #pagespeed ExperimentVariable 1; #pagespeed ExperimentSpec "id=1;percent=50;level=CoreFilters;enabled=collapse_whitespace,remove_comments;"; #pagespeed ExperimentSpec "id=2;percent=50;default;"; #let's speed up PageSpeed by storing it in the super duper fast memcached pagespeed MemcachedServers "127.0.0.1:11211"; pagespeed MemcachedTimeoutUs 100000;
升级 代码: ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --add-module=/usr/local/ngx_pagespeed-latest-stable/
提示 代码: configuring additional modules adding module in /usr/local/ngx_pagespeed-latest-stable/ ngx_pagespeed: pagespeed optimization library not found: You need to separately download the pagespeed library: $ cd /usr/local/ngx_pagespeed-latest-stable/ $ wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz $ tar -xzvf 1.13.35.2-x64.tar.gz # expands to psol/ Or see the installation instructions: https://developers.google.com/speed/pagespeed/module/build_ngx_pagespeed_from_source 解决办法: yum install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel 参考:https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source