运维开发网

tengine-2.3.2负载均衡和haproxy七层代理

运维开发网 https://www.qedev.com 2020-06-22 12:52 出处:51CTO 作者:我是陈先生
[[email protected]]#wgethttp://tengine.taobao.org/download/tengine-2.3.2.tar.gz```添加www用户[[email protected]]#useraddwww-s/sbin/nologin-u1000[[email protected]]#ll-rw-r--r--1rootroot2835884Sep52019tengine-2.3.2.tar.g

下载tengine-2.3.2

[[email protected] soft]# wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz

添加www用户

[[email protected] soft]# useradd www -s /sbin/nologin -u 1000

[[email protected] soft]# ll

-rw-r--r-- 1 root root 2835884 Sep 5 2019 tengine-2.3.2.tar.gz

编译安装tengine

[[email protected] soft]# yum -y install gcc gcc-c++

[[email protected] soft]# tar -zxvf tengine-2.3.2.tar.gz

[[email protected] soft]# cd tengine-2.3.2/

[[email protected] 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

[[email protected] tengine-2.3.2]# make -j 2 && make install

[[email protected] conf]# pwd

/data/tools/tengine/conf

配置tengine

[[email protected] conf]# grep -E "vhost|www" nginx.conf

user www www;

include vhost/*.conf;

配置虚拟主机

[[email protected] conf]# mkdir vhost

[[email protected] conf]#cd vhost

[[email protected] vhost]# cat localhost.conf

server {

listen 9001;

server_name localhost;

index index.html index.htm index.php;

root /data/wwwroot/localhost;

}

配置代理

[[email protected] 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:\Windows\System32\drivers\etc\hosts

192.168.100.116 api.abc.com

测试

访问9001端口

tengine-2.3.2负载均衡和haproxy七层代理

访问代理地址

tengine-2.3.2负载均衡和haproxy七层代理

配置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集群地址

[[email protected] ~]# 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

0

精彩评论

暂无评论...
验证码 换一张
取 消