- 下载安装varnish
[[email protected] ~]# rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
[[email protected] ~]# yum install varnish.x86_64
[[email protected] ~]# egrep -v "^$|^#" /etc/sysconfig/varnish
NFILES=131072
MEMLOCK=82000
NPROCS="unlimited"
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_LISTEN_PORT=88 -远程访问端口
VARNISH_ADMIN_LISTEN_ADDRESS=192.168.2.112
VARNISH_ADMIN_LISTEN_PORT=6082 --监控管理地址
VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_MIN_THREADS=50
VARNISH_MAX_THREADS=1000
VARNISH_THREAD_TIMEOUT=120
VARNISH_STORAGE_SIZE=256M
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
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} \
-p thread_pool_min=${VARNISH_MIN_THREADS} \
-p thread_pool_max=${VARNISH_MAX_THREADS} \
-p thread_pool_timeout=${VARNISH_THREAD_TIMEOUT} \
-u varnish -g varnish \
-S ${VARNISH_SECRET_FILE} \
-s ${VARNISH_STORAGE}"
[[email protected] varnish]# netstat -antulp | grep varnish
tcp 0 0 0.0.0.0:88 0.0.0.0:* LISTEN 5107/varnishd
tcp 0 0 192.168.2.112:6082 0.0.0.0:* LISTEN 5105/varnishd
tcp 0 0 :::88 :::* LISTEN 5107/varnishd
[[email protected] ~]# cd /etc/varnish/
2.简单的反向代理
[[email protected] varnish]# vim default.vcl
vcl 4.0;
backend node1 {
.host = "192.168.2.112";
.port = "8082";
}
sub vcl_recv {
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
2.根据文件对服务器分发
[[email protected] varnish]# vim default.vcl
vcl 4.0;
backend node1 {
.host = "192.168.2.112";
.port = "8082";
}
backend node2 {
.host = "192.168.2.114";
.port = "8082";
}
sub vcl_recv {
if (req.url ~ "^/index.html") {
set req.backend_hint = node1;
}else {
set req.backend_hint = node2;
}
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
3.权限主机分配服务器
[[email protected] varnish]# vim default.vcl
vcl 4.0;
backend node1 {
.host = "192.168.2.112";
.port = "8082";
}
backend node2 {
.host = "192.168.2.114";
.port = "8082";
}
backend node3 {
.host = "192.168.2.115";
.port = "8082";
}
sub vcl_recv {
if (req.http.host ~ "www.tong.com" || req.http.host ~ "tong.com") {
set req.backend_hint = node1;
}elif (req.http.host ~ "www.cheng.com" || req.http.host ~ "cheng.com") {
set req.backend_hint = node2;
}
else {
set req.backend_hint = node3;
}
}
sub vcl_backend_response {
}
sub vcl_deliver {
}