1、安装HAProxy
~]# yum install HAProxy
2、配置HAProxy
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend web *:80
#acl url_static path_beg -i /static /images /javascript /stylesheets
#acl url_static path_end -i .jpg .gif .png .css .js .html .txt .htm
#acl url_dynamic path_begin -i .php .jsp
#default_backend static_srv if url_static
#use_backend dynamic_srv if url_dynamic
use_backend varnish_srv
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend varnish_srv
balance uri #使用基于URL的一致性哈希调度算法
hash-type consistent
server varnish1 172.16.39.14:9527 check
server varnish2 172.16.39.15:9527 check
listen stats #开启HAProxy图形化Web管理功能
bind :9091
stats enable
stats uri /simpletime?admin
stats hide-version
stats auth admin:abc.123
stats admin if TRUE
3、启动服务
~]# systemctl start haproxy
~]# systemctl status haproxy #查看状态
~]# ss -tnlp #查看80和9091端口是否启用
~]# systemctl enable haproxy #设置开机启动
#############定义负载均衡及算法###############
sub vcl_init {
new webcluster = directors.round_robin();
webcluster.add_backend(web1);
webcluster.add_backend(web2);
new appcluster = directors.round_robin();
appcluster.add_backend(app1);
appcluster.add_backend(app2);
}
################定义vcl_recv函数段######################
sub vcl_recv {
#####ACL未授权,不允许PURGE,并返回405#####
if (req.method == "PURGE") {
if(!client.ip ~ purgers){
return(synth(405,"Purging not allowed for" + client.ip));
}
return (purge);
}
#####添加首部信息,使后端服务记录访问者的真实IP
# if (req.restarts == 0) {
# set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
# } else {
# set req.http.X-Forwarded-For = client.ip;
# }
# set req.backend_hint = webcluster.backend();
# set req.backend_hint = appcluster.backend();
#注:因为Varnish不是一级代理,配置forward只能取到上级代理IP,而上级代理IP,本身就包含在HAProxy发送过来的Forward里,所以没必要配置,而后端服务器只要日志格式有启用记录Forward信息,并且上级代理没有做限制,那么,就能获取到客户端真实IP;
#####动静分离#####
if (req.url ~ "(?i)\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
set req.backend_hint = appcluster.backend();
}
#####不正常的请求不缓存#####
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "PATCH" &&
req.method != "DELETE") {
return (pipe);
}
#####如果请求不是GET或者HEAD,不缓存#####
if (req.method != "GET" && req.method != "HEAD") {
return (pass);
}
#####如果请求包含Authorization授权或Cookie认证,不缓存#####
if (req.http.Authorization || req.http.Cookie) {
return (pass);
}
#####启用压缩,但排除一些流文件压缩#####
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
unset req.http.Accept-Encoding;
} elseif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elseif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
return (hash);
}
####################定义vcl_pipe函数段#################
sub vcl_pipe {
return (pipe);
}
sub vcl_miss {
return (fetch);
}
####################定义vcl_hash函数段#################
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
if (req.http.Accept-Encoding ~ "gzip") {
hash_data ("gzip");
} elseif (req.http.Accept-Encoding ~ "deflate") {
hash_data ("deflate");
}
}
##############设置资源缓存时长#################
sub vcl_backend_response {
if (beresp.http.cache-control !~ "s-maxage") {
if (bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js|html|htm)$") {
unset beresp.http.Set-Cookie;
set beresp.ttl = 3600s;
}
}
}
################启用Purge#####################
sub vcl_purge {
return(synth(200,"Purged"));
}
###############记录缓存命中状态##############
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from " + req.http.host;
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS from " + req.http.host;
}
unset resp.http.X-Powered-By;
unset resp.http.Server;
unset resp.http.Via;
unset resp.http.X-Varnish;
unset resp.http.Age;
}
2、加载配置,因为还没有配置后端应用服务器,可以看到后端主机健康检测全部处于Sick状态
~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
varnish> vcl.load conf1 default.vcl
200
VCL compiled.
varnish> vcl.use conf1
200
VCL 'conf1' now active
varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.39.151,,80) 15 probe Sick 0/5
web2(172.16.39.152,,80) 15 probe Sick 0/5
app1(172.16.39.14,,80) 15 probe Sick 0/5
app2(172.16.39.15,,80) 15 probe Sick 0/5
三、部署Mysql(172.16.39.150)
~]# yum install mariadb.server
~]# rpm -qe mariadb-server
mariadb-server-5.5.44-2.el7.centos.x86_64
~]# vim /etc/my.cnf #数据库基本优化
[mysqld]
innodb_file_per_table = ON
skip_name_resolve = ON
~]# mysql #创建wordpress数据库并授权该数据库用户
> create database wwwdb;
> grant all on wwwdb.* to www@'172.16.39.%' identified by "abc.123";
> exit