CentOs6.3下配置nginx載入ngx_pagespeed模組

來源:互聯網
上載者:User

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

650) this.width=650;" src="http://img1.51cto.com/attachment/201309/170204382.jpg" title="1.jpg" />

注:顯示X-Page-Speed: 1.6.29.5-3346說明載入成功


瀏覽器訪問首頁,查看JS源檔案對比模組是否載入

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/154051A02-1.jpg" style="float:none;" title="2.jpg" />

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/1540515234-2.jpg" style="float:none;" title="3.jpg" />


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


本文出自 “一路向北” 部落格,請務必保留此出處http://showerlee.blog.51cto.com/2047005/1287148

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.