做爲Nginx組件,ngx_pagespeed將重寫你的網頁,讓用戶以更快的速度進行訪問。重寫的工做包括壓縮圖片、縮減CSS和JavaScript、擴展緩存時間,一樣還包括其它一些最佳實踐: html
本文是基於LNMP環境,LNMP腳本參考:《LNMP最新源碼安裝腳本》
下載解壓PageSpeed模塊
linux
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/
查看線上版本編譯參數
nginx
# /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模塊從新編譯
git
# 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
github
# 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
使用後效果以下: shell