vlambda博客
学习文章列表

linux只nginx四层负载均衡代理七层实例

      四层负载代理七层的实现

lb-4 10.0.0.3

lb-7 10.0.0.5 172.16.1.5

lb-7 10.0.0.6 172.16.1.6

在lb02上安装Nginx

cat /etc/yum.reos.d/nginx.repo

[nginx]

name=nginx repo

baseuel=http://nginx.org/packages/centos/7/$basearch/

gpgckeck=0

enabled=1

yum install nginx -y

在lb02上拷贝lb02的所有nginx相关配置

scp -rp [email protected]:/etc/nginx /etc/

启动nginx

nginx -t

systemctl start nginx

systemctl enable nginx

配置nginx四层负载均衡

cat /etc/yum.reos.d/nginx.repo

[nginx]

name=nginx repo

baseuel=http://nginx.org/packages/centos/7/$basearch/

gpgckeck=0

enabled=1

yum install nginx -y

vim /etc/nginx/nginx.conf

events{...}

include /etc/nginx/conf.c/*.conf;

http{...}

创建存放四层负载均衡配置的目录

rm -f /etc/nginx/conf.d/default.conf

mkdir /etc/nginx/conf.c

cd mkdir /etc/nginx/conf.c

vim lb_domani.conf

stream {

upstream lb{

server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s;

server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s;

}

server{

listen 80;

proxy_connect_timeout 3s;

proxy_timeout 3s;

proxy_pass lb;

}

}

重载服务

nginx -t

systemctl start nginx

使用nginx四层负载均衡实现tcp的转发

请求负载均衡 5555 --->172.16.17:22

stream {

upstream lb{

server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s;

server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s;

}

upstream ssh_7{server 172.16.1.7:22;}

upstream mysql_51{server 172.16.1.51:3306;}

server{

listen 80;

proxy_connect_timeout 3s;

proxy_timeout 3s;

proxy_pass lb;

}

server{

listen 5555;

proxy_connect_timeout 3s;

proxy_timeout 3s;

proxy_pass ssh_7;

}

server{

listen 6666;

proxy_connect_timeout 3s;

proxy_timeout 3s;

proxy_pass mysql_51;

}

}

nginx四层负载均衡记录日志

stream{

log_format proxy '$romote_addr $remote_port -[$time_local] $status $protocol'

'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connetc_time"'

access_log /var/log/nginx/proxy.log proxy;

}