欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

CentOs6.3下配置nginx加载ngx_pagespeed模块

showerlee 2013-09-02 17:34 NGINX 阅读 (1,945) 抢沙发

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 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 --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

四.配置nginx加载ngx_pagespeed模块

1.创建pagespeed缓存目录

# mkdir /tmp/ngx_pagespeed_cache

# chmod -R 777 /tmp/ngx_pagespeed_cache

2.配置nginx加载

1).配置nginx主配置文件

# vi /usr/local/nginx/conf/nginx.conf

在http层级末行添加:

-------------------------------------

# 缓存路径

pagespeed FileCachePath /tmp/ngx_pagespeed_cache;

# 加载模块

pagespeed on;

# 添加vhost配置文件

include "/usr/local/nginx/conf/vhosts/*.conf";

-------------------------------------

2).配置ngx_pagespeed.inc参数

# vi /usr/local/nginx/conf/ngx_pagespeed.inc

-----------------------------------

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; }

location /pagespeed_console { allow 127.0.0.1; deny all; }

------------------------------------

3).配置vhost

# cd /usr/local/nginx/conf/

# mkdir vhosts

# vi /usr/local/nginx/conf/vhosts/default.conf

------------------------------

server {

     server_name "www.abc.com";

     pagespeed on;

     include ngx_pagespeed.inc;

    location / {

    index index.html index.php;

    root /usr/local/nginx/html/www.abc.com;

    }

    location ~ \.php$ {

          root           html;

          fastcgi_pass   127.0.0.1:9000;

          fastcgi_index  index.php;

          fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/www.abc.com$fastcgi_script_name;

          include        fastcgi_params;

    }

   }

------------------------------

重启服务

# service nginx restart

3.验证ngx_pagespeed模块是否加载

# curl -I localhost | grep X-Page-Speed

1

注:显示X-Page-Speed: 1.6.29.5-3346说明加载成功

访问浏览器首页,查看JS源文件对比ngx_pagespeed模块是否加载

未加载状态

2 

加载状态

3

------------大功告成---------------

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/858

继续浏览:NGINX,ngx_pagespeed

还没有评论,快来抢沙发!

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif