AWS Varnish Monit實戰

源碼編譯varnish和 monit 使用monit來監控varnish 進程
    Aws EC2
    Cento5.7 i386
    Varnish-3.0.2 revision 55e70a4
    monit-5.3.2.tar.gz
編寫管理腳本前的說明
++++++++++++++++
1. 修改varnish rpm 安裝方式時的service管理腳本用於 varnish源碼編譯後的service管理。
2.文件/etc/init.d/varnish 主要修改的地方如下
VARNISH_VCL_CONF=/usr/local/varnish/etc/varnish/default.vcl
pidfile=/var/run/varnish.pid
exec="/usr/local/varnish/sbin/varnishd"
reload_exec="/usr/local/varnish/sbin/varnish_reload_vcl"
prog="varnishd"
config="/etc/sysconfig/varnish"
lockfile="/var/lock/subsys/varnish"
VARNISH_SECRET_FILE=/etc/varnish/secret
exec="/usr/local/varnish/sbin/varnishd"
VARNISHADM="/usr/local/varnish/bin/varnishadm $secret -T $VARNISH_ADMIN_LISTEN_ADDRESS:$VARNISH_ADMIN_LISTEN_PORT"
3.如果啓用了密碼文件,要保證它不不爲空: 如 /etc/varnish/secret 否則varnish服務是起不來的
4.varnish的編譯後安裝目錄爲/usr/local/varnish以下幾個目錄和文件要存在並做設置執行權限
  /etc/varnish
  /etc/sysconfig/varnish
echo "oursect">/etc/varnish/secret
chmod 755 /etc/init.d/varnish
chmod 755 /usr/local/varnish/sbin/varnish_reload_vcl
5. varnish用戶要存在
6.分配給varnish內存和打開文件數( ulimit -l, ulimit -n)通過命令先設置好:ulimit -SH -n 131072
 查看當前系統狀態 ulimit -l
                  ulimit -n
7.monit內置一個管理服務通過3500的默認端口,這裏只允許本地查看監控狀態,如果要通過遠程http查看其狀態,則要修改兩個use address 0.0.0.0和allow ip
set httpd port 3500 and
   use address localhost
   allow localhost
   allow monit:userpassord         //通過web http查看monit狀態,時用到的 用戶和密碼
 
++++++++++++++++
第一部分varnish的安裝與管理腳本
(1)安裝 varnish-cache-3.0.2
  依賴性
Build dependencies on Debian / Ubuntu
In order to build Varnish from source you need a number of packages installed. On a Debian or Ubuntu system these are:
    autotools-dev
    automake1.9
    libtool
    autoconf
    libncurses-dev
    xsltproc
    groff-base
    libpcre3-dev
    pkg-config
Build dependencies on Red Hat / CentOS
To build Varnish on a Red Hat or CentOS system you need the following packages installed:
    automake
    autoconf
    libtool
    ncurses-devel
    libxslt
    groff
    pcre-devel
    pkgconfig
wget -c  http://repo.varnish-cache.org/source/varnish-3.0.2.tar.gz
tar -zxvf varnish-3.0.2.tar.gz
chown root.root -R varnish-3.0.2
cd varnish-3.0.2
./configure  --prefix=/usr/local/varnish
make
make check
make install
ldconfig
(2)將如下兩行放在 /etc/profile文件中來設置系統變量。
export VARNISH_HOME=/usr/local/varnish
export PATH=$PATH:$VARNISH_HOME:$VARNISH_HOME/sbin:$VARNISH_HOME/bin
(3)varnish VCL文件配置
vi /usr/local/varnish/etc/varnish/default.vcl
# Default backend definition. The Seting is just for varnish3.0.X
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
sub vcl_recv {
set req.backend = default;
set req.grace = 5m;
    if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
           set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
         }
         else {
            set req.http.X-Forwarded-For = client.ip;
         }
     }
     # Properly handle different encoding types
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$") {
            remove req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";}
          elsif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";}
        else {
         remove req.http.Accept-Encoding;
      }
    }
   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章