当前位置:首页 > PHP教程 > PHP总结归纳

【原创】haproxy+varnish+nginx+fastCGi+mysql搭建

二、172.26.11.71 172.26.11.72 安装varnish mkdir -p /data/softwaremkdir -p /data/srcmkdir -p /data/confmkdir -p /data/logsyum -y install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel lib

二、172.26.11.71 172.26.11.72 安装varnish

mkdir -p /data/software
mkdir -p /data/src
mkdir -p /data/conf
mkdir -p /data/logs
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
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
yum -y install rsyslog gcc gcc-c++ libstdc++-devel httpd-devel pcre perl pcre-devel zlib zlib-devel geoip geoip-devel
cd /data/software/
wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
cd /data/src/
tar zxf ../software/varnish-3.0.3.tar.gz
cd varnish-3.0.3
./autogen.sh
./configure --prefix=/usr/local/varnish pkg_config_path=/usr/lib/pkgconfig
make && make install
#指定一下命令的快捷方式
ln -s /usr/local/varnish/sbin/varnishd /usr/bin/varnishd
ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/varnishd
ln -s /usr/local/varnish/bin/varnishlog /usr/bin/varnishlog
ln -s /usr/local/varnish/bin/varnishncsa /usr/bin/varnishncsa
ln -s /usr/local/varnish/bin/varnishadm /usr/bin/varnishadm
ln -s /usr/local/varnish/bin/varnishstat   /usr/bin/varnishstat
#配置文件也指定到熟悉的位置
ln -s /usr/local/varnish/etc/varnish/default.vcl  /etc/varnish.conf
#看看是否已正确安装了
varnishd -v

vi /etc/varnish.conf

# this is a basic vcl configuration file for varnish.  see the vcl(7)
# man page for details on vcl syntax and semantics.
#
# default backend definition.  set this to point to your content
# server.
#
backend web1 {
        .host = "172.26.11.73";
        .port = "8080";
        .connect_timeout = 1s;
        .first_byte_timeout = 5s;
        .between_bytes_timeout = 2s;
}
backend web2 {
        .host = "172.26.11.74";
        .port = "8080";
        .connect_timeout = 1s;
        .first_byte_timeout = 5s;
        .between_bytes_timeout = 2s;
}
director load random {
    {
      .backend = web1;
      .weight = 5;
     }
     {
       .backend = web2;
       .weight = 5;
     }
}
#
# below is a commented-out copy of the default vcl logic.  if you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
acl purge {
       "localhost";
       "127.0.0.1";
}
sub vcl_recv {
	if (req.request == "purge") {
		if (!client.ip ~ purge) {
			error 405 "not allowed.";
		}
		return (lookup);
	}
	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;
		}
	}
	if (req.request == "get" && req.url ~ ".(js|css|html|jpg|png|gif|swf|jpeg|ico)$") {
		unset req.http.cookie;
	}
	if (req.http.host ~ "^(.*)ijie.com$") {
		set req.backend = load;
		if (req.request != "get" && req.request != "head") {
			return (pipe);
		}
		elseif(req.url ~ ".(php|cgi)($|?)") {
			return (pass);
			#return (lookup);
		}
		else {
			return (lookup);
		}
	}
	else {
		error 404 "tyler's server";
		return (lookup);
	}
}
#
sub vcl_pipe {
#     # note that only the first request to the backend will have
#     # x-forwarded-for set.  if you use x-forwarded-for and want to
#     # have it set for all requests, make sure to have:
#     # set bereq.http.connection = "close";
#     # here.  it is not set by default as it might break some broken web
#     # applications, like iis with ntlm authentication.
     return (pipe);
 }
