六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1073|回复: 0

haproxy + varnish + nginx + fastCGi + mysql 搭建高可用web集群服务器...

[复制链接]
 楼主| 发表于 2014-11-28 14:51:36 | 显示全部楼层 |阅读模式
linux下haproxy + varnish + nginx + fastCGi + mysql 搭建高可用web集群服务器(二)
二、172.26.11.71 172.26.11.72 安装varnish
  1. mkdir -p /data/software
  2. mkdir -p /data/src
  3. mkdir -p /data/conf
  4. mkdir -p /data/logs
  5. yum -y install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel  gettext-devel
  6. yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libtool
  7. yum -y install rsyslog gcc gcc-c++ libstdc++-devel httpd-devel pcre perl pcre-devel zlib zlib-devel GeoIP GeoIP-devel

  8. cd /data/software/
  9. wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
  10. cd /data/src/
  11. tar zxf ../software/varnish-3.0.3.tar.gz
  12. cd varnish-3.0.3
  13. ./autogen.sh
  14. ./configure --prefix=/usr/local/varnish PKG_CONFIG_PATH=/usr/lib/pkgconfig
  15. make && make install

  16. #指定一下命令的快捷方式
  17. ln -s /usr/local/varnish/sbin/varnishd /usr/bin/varnishd
  18. ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/varnishd
  19. ln -s /usr/local/varnish/bin/varnishlog /usr/bin/varnishlog
  20. ln -s /usr/local/varnish/bin/varnishncsa /usr/bin/varnishncsa
  21. ln -s /usr/local/varnish/bin/varnishadm /usr/bin/varnishadm
  22. ln -s /usr/local/varnish/bin/varnishstat   /usr/bin/varnishstat
  23. #配置文件也指定到熟悉的位置
  24. ln -s /usr/local/varnish/etc/varnish/default.vcl  /etc/varnish.conf
  25. #看看是否已正确安装了
  26. varnishd -V
