ngx_pagespeed 是 Google 為 Nginx 開發的自動優化模塊,它會在響應返回前即時重寫你的頁面內容,讓使用者拿到更「輕」的網站。重寫動作涵蓋圖片壓縮、CSS 與 JavaScript 精簡、緩存時間延長等一系列前端最佳實踐:
以下操作以 LNMP 環境為例(LNMP 腳本可參考《LNMP 最新源碼安裝腳本》)。文章持續更新:https://blog.linuxeye.com/318.html,問題反饋請加 QQ 群:235258658。
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
# wget https://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
執行 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 編譯選項:
# 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) #備份舊二進制 # cp objs/nginx /usr/local/nginx/sbin/nginx #用新編譯的產物覆蓋 # /usr/local/nginx/sbin/nginx -t #先驗證配置語法 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` #上線新版本 # kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` #退掉舊進程
# 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)
# 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
# curl -I 'https://www.linuxeye.com/' | grep X-Page-Speed
X-Page-Speed: 1.4.0.0-2748
看到 X-Page-Speed 響應頭,就代表模塊已經生效。
如果站點是 Discuz! 且採用了偽靜態,需要把下面這段注銷掉(每行前面加 #):
#if (!-e $request_filename) {
# return 404;
#}
開啟後的實際效果可參考示範站點 https://www.linuxeye.com:
