Skip to content Skip to main navigation Skip to footer

Linux:Nginx PageSpeed模块配置和使用

Google Pagespeed在作为一个Nginx的模块在研发这么久之后仍是beta版本,着实有点尴尬,不过也证明了该项目是成功的,最起码它仍在迭代,实际上在使用中,我们也碰到一些非模块本身的问题,譬如当它基于反向代理时模板的寻址,图片的URL重写…为此不得不对其做一些配置让它不是看起来多了一个协议,而是真正的能对页面的细节进行优化,最大限度的压榨服务器的的性能提高用户访问速度..

Linux:Nginx PageSpeed模块配置和使用
Linux:Nginx PageSpeed模块配置和使用

在实际使用中,它能够带来什么

    1、优化缓存并整合应用程序的数据和逻辑    2、优化往返请求,降低请求开销,降低响应页面大小    3、对CSS Javascript 进行合并,这里相对来说要慎用(需要前期测试)    4、DNS预加载,延时加载图片等过滤器的支持(赞)    5、等等(少说多做)

 

首先是我的目录结构

    ###其他目录不作解释,source 目录即所有源码包的路径###
    [root@ipython software]# ls /software/
    mysql nginx php readme.txt sharelib source var_temp
    ###库文件的支持###
    [root@ipython software]# ls /software/sharelib/
    curl freetype gd google-libunwind google-perftools libiconv libjpeg libmcrypt libpng libtool libxml2 mhash ncurses openssl pcre zlib
    Nginx版本:
    [root@ipython software]# curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.7.5
    Date: Fri, 07 Nov 2014 14:26:25 GMT
    Content-Type: text/html
    Connection: keep-alive
    Vary: Accept-Encoding

 

下载地址和参考地址(墙内就不建议点了):

ngx-pagespeed for github

[root@ipython source]# wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.9.32.1-beta.zip

 ngx-pagespeed optimization library

[root@ipython source]# wget https://dl.google.com/dl/page-speed/psol/1.9.32.1.tar.gz

ngx-pagespeed official

http://ngxpagespeed.com/ngx_pagespeed_example/

GoogleDevelopers

https://developers.google.com/speed/docs/mod_pagespeed/build_ngx_pagespeed_from_source

 

开始编译Ngx-pagespeed

    ###解开程序包--我在写这篇文章的时候,它似乎已经又更新了...建议参考的同学使用最新版###
    [root@ipython source]# unzip release-1.9.32.1-beta
    [root@ipython source]# cd ngx_pagespeed-release-1.9.32.1-beta/
    ##将pagespeed 的优化库解压至当前目录##
    [root@ipython ngx_pagespeed-release-1.9.32.1-beta]# tar zxf ../1.9.32.1.tar.gz -C ./
    ##此刻你的目录路径应该是这样的##
    [root@ipython ngx_pagespeed-release-1.9.32.1-beta]# ls
    config cpp_feature LICENSE psol README.md scripts src test
    ##重新编译nginx##
    [root@ipython ngx_pagespeed-release-1.9.32.1-beta]# cd ../nginx-1.7.5/
    ###请基于你自己的编译参数加上最后一条 --add-module ,切勿直接复制粘贴,因为我的环境是基于自己弄的一键包来演示的(如果你也需要?)###
    [root@ipython nginx-1.7.5]# ./configure --prefix=/software/nginx --user=nginx --group=nginx --add-module=../ngx_pagespeed-release-1.9.32.1-beta
    ##预配置结束后执行##
    [root@ipython nginx-1.7.5]# make
    ##如果你是全新安装,在预配置结束后make install,如果是重新编译,你可以这样##
    [root@ipython nginx-1.7.5]# mv /software/nginx/sbin/nginx /software/nginx/sbin/nginx.bak
    [root@ipython nginx-1.7.5]# cp objs/nginx /software/nginx/sbin/
    [root@ipython nginx-1.7.5]# kill -USR2 `cat /software/nginx/nginx.pid`
    [root@ipython nginx-1.7.5]# kill -QUIT `cat /software/nginx/nginx.pid.oldbin`

 

让常用的配置开启Pagespeed

    ##注意那条include##
    [root@ipython nginx-1.7.5]# vi /software/nginx/conf/nginx.conf
    server
    {
    listen 80;
    server_name 127.0.0.1;
    include pagespeed.conf;
    ...我省略了好多行
    #新增配置文件,配置后面再解释#
    [root@ipython nginx-1.7.5]# cat /software/nginx/conf/pagespeed.conf
    pagespeed on;
    pagespeed FileCachePath /software/var_temp/nginx/pagespeed/;
    ##重启nginx|reload也可以##
    [root@ipython nginx-1.7.5]# service nginx restart
    Stopping nginx: [ OK ]
    Starting nginx: [1107/230146:INFO:google_message_handler.cc(35)] No threading detected. Own threads: 1 Rewrite, 1 Expensive Rewrite.
    [ OK ]
    ##看看http头 和前面是不是不一样了##
    [root@ipython nginx-1.7.5]# curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.7.5
    Content-Type: text/html
    Connection: keep-alive
    Vary: Accept-Encoding
    Date: Fri, 07 Nov 2014 15:03:29 GMT
    X-Page-Speed: 1.9.32.1-4238
    Cache-Control: max-age=0, no-cache
    ##临时关闭pagespeed##
    [root@ipython nginx-1.7.5]# curl -I 127.0.0.1?ModPagespeed=off
    HTTP/1.1 200 OK
    Server: nginx/1.7.5
    Date: Fri, 07 Nov 2014 15:04:56 GMT
    Content-Type: text/html
    Connection: keep-alive
    Vary: Accept-Encoding

 

一个示例配置文件

    [root@ipython nginx-1.7.5]# cat /software/nginx/conf/pagespeed.conf
    # 启用ngx_pagespeed
    pagespeed on;
    pagespeed FileCachePath /software/var_temp/nginx/pagespeed/;
    # 禁用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;
    # Example 禁止pagespeed 处理/ipython/目录
    pagespeed Disallow "*/ipython/*";

 

来源:http://www.ipython.me/centos/nginx-pagespeed-module-config.html

0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.