复制代码
vi /etc/varnish.conf
  1. # This is a basic VCL configuration file for varnish.  See the vcl(7)
  2. # man page for details on VCL syntax and semantics.
  3. #
  4. # Default backend definition.  Set this to point to your content
  5. # server.
  6. #

  7. backend web1 {
  8.         .host = "172.26.11.73";
  9.         .port = "8080";
  10.         .connect_timeout = 1s;
  11.         .first_byte_timeout = 5s;
  12.         .between_bytes_timeout = 2s;
  13. }
  14. backend web2 {
  15.         .host = "172.26.11.74";
  16.         .port = "8080";
  17.         .connect_timeout = 1s;
  18.         .first_byte_timeout = 5s;
  19.         .between_bytes_timeout = 2s;
  20. }

  21. director load random {
  22.     {
  23.       .backend = web1;
  24.       .weight = 5;
  25.      }
  26.      {
  27.        .backend = web2;
  28.        .weight = 5;
  29.      }

  30. }

  31. #
  32. # Below is a commented-out copy of the default VCL logic.  If you
  33. # redefine any of these subroutines, the built-in logic will be
  34. # appended to your code.

  35. acl purge {
  36.        "localhost";
  37.        "127.0.0.1";
  38. }

  39. sub vcl_recv {
  40.     if (req.request == "PURGE") {
  41.         if (!client.ip ~ purge) {
  42.             error 405 "Not allowed.";
  43.         }
  44.         return (lookup);
  45.     }
  46.     if (req.restarts == 0) {
  47.         if (req.http.x-forwarded-for) {
  48.             set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
  49.         } else {
  50.             set req.http.X-Forwarded-For = client.ip;
  51.         }
  52.     }

  53.     if (req.request == "GET" && req.url ~ "\.(js|css|html|jpg|png|gif|swf|jpeg|ico)$") {
  54.         unset req.http.cookie;
  55.     }

  56.     if (req.http.host ~ "^(.*)ijie.com$") {
  57.         set req.backend = load;
  58.         if (req.request != "GET" && req.request != "HEAD") {
  59.             return (pipe);
  60.         }
  61.         elseif(req.url ~ "\.(php|cgi)($|\?)") {
  62.             return (pass);
  63.             #return (lookup);
  64.         }
  65.         else {
  66.             return (lookup);
  67.         }
  68.     }
  69.     else {
  70.         error 404 "Tyler's Server";
  71.         return (lookup);
  72.     }
  73. }
  74. #
  75. sub vcl_pipe {
  76. #     # Note that only the first request to the backend will have
  77. #     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
  78. #     # have it set for all requests, make sure to have:
  79. #     # set bereq.http.connection = "close";
  80. #     # here.  It is not set by default as it might break some broken web
  81. #     # applications, like IIS with NTLM authentication.
  82.      return (pipe);
  83. }
  84. #
  85. sub vcl_pass {
  86.     return (pass);
  87. }
  88. #
  89. sub vcl_hash {
  90.     hash_data(req.url);
  91.     if (req.http.host) {
  92.         hash_data(req.http.host);
  93.     } else {
  94.         hash_data(server.ip);
  95.     }
  96.     return (hash);
  97. }
  98. #
  99. sub vcl_hit {
  100. #       if(req.http.Cache-Control~"no-cache"||req.http.Cache-Control~"max-age=0"||req.http.Pragma~"no-cache"){
  101. #               set obj.ttl=0s;
  102. #               return (restart);
  103. #       }
  104.     return (deliver);
  105. }
  106. #
  107. sub vcl_miss {
  108.     return (fetch);
  109. }

  110. sub vcl_fetch {
  111. #    if (beresp.ttl <= 0s ||
  112. #        beresp.http.Set-Cookie ||
  113. #       beresp.http.Vary == "*") {
  114.                 /*
  115.                  * Mark as "Hit-For-Pass" for the next 2 minutes
  116.                  */
  117. #                set beresp.ttl = 3600 s;
  118. #                return (hit_for_pass);
  119. #    }
  120. set beresp.ttl = 3600m;
  121.         if (req.url ~ "html$") {
  122.                 set beresp.ttl = 3600m;
  123.                 set beresp.do_gzip = true;
  124.                 unset beresp.http.Cache-Control;
  125.                 unset beresp.http.Pragma;
  126.                 set beresp.http.Cache-Control = "max-age=3600";
  127.                 unset beresp.http.Expires;
  128.         }

  129.         if (beresp.http.Pragma ~"no-cache" || beresp.http.Cache-Control ~"no-cache" ||beresp.http.Cache-Control ~"private") {
  130.             return (deliver);
  131.         }
  132.    if (req.request == "GET"&&req.url ~ "(?i)\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js)$") {
  133.         set beresp.ttl = 30d;
  134.    }
  135.    if (req.request == "GET"&& req.url ~ "\.(html|htm)$") {
  136.         set beresp.ttl = 1d;
  137.    }
  138.     return (deliver);
  139. }

  140. sub vcl_deliver {
  141.     return (deliver);
  142. }
  143. #
  144. sub vcl_error {
  145.      set obj.http.Content-Type = "text/html; charset=utf-8";
  146.      set obj.http.Retry-After = "5";
  147.      synthetic {"
  148. <?xml version="1.0" encoding="utf-8"?>
  149. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  150.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  151. <html>
  152.    <head>
  153.      <title>"} + obj.status + " " + obj.response + {"</title>
  154.    </head>
  155.    <body>
  156.      <h1>Error "} + obj.status + " " + obj.response + {"</h1>
  157.      <p>"} + obj.response + {"</p>
  158.      <h3>Guru Meditation:</h3>
  159.      <p>XID: "} + req.xid + {"</p>
  160.      <hr>
  161.      <p>Varnish cache server</p>
  162.    </body>
  163. </html>
  164. "};
  165.      return (deliver);
  166. }
  167. #
  168. sub vcl_init {
  169.     return (ok);
  170. }

  171. sub vcl_fini {
  172.     return (ok);
  173. }
复制代码
#建立Varnish用户以及用户组
useradd -s /sbin/nologin varnish
#将varnish配置文件和服务写入到系统:
cp /data/src/varnish-3.0.3/redhat/varnish.initrc /root/varnish
cp /data/src/varnish-3.0.3/redhat/varnish.sysconfig /etc/sysconfig/varnish
cp /data/src/varnish-3.0.3/redhat/varnish_reload_vcl /usr/local/varnish/bin/
#生成一个secret用于varnish 的 reload,这样以后修改了 /etc/varnish.conf,可以不用重启就可以重新载入新的配置了!
mkdir -p /etc/varnish/
uuidgen > /etc/varnish/secret
chmod 600 /etc/varnish/secret
mkdir -p /data/varnish/cache/
vi /etc/init.d/varnish
  1. #! /bin/sh
  2. . /etc/init.d/functions
  3. retval=0
  4. pidfile=/var/run/varnish.pid
  5. exec="/usr/bin/varnishd"                                        #attention this...
  6. reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"         #attention this...
  7. prog="varnishd"                                                                         #attention this...
  8. config="/etc/sysconfig/varnish"                                 #attention this...
  9. lockfile="/var/lock/subsys/varnish"

  10. # Include varnish defaults
  11. [ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish

  12. start() {

  13.         if [ ! -x $exec ]
  14.         then
  15.                 echo $exec not found
  16.                 exit 5
  17.         fi

  18.         if [ ! -f $config ]
  19.         then
  20.                 echo $config not found
  21.                 exit 6
  22.         fi
  23.         echo -n "Starting Varnish Cache: "

  24.         # Open files (usually 1024, which is way too small for varnish)
  25.         ulimit -n ${NFILES:-131072}

  26.         # Varnish wants to lock shared memory log in memory.
  27.         ulimit -l ${MEMLOCK:-82000}

  28.         # $DAEMON_OPTS is set in /etc/sysconfig/varnish. At least, one
  29.         # has to set up a backend, or /tmp will be used, which is a bad idea.
  30.         if [ "$DAEMON_OPTS" = "" ]; then
  31.                 echo "\$DAEMON_OPTS empty."
  32.                 echo -n "Please put configuration options in $config"
  33.                 return 6
  34.         else
  35.                 # Varnish always gives output on STDOUT
  36.                 daemon --pidfile $pidfile  $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
  37.                 retval=$?
  38.                 if [ $retval -eq 0 ]
  39.                 then
  40.                         touch $lockfile
  41.                         echo_success
  42.                         echo
  43.                 else
  44.                         echo_failure
  45.                         echo
  46.                 fi
  47.                 return $retval
  48.         fi
  49. }

  50. stop() {
  51.         echo -n "Stopping Varnish Cache: "
  52.         killproc -p $pidfile $prog
  53.         retval=$?
  54.         echo
  55.         [ $retval -eq 0 ] && rm -f $lockfile
  56.         return $retval
  57. }

  58. restart() {
  59.         stop
  60.         start
  61. }

  62. reload() {
  63.         if [ "$RELOAD_VCL" = "1" ]
  64.         then
  65.                 $reload_exec
  66.         else
  67.                 force_reload
  68.         fi
  69. }

  70. force_reload() {
  71.         restart
  72. }

  73. rh_status() {
  74.         status -p $pidfile $prog
  75. }

  76. rh_status_q() {
  77.         rh_status >/dev/null 2>&1
  78. }

  79. configtest() {
  80.     if [ -f "$VARNISH_VCL_CONF" ]; then
  81.         $exec -f "$VARNISH_VCL_CONF" -C -n /tmp > /dev/null && echo "Syntax ok"
  82.     else
  83.         echo "VARNISH_VCL_CONF is  unset or does not point to a file"
  84.     fi
  85. }

  86. # See how we were called.
  87. case "$1" in
  88.         start)
  89.                 rh_status_q && exit 0
  90.                 $1
  91.                 ;;
  92.         stop)
  93.                 rh_status_q || exit 0
  94.                 $1
  95.                 ;;
  96.         restart)
  97.                 $1
  98.                 ;;
  99.         reload)
  100.                 rh_status_q || exit 7
  101.                 $1
  102.                 ;;
  103.         force-reload)
  104.                 force_reload
  105.                 ;;
  106.         status)
  107.                 rh_status
  108.                 ;;
  109.         condrestart|try-restart)
  110.                 rh_status_q || exit 0
  111.                 restart
  112.                 ;;
  113.         configtest)
  114.                 configtest
  115.                 ;;
  116.         *)
  117.         echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"

  118.         exit 2
  119. esac

  120. exit $?
复制代码
vi /etc/sysconfig/varnish
  1. NFILES=131072
  2. MEMLOCK=82000
  3. RELOAD_VCL=1
  4. VARNISH_VCL_CONF=/etc/varnish.conf   #attention this...
  5. VARNISH_LISTEN_ADDRESS=0.0.0.0
  6. VARNISH_LISTEN_PORT=80
  7. VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 #这里设置成0.0.0.0表示允许任何IP对其进行管理,当然secret要通过才行~
  8. VARNISH_ADMIN_LISTEN_PORT=2000
  9. VARNISH_SECRET_FILE=/etc/varnish/secret   #attention this...
  10. VARNISH_MIN_THREADS=50
  11. VARNISH_MAX_THREADS=1000
  12. VARNISH_THREAD_TIMEOUT=120   #attention this...
  13. VARNISH_STORAGE_FILE=/data/varnish/cache/varnish_cache.data   #attention this...
  14. VARNISH_STORAGE_SIZE=1G
  15. VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"    #attention this...
  16. #it can also be fixed as this: VARNISH_STORAGE="malloc,1G"
  17. VARNISH_TTL=120
  18. DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
  19.              -f ${VARNISH_VCL_CONF} \
  20.              -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
  21.              -t ${VARNISH_TTL} \
  22.              -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
  23.              -u varnish -g varnish \
  24.              -S ${VARNISH_SECRET_FILE} \
  25.              -s ${VARNISH_STORAGE}"
