1.下载安装varnish
[root@node3 ~]# rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
[root@node3 ~]# yum install varnish.x86_64
[root@node3 ~]# 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}"
[root@node3 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
[root@node3 ~]# cd /etc/varnish/
2.端口反向代理
[root@node3 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 {
}
3.根据uri地址反向代理
[root@node3 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 {
}
4.根据虚拟主机反向代理
[root@node3 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 {
}
5.根据directors模块随机分配服务器(轮循算法)
[root@node3 varnish]# vim default.vcl
vcl 4.0;
import directors;
backend node1 {
.host = "192.168.2.112";
.port = "8082";
}
backend node2 {
.host = "192.168.2.114";
.port = "8082";
}
backend node3 {
.host = "192.168.2.116";
.port = "8082";
}
sub vcl_init {
new bar = directors.round_robin();
bar.add_backend(node1);
bar.add_backend(node2);
bar.add_backend(node3);
}
sub vcl_recv {
set req.backend_hint = bar.backend();
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
6.健康检查后端服务器
7.hashing算法反向代理服务器