歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux配置 >> Nginx下安裝配置PageSpeed模塊,輕松完成網站提速

Nginx下安裝配置PageSpeed模塊,輕松完成網站提速

日期:2017/2/27 14:57:36   编辑:Linux配置

作為Nginx組件,ngx_pagespeed將重寫你的網頁,讓用戶以更快的速度進行訪問。重寫的工作包括壓縮圖片、縮減CSS和JavaScript、擴展緩存時間,同樣還包括其它一些最佳實踐:

  • 優化緩存——整合應用程序的數據和邏輯
  • 最小化round-trip次數——削減連續的請求/響應周期數
  • 最小化請求開銷——削減上傳大小
  • 最小化負載大小——削減響應、下載及緩存頁面大小
  • 優化浏覽器渲染——改善浏覽器頁面布局
  • 移動方面的優化——優化站點移動網絡和設備方面的相關特性

本文是基於LNMP環境,LNMP腳本參考:《LNMP最新源碼安裝腳本》
持續更新文章:http://blog.linuxeye.com/318.html
問題反饋請加QQ群: 235258658

下載解壓PageSpeed模塊

cd /root/lnmp/source
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.6.29.3-beta.zip
unzip -q release-1.6.29.3-beta
wget https://dl.google.com/dl/page-speed/psol/1.6.29.3.tar.gz
tar xzf 1.6.29.3.tar.gz -C ngx_pagespeed-release-1.6.29.3-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.3-beta/
# make //注意只make 不要make install (如果報錯請看下面)

安裝ngx_pagespeed (make) 可能會報如下錯誤

../ngx_pagespeed-release-1.6.29.3-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/source/nginx-1.4.2/../ngx_pagespeed-release-1.6.29.3-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/source/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.3-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;
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

Copyright © Linux教程網 All Rights Reserved