复制代码
chmod 755 /root/varnish
chmod 755 /usr/local/varnish/bin/varnish_reload_vcl
#可以用的命令:
/root/varnish {start|stop|status|restart|condrestart|try-restart|reload|force-reload}
#查看实时运行状况
varnishstat
#查看日志 方式一(varnish的特有方式):
varnishlog
#查看日志 方式二(与nginx日志相似方式):
varnishncsa
#清除缓存:
varnishadm -T 127.0.0.1:2000 -S /etc/varnish/secret ban.url ^/index.html
varnishadm -T 127.0.0.1:2000 -S /etc/varnish/secret ban.url ^.* #清除所有的
varnishadm -T 127.0.0.1:2000 -S /etc/varnish/secret ban.list
清除www.bbs.com域名下的/static/image/tt.jpg
varnishadm -T 127.0.0.1:2000 -S /etc/varnish/secret ban “req.http.host ~www.bbs.com$ && req.url ~ /static/image/tt.jpg”
varnishadm -T 127.0.0.1:2000 -S /etc/varnish/secret BAN “req.http.host ~www.aipinp.com$ && req.url ~ /index.html”
#优化Linux内核参数
vi /etc/sysctl.conf
  1. net.ipv4.tcp_fin_timeout = 30
  2. net.ipv4.tcp_keepalive_time = 300
  3. net.ipv4.tcp_syncookies = 1
  4. net.ipv4.tcp_tw_reuse = 1
  5. net.ipv4.tcp_tw_recycle = 1
  6. net.ipv4.ip_local_port_range = 5000    65000
复制代码
[size=14.4444446563721px]linux下haproxy + varnish + nginx + fastCGi + mysql 搭建高可用web集群服务器(二)
摘自: http://blog.zhuyin.org/720.html | 拒绝平庸的技术博客


该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表