作者:拾_间 | 来源:互联网 | 2023-09-02 12:24
本文由编程笔记#小编为大家整理,主要介绍了tengine-2.3.2负载均衡和haproxy七层代理相关的知识,希望对你有一定的参考价值。下载tengine-2.3.2
本文由编程笔记#小编为大家整理,主要介绍了tengine-2.3.2负载均衡和haproxy七层代理相关的知识,希望对你有一定的参考价值。
下载tengine-2.3.2
[root@ha1 soft]# wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
添加www用户
[root@ha1 soft]# useradd www -s /sbin/nologin -u 1000
[root@ha1 soft]# ll
-rw-r--r-- 1 root root 2835884 Sep 5 2019 tengine-2.3.2.tar.gz
编译安装tengine
[root@ha1 soft]# yum -y install gcc gcc-c++
[root@ha1 soft]# tar -zxvf tengine-2.3.2.tar.gz
[root@ha1 soft]# cd tengine-2.3.2/
[root@localhost tengine-2.3.2]# ./configure
--prefix=/data/tools/tengine-2.3.2
--user=www
--group=www
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_stub_status_module
--with-http_gzip_static_module
--with-pcre
--with-stream
--with-stream_ssl_module
--with-stream_realip_module
[root@ha1 tengine-2.3.2]# make -j 2 && make install
[root@ha1 conf]# pwd
/data/tools/tengine/conf
配置tengine
[root@ha1 conf]# grep -E "vhost|www" nginx.conf
user www www;
include vhost/*.conf;
配置虚拟主机
[root@ha1 conf]# mkdir vhost
[root@ha1 conf]#cd vhost
[root@ha1 vhost]# cat localhost.conf
server {
listen 9001;
server_name localhost;
index index.html index.htm index.php;
root /data/wwwroot/localhost;
}
配置代理
[root@ha1 vhost]# cat api.abc.com.conf
upstream api {
server 127.0.0.1:9001;
}
server {
listen 80;
server_name api.abc.com;
location / {
proxy_pass http://api;
}
}
windows添加本地host解析
C:WindowsSystem32driversetchosts
192.168.100.116 api.abc.com
测试
访问9001端口
访问代理地址
配置haproxy七层代理,实现/a转发到a集群地址,/b转发到b集群地址。
编译安装haproxy
tar xvfhaproxy-1.8.20.tar.gz && cd haproxy-1.8.20
#ARCH:平台架构,TARGET:内核版本,USE_PCRE:开启pcre,USE_OPENSSL:开启ssl
#USE_SYSTEMD:支持使用 -Ws参数(systemd-aware master-worker 模式)启动Haproxy,从而实现单主进程多子进程运行模式
#USE_CPU_AFFINITY:开启haproxy进程与CPU核心绑定
make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/data/tools/haproxy-1.8.20
make install PREFIX=/data/tools/haproxy-1.8.20
cp haproxy /usr/sbin/
cd /data/tools/ && ln -sv /data/tools/haproxy-1.8.20 haproxy
mkdir -p /etc/haproxy/
useradd haproxy -s /sbin/nologin
mkdir /var/run/haproxy
chown -R haproxy.haproxy /var/run/haproxy
#haproxy添加代理配置实现访问/a跳转到a集群地址,访问/b跳转到b集群地址
[root@www ~]# cat /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /data/tools/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
#uid 99
#gid 99
user haproxy
group haproxy
daemon
nbproc 4
cpu-map 1 0
cpu-map 2 1
cpu-map 3 2
cpu-map 4 3
pidfile /var/run/haproxy/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
#添加haproxy代理配置
listen web_port
bind 192.168.100.101:80
mode http
acl a_path path_beg -i /a
use_backend a_path_host if a_path
acl b_path path_beg -i /b
use_backend b_path_host if b_path
backend a_path_host
mode http
server web1 192.168.100.102:8080 check inter 2000 fall 3 rise 5
backend b_path_host
mode http
server web1 192.168.100.103:8080 check inter 2000 fall 3 rise 5