ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升web性能的实践应用到网页和相关的资源(CSS、JS和图片)上,无需你修改内容和流程。
功能包括:
图片优化: stripping meta-data, dynamic resizing, recompression
CSS & JavaScript 压缩、合并等
小资源的内联
推迟图片和 JS 的加载
HTML 重写
延长缓存扩展
-----------------------------------------------
系统环境: CentOS6.3 x64
NGINX: nginx-1.4.1
pagespeed: ngx_pagespeed-1.6.29.5
一.部署环境:
1.关闭iptables和SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
2.安装依赖包
# yum install gcc-c++ pcre-devel zlib-devel zip wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype*
3.同步时间
# ntpdate asia.pool.ntp.org
二.安装ngx_pagespeed
1.配置ngx_pagespeed
# wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.6.29.5-beta.zip
# unzip release-1.6.29.5-beta.zip
# cd ngx_pagespeed-release-1.6.29.5-beta/
2.配置psol模块
# wget https://dl.google.com/dl/page-speed/psol/1.6.29.5.tar.gz
# tar -xzvf 1.6.29.5.tar.gz
三.安装nginx
1.安装前的配置
先添加nginx用户和用户组
# groupadd nginx
# useradd -g nginx -s /bin/false -M nginx
2.解压编译源码包,这里采用最新的源码包1.4.1
# wget
# tar -xvzf nginx-1.4.1.tar.gz
# cd nginx-1.4.1/
# ./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-debug --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --add-module=$HOME/ngx_pagespeed-release-1.6.29.5-beta
注:--add-module=$HOME/ngx_pagespeed-release-1.6.29.5-beta为编译时加载ngx_pagespeed模块
这将使用二进制PageSpeed优化库,但它也有可能建立从源PSOL
ngx_pagespeed目前不支持Windows或MacOS因为不底层PSOL库。
# make && make install
创建缓存目录
# mkdir -p /var/tmp/nginx/client
3.创建启动脚本
# vi /etc/init.d/nginx
-------------------------------
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-
reload|configtest}"
exit 2
esac
---------------------------
# chmod 755 /etc/init.d/nginx
# chkconfig --add nginx
# service nginx start
# chkconfig nginx on
推荐阅读: