Haproxy结合Tengine使用技巧一则
2016-06-06Linux撒加4891°c
A+ A-文中所写均为生产环境中的配置。
环境介绍
Haproxy:1.5.14
Tengine:2.1.2
前端集群架构:
需求一:实现对公司域名的泛解析
需求二:使用Haproxy对使用pinpoint的机器进行分流
第一个需求,最终其实是在Tengine上做配置,而Haproxy仅负责根据主机头将请求转发到合适的backend上,另外需要在DNSPOD做相应的配置
第二个需求,主要配置在Haproxy
针对两个需求,分别在Haproxy和Tengine上的主要配置如下
Haproxy:
#Host Access List
acl pinpoint_hosts hdr(host) -f /opt/config/haproxy/pinpoint_hosts
acl ttpai_cn hdr_end(host) -i ttpai.cn
##use_backend
use_backend pinpoint_pool if pinpoint_hosts
use_backend varnish_pool if ttpai_cn
Tengine:
server
{
listen 80 default_server;
if ($http_user_agent ~* "iPhone|Android") {
rewrite ^/(.*)$ https://m.ttpai.cn/$1 break;
}
location / {
rewrite ^/(.*)$ https://www.ttpai.cn/$1 break;
}
access_log /opt/logs/tengine/access.default.log access;
error_log /opt/logs/tengine/error.default.log;
}
server{
listen 80;
server_name ttpai.cn www.ttpai.cn;
.......
}
在这个例子中,主要考察了两方面的知识点
1、Haproxy中acl的顺序没有关系,主要是use_backend顺序来决定acl的执行,并不是acl决定use_backend的使用,另外本例中用到了acl中使用-f来加载文件,从指定的文件中匹配一些条件
2、Tengine中关于default_server的使用
当然实现2个需求,还有更简单的方式i,就不在此做讨论了