Nginx下安装配置PageSpeed模块,轻松完成网站提速

青春壹個敷衍的年華 提交于 2019-11-30 21:04:36

作为Nginx组件,ngx_pagespeed将重写你的网页,让用户以更快的速度进行访问。重写的工作包括压缩图片、缩减CSS和JavaScript、扩展缓存时间,同样还包括其它一些最佳实践:

  • 优化缓存——整合应用程序的数据和逻辑
  • 最小化round-trip次数——削减连续的请求/响应周期数
  • 最小化请求开销——削减上传大小
  • 最小化负载大小——削减响应、下载及缓存页面大小
  • 优化浏览器渲染——改善浏览器页面布局
  • 移动方面的优化——优化站点移动网络和设备方面的相关特性

本文是基于LNMP环境,LNMP脚本参考:《LNMP最新源码安装脚本》
下载解压PageSpeed模块


cd /root/lnmp/source wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.5.27.3-beta.zip
unzip -q release-1.5.27.3-beta
wget https://dl.google.com/dl/page-speed/psol/1.5.27.3.tar.gz
tar xzf 1.5.27.3.tar.gz -C ngx_pagespeed-release-1.5.27.3-beta/

查看线上版本编译参数


# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.4.0 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.1.tar.gz
# tar -xvzf nginx-1.4.1.tar.gz
# cd nginx-1.4.1/
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_pagespeed-release-1.5.27.3-beta/
# make //注意只make 不要make install
# 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
[0428/104829:INFO:google_message_handler.cc(33)] Shutting down ngx_pagespeed root
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; #这2行建议放在给个server内,如果放在全局,测试discuz不能用?
pagespeed FileCachePath /var/ngx_pagespeed_cache;
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

使用后效果如下:

转自:http://www.linuxeye.com/configuration/1540.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!