作为Nginx组件,ngx_pagespeed将重写你的网页,让用户以更快的速度进行访问。重写的工作包括压缩图片、缩减CSS和JavaScript、扩展缓存时间,同样还包括其它一些最佳实践:
- 优化缓存——整合应用程序的数据和逻辑
- 最小化round-trip次数——削减连续的请求/响应周期数
- 最小化请求开销——削减上传大小
- 最小化负载大小——削减响应、下载及缓存页面大小
- 优化浏览器渲染——改善浏览器页面布局
- 移动方面的优化——优化站点移动网络和设备方面的相关特性
本文是基于LNMP环境,LNMP脚本参考:《LNMP最新源码安装脚本》
问题反馈请加QQ群: 235258658
下载解压PageSpeed模块
- cd /root/lnmp/src
- wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.6.29.5-beta.zip
- unzip -q release-1.6.29.5-beta
- wget https://dl.google.com/dl/page-speed/psol/1.6.29.5.tar.gz
- tar xzf 1.6.29.5.tar.gz -C ngx_pagespeed-release-1.6.29.5-beta
查看线上版本编译参数
- # /usr/local/nginx/sbin/nginx -V
- nginx version: nginx/1.4.1
- built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)
- TLS SNI support enabled
- configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
增加ngx_pagespeed模块重新编译
- # wget http://nginx.org/download/nginx-1.4.2.tar.gz
- # tar -xvzf nginx-1.4.2.tar.gz
- # cd nginx-1.4.2/
- # ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_pagespeed-release-1.6.29.5-beta/
- # make //注意只make 不要make install (如果报错请看下面)
安装ngx_pagespeed (make) 32位操作系统可能会报如下错误
- ../ngx_pagespeed-release-1.6.29.5-beta/psol/lib/Release/linux/ia32/pagespeed_automatic.a(104.system.o.o): In function `cv::tempfile(char const*)':
- system.cpp:(.text._ZN2cv8tempfileEPKc+0x1e): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
- objs/addon/src/ngx_base_fetch.o: In function `net_instaweb::NgxBaseFetch::DecrefAndDeleteIfUnreferenced()':
- /root/lnmp/src/nginx-1.4.2/../ngx_pagespeed-release-1.6.29.5-beta/src/ngx_base_fetch.cc:185: undefined reference to `__sync_add_and_fetch_4'
- collect2: ld returned 1 exit status
- make[1]: *** [objs/nginx] Error 1
- make[1]: Leaving directory `/root/lnmp/src/nginx-1.4.2'
- make: *** [build] Error 2
解决方法
./configure 后面加上--with-cc-opt='-DLINUX=2 -D_REENTRANT -D_LARGEFILE64_SOURCE -march=i686 -pthread' ,如下:
- # make clean
- # ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_pagespeed-release-1.6.29.5-beta \
- --with-cc-opt='-DLINUX=2 -D_REENTRANT -D_LARGEFILE64_SOURCE -march=i686 -pthread'
- # make
- # mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx$(date +%m%d) #备份nginx原文件
- # cp objs/nginx /usr/local/nginx/sbin/nginx #复制编译make后的文件覆盖nginx原文件
- # /usr/local/nginx/sbin/nginx -t #检测nginx配置文件语法是否正确
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- # kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` #把nginx.pid改成nginx.pid.oldbin跟着启动新的nginx
- # kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` #退出旧的nignx
使用ngx_pagespeed
- # mkdir /var/ngx_pagespeed_cache
- # chown www.www /var/ngx_pagespeed_cache
- # cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf$(date +%m%d) #备份nginx配置文件
- # vi /usr/local/nginx/conf/nginx.conf
- server {
- #省略
- pagespeed on;
- pagespeed FileCachePath /var/ngx_pagespeed_cache;
- pagespeed RewriteLevel CoreFilters;
- 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 combine_css;
- pagespeed EnableFilters rewrite_javascript;
- pagespeed EnableFilters rewrite_css,sprite_images;
- pagespeed EnableFilters rewrite_style_attributes;
- pagespeed EnableFilters recompress_images;
- pagespeed EnableFilters resize_images;
- pagespeed EnableFilters convert_meta_tags;
- 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; }
- #省略
- }
- # /usr/local/nginx/sbin/nginx -t
- Setting option from ("on")
- Setting option from ("FileCachePath", "/var/ngx_pagespeed_cache")
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- # service nginx restart
- Stopping nginx: [ OK ]
- Starting nginx: Setting option from ("on")
- Setting option from ("FileCachePath", "/var/ngx_pagespeed_cache") [ OK ]
- # curl -I 'http://www.linuxeye.com/' | grep X-Page-Speed
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0
- X-Page-Speed: 1.4.0.0-2748
注意:
如果discuz使用了伪静态要注销掉下面部分(前面加#注销):
- #if (!-e $request_filename) {
- # return 404;
- #}
使用后效果如下:
使用PageSpeed模块效果演示网站:http://www.linuxeye.com
参考:
https://developers.google.com/speed/pagespeed/ngx
https://github.com/pagespeed/ngx_pagespeed
Sun Apr 28 11:41:05 CST 2013
1F
请问怎样安装PageSpeed模块 谢谢!
B1
@ zhang 建议不要安装pagespeed,触发bug容易导致cpu 100%,不稳定。如需要安装参考:https://linuxeye.com/318.html
B2
@ yeho 请问出发了BUG导致CPU100%具体表现是什么情况?是nginx占用CPU100%还是PHP进程或其他?
B3
@ Qa nginx
2F
/root/ngx_pagespeed-release-1.5.27.3-beta/src/ngx_fetch.cc:167: error: ‘struct ngx_resolver_ctx_s’ has no member named ‘type’
/root/ngx_pagespeed-release-1.5.27.3-beta/src/ngx_fetch.cc: In static member function ‘static void net_instaweb::NgxFetch::NgxFetchResolveDone(ngx_resolver_ctx_t*)’:
/root/ngx_pagespeed-release-1.5.27.3-beta/src/ngx_fetch.cc:306: error: cannot convert ‘ngx_addr_t’ to ‘in_addr_t’ in assignment
make[1]: *** [objs/addon/src/ngx_fetch.o] Error 1
make[1]: Leaving directory `/soft/nginx-1.6.3′
make: *** [build] Error 2
================
重新编译执行make的结果,不知道是哪里出错了
B1
@ teddy 版本不兼容吧,很久不搞这个玩意
3F
好像最新版开始对内存有要求了!1G内存的好像就不能编译了!无语!
B1
@ 玉满斋 PageSpeed?
B2
@ yeho 博主你好,用源码编译的话,好像64bit操作系统不能直接那样用,要./configure –add-module=$HOME/ngx_pagespeed-${NPS_VERSION} ${PS_NGX_EXTRA_FLAGS}这样子,NPS_VERSION是版本号,但是后面那个PS_NGX_EXTRA_FLAGS谷歌网站并没有提及……这个是什么呢?