WeiCN

使用ngx_pagespeed模块给Nginx加速
为了优化一下http/1.1的速度,尝试一下这个模块首先你必须已经升级了gcc,可以看上一篇文章下面来看看怎么用,...
扫描右侧二维码阅读全文
07
2016/04

使用ngx_pagespeed模块给Nginx加速

为了优化一下http/1.1的速度,尝试一下这个模块

首先你必须已经升级了gcc,可以看上一篇文章

下面来看看怎么用,nginx的模块都需要静态编译,我们只有重新编译源码

[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

server { 
    # 侦听的端口 
    listen 80; 
    # 服务器名称 
    server_name weicn.org; 
    # 记下根目录 
    root /usr/local/nginx/html; 
    # 访问日志 
    access_log /var/log/nginx/access.log main; 
    # 启用ngx_pagespeed 
    pagespeed on; 
    # 在此放置一个或多个pagespeed过滤器。
} 

说到指定PageSpeed过滤器,有两种不同的级别可供你选择:CoreFilters和PassThrough。除非有所指定,否则默认情况下使用CoreFilters。

server { 
    # 侦听的端口 
    listen 80; 
    # 服务器名称 
    server_name weicn.org; 
    # 记下根目录 
    root /usr/local/nginx/html; 
    # 访问日志 
    access_log /var/log/nginx/access.log main; 
    # 启用ngx_pagespeed 
    pagespeed on; 
    pagespeed FileCachePath /var/ngx_pagespeed_cache; 
    # 禁用CoreFilters 
    pagespeed RewriteLevel PassThrough; 
    # 启用压缩空白过滤器 
    pagespeed EnableFilters collapse_whitespace; 
    # 启用JavaScript库卸载 
    pagespeed EnableFilters canonicalize_javascript_libraries; 
    # 把多个CSS文件合并成一个CSS文件 
    pagespeed EnableFilters combine_css; 
    # 把多个JavaScript文件合并成一个JavaScript文件 
    pagespeed EnableFilters combine_javascript; 
    # 删除带默认属性的标签 
    pagespeed EnableFilters elide_attributes; 
    # 改善资源的可缓存性 
    pagespeed EnableFilters extend_cache; 
    # 更换被导入文件的@import,精简CSS文件 
    pagespeed EnableFilters flatten_css_imports; 
    pagespeed CssFlattenMaxBytes 5120; 
    # 延时加载客户端看不见的图片 
    pagespeed EnableFilters lazyload_images; 
    # 启用JavaScript缩小机制 
    pagespeed EnableFilters rewrite_javascript; 
    # 启用图片优化机制 
    pagespeed EnableFilters rewrite_images; 
    # 预解析DNS查询 
    pagespeed EnableFilters insert_dns_prefetch; 
    # 重写CSS,首先加载渲染页面的CSS规则 
    pagespeed EnableFilters prioritize_critical_css; 
} 

创建将由Nginx写入的一个文件缓存目录。

[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

贴一下我现在用的规则

        pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;
        pagespeed UseExperimentalJsMinifier on;
        pagespeed RewriteLevel PassThrough;
#        pagespeed EnableFilters local_storage_cache;
        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 defer_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 lazyload_images;
#        pagespeed EnableFilters rewrite_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 EnableFilters prioritize_critical_css;
        pagespeed LoadFromFile "https://weicn.org" "/home/weicn.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网站服务器提速?

Last modification:April 10th, 2016 at 07:54 pm

One comment

  1. 蔓舞寻樱

    mark
    这个好黑科技啊,明明nginx已经很快了~

Leave a Comment