#
sub vcl_pass {
    return (pass);
}
#
sub vcl_hash {
	hash_data(req.url);
	if (req.http.host) {
		hash_data(req.http.host);
	} else {
		hash_data(server.ip);
	}
	return (hash);
}
#
sub vcl_hit {
#       if(req.http.cache-control~"no-cache"||req.http.cache-control~"max-age=0"||req.http.pragma~"no-cache"){
#               set obj.ttl=0s;
#               return (restart);
#       }
    return (deliver);
}
#
sub vcl_miss {
    return (fetch);
}
sub vcl_fetch {
#    if (beresp.ttl  /etc/varnish/secret
chmod 600 /etc/varnish/secret

mkdir -p /data/varnish/cache/

vi /etc/init.d/varnish

#! /bin/sh
. /etc/init.d/functions
retval=0
pidfile=/var/run/varnish.pid
exec="/usr/bin/varnishd"                                        #attention this...
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"         #attention this...
prog="varnishd"                                                                         #attention this...
config="/etc/sysconfig/varnish"                                 #attention this...
lockfile="/var/lock/subsys/varnish"
# include varnish defaults
[ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish
start() {
        if [ ! -x $exec ]
        then
                echo $exec not found
                exit 5
        fi
        if [ ! -f $config ]
        then
                echo $config not found
                exit 6
        fi
        echo -n "starting varnish cache: "
        # open files (usually 1024, which is way too small for varnish)
        ulimit -n ${nfiles:-131072}
        # varnish wants to lock shared memory log in memory.
        ulimit -l ${memlock:-82000}
        # $daemon_opts is set in /etc/sysconfig/varnish. at least, one
        # has to set up a backend, or /tmp will be used, which is a bad idea.
        if [ "$daemon_opts" = "" ]; then
                echo "$daemon_opts empty."
                echo -n "please put configuration options in $config"
                return 6
        else
                # varnish always gives output on stdout
                daemon --pidfile $pidfile  $exec -p $pidfile "$daemon_opts" > /dev/null 2>&1
                retval=$?
                if [ $retval -eq 0 ]
                then
                        touch $lockfile
                        echo_success
                        echo
                else
                        echo_failure
                        echo
                fi
                return $retval
        fi
}
stop() {
        echo -n "stopping varnish cache: "
        killproc -p $pidfile $prog
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
}
restart() {
        stop
        start
}
reload() {
        if [ "$reload_vcl" = "1" ]
        then
                $reload_exec
        else
                force_reload
        fi
}
force_reload() {
        restart
}
rh_status() {
        status -p $pidfile $prog
}
rh_status_q() {
        rh_status >/dev/null 2>&1
}
configtest() {
    if [ -f "$varnish_vcl_conf" ]; then
        $exec -f "$varnish_vcl_conf" -c -n /tmp > /dev/null && echo "syntax ok"
    else
        echo "varnish_vcl_conf is  unset or does not point to a file"
    fi
}
# see how we were called.
case "$1" in
        start)
                rh_status_q && exit 0
                $1
                ;;
        stop)
                rh_status_q || exit 0
                $1
                ;;
        restart)
                $1
                ;;
        reload)
                rh_status_q || exit 7
                $1
                ;;
        force-reload)
                force_reload
                ;;
        status)
                rh_status
                ;;
        condrestart|try-restart)
                rh_status_q || exit 0
                restart
                ;;
        configtest)
                configtest
                ;;
        *)
        echo "usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

vi /etc/init.d/varnish

nfiles=131072
memlock=82000
reload_vcl=1
varnish_vcl_conf=/etc/varnish.conf   #attention this...
varnish_listen_address=0.0.0.0
varnish_listen_port=80
varnish_admin_listen_address=127.0.0.1
varnish_admin_listen_port=2000
varnish_secret_file=/etc/varnish/secret   #attention this...
varnish_min_threads=50
varnish_max_threads=1000
varnish_thread_timeout=120   #attention this...
varnish_storage_file=/data/varnish/cache/varnish_cache.data   #attention this...
varnish_storage_size=1g
varnish_storage="file,${varnish_storage_file},${varnish_storage_size}"    #attention this...
#it can also be fixed as this: varnish_storage="malloc,1g"
varnish_ttl=120
daemon_opts="-a ${varnish_listen_address}:${varnish_listen_port} 
             -f ${varnish_vcl_conf} 
             -t ${varnish_admin_listen_address}:${varnish_admin_listen_port} 
             -t ${varnish_ttl} 
             -w ${varnish_min_threads},${varnish_max_threads},${varnish_thread_timeout} 
             -u varnish -g varnish 
             -s ${varnish_secret_file} 
             -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

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!