为了优化一下 http/1.1 的速度,尝试一下这个模块
首先你必须已经升级了 gcc,可以看上一篇文章
下面来看看怎么用,nginx 的模块都需要静态编译,我们只有重新编译源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| [root@centos soft]# cd /tmp/soft [root@centos soft]# NPS_VERSION=1.11.33.0 [root@centos soft]# wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip -O release-${NPS_VERSION}-beta.zip [root@centos soft]# unzip release-${NPS_VERSION}-beta.zip [root@centos ngx_pagespeed-release-1.11.33.0-beta]# cd ngx_pagespeed-release-${NPS_VERSION}-beta/ [root@centos ngx_pagespeed-release-1.11.33.0-beta]# wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz #这是谷歌的PageSpeed优化库 [root@centos ngx_pagespeed-release-1.11.33.0-beta]# tar -xzvf ${NPS_VERSION}.tar.gz # extracts to psol/ [root@centos ngx_pagespeed-release-1.11.33.0-beta]# /usr/local/nginx/sbin/nginx -V nginx version: weicn-server/1.0 built by gcc 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC) built with OpenSSL 1.0.2g 1 Mar 2016 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-openssl=/tmp/soft/openssl-1.0.2g --with-http_sub_module --with-http_stub_status_module --with-pcre --with-pcre=/tmp/soft/pcre-8.33 --with-zlib=/tmp/soft/zlib-1.2.8 --with-http_secure_link_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-ld-opt=-ljemalloc --add-module=/tmp/soft/nginx-ct-1.0.0 [root@centos ngx_pagespeed-release-1.11.33.0-beta]# cd /tmp/soft/nginx-1.9.13 [root@centos nginx-1.9.13]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-openssl=/tmp/soft/openssl-1.0.2g --with-http_sub_module --with-http_stub_status_module --with-pcre --with-pcre=/tmp/soft/pcre-8.33 --with-zlib=/tmp/soft/zlib-1.2.8 --with-http_secure_link_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-ld-opt=-ljemalloc --add-module=/tmp/soft/nginx-ct-1.0.0 --add-module=/tmp/soct/ngx_pagespeed-release-${NPS_VERSION}-beta [root@centos nginx-1.9.13]# make [root@centos nginx-1.9.13]# mv /usr/local/nginx/sbin/nginx{,_`date +%F`} [root@centos nginx-1.9.13]# cp objs/nginx /usr/local/nginx/sbin
|
然后配置 conf
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { listen 80; server_name cuojue.org; root /usr/local/nginx/html; access_log /var/log/nginx/access.log main; pagespeed on; }
|
说到指定 PageSpeed 过滤器,有两种不同的级别可供你选择:CoreFilters 和 PassThrough。除非有所指定,否则默认情况下使用 CoreFilters。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| server { listen 80; server_name cuojue.org; root /usr/local/nginx/html; access_log /var/log/nginx/access.log main; pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; 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 flatten_css_imports; pagespeed CssFlattenMaxBytes 5120; pagespeed EnableFilters lazyload_images; pagespeed EnableFilters rewrite_javascript; pagespeed EnableFilters rewrite_images; pagespeed EnableFilters insert_dns_prefetch; pagespeed EnableFilters prioritize_critical_css; }
|
创建将由 Nginx 写入的一个文件缓存目录。
1 2
| [root@centos nginx-1.9.13]# mkdir /var/ngx_pagespeed_cache [root@centos nginx-1.9.13]# chown www:www /var/ngx_pagespeed_cache
|
好了,重启一下 Nginx
贴一下我现在用的规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; pagespeed UseExperimentalJsMinifier on; pagespeed RewriteLevel PassThrough;
pagespeed EnableFilters collapse_whitespace,remove_comments; pagespeed EnableFilters outline_css; pagespeed EnableFilters flatten_css_imports; pagespeed EnableFilters move_css_above_scripts; pagespeed EnableFilters move_css_to_head; pagespeed EnableFilters outline_javascript; pagespeed EnableFilters combine_javascript; pagespeed EnableFilters inline_javascript;
pagespeed EnableFilters combine_css; pagespeed EnableFilters rewrite_javascript; pagespeed EnableFilters rewrite_css,sprite_images; pagespeed EnableFilters rewrite_style_attributes; pagespeed EnableFilters recompress_images;
pagespeed EnableFilters convert_meta_tags; pagespeed EnableFilters canonicalize_javascript_libraries; pagespeed EnableFilters elide_attributes; pagespeed EnableFilters extend_cache; pagespeed EnableFilters insert_dns_prefetch;
pagespeed LoadFromFile "https://cuojue.org" "/home/cuojue.org/"; pagespeed EnableFilters pedantic; pagespeed MemcachedServers "127.0.0.1:11211"; pagespeed MaxSegmentLength 250; location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" "";} location ~ "^/pagespeed_static/" {} location ~ "^/ngx_pagespeed_beacon$" {} location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; } location /ngx_pagespeed_global_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; } location ~ ^/pagespeed_admin { allow 127.0.0.1; deny all; } location ~ ^/pagespeed_global_admin { allow 127.0.0.1; deny all; } if ($request_uri ~ "(pagespeed_([^.]+)/(.*)?)") { break; } if ($request_uri ~ "(ngx_pagespeed_([^.]+)/(.*)?)") { break; }
|
参考列表:
Build ngx_pagespeed From Source
如何借助 PageSpeed,为 Nginx 网站服务器提速?