Nginx四层负载均衡概述
Nginx四层负载均衡概述
什么是负载均衡
四层负载均衡是基于传输层协议包来封装的(如:TCP/IP),那我们前面使用到的七层是指的应用层,他的组装在四层的基础之上,无论四层还是七层都是指的OSI网络模型。
负载均衡应用场景
1、四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;如:nginx就无法保证自己的服务高可用,需要依赖LVS或者keepalive。
2、如:tcp协议的负载均衡,有些请求是TCP协议的(mysql、ssh),或者说这些请求只需要使用四层进行端口的转发就可以了,所以使用四层负载均衡。
四层,七层集群架构

四层负载均衡总结
1、四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
2、四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
3、四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同事的使用)
4、四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5、通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。
Nginx如何配置四层负载均衡
1、通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务;
2、通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。
先配置两台lb负载均衡
[root@lb02 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
#在lb02上安装nginx
[root@lb02 yum.repos.d]# yum install -y nginx
#在lb02上同步lb01的所有nginx相关配置
[root@lb02 ~]# scp -r root@172.16.1.5:/etc/nginx /etc/
#启动nginx
[root@lb02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 conf.d]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@lb02 conf.d]# nginx
1.创建存放四层负载均衡配置文件的目录
[root@lb02 ~]# vim /etc/nginx/nginx.conf
events {
....
}
include /etc/nginx/conf.c/*.conf;
http {
.....
}
[root@lb02 ~]# mkdir /etc/nginx/conf.c
2.配置四层负载均衡
[root@lb02 conf.c]# cat lb_domain.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;
}
}
[root@web03 conf.c]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.c]# nginx -s reload
#配置本机hosts解析后浏览器访问并查看nginx日志
3.四层负载均衡开启日志
#四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层复杂均衡配置实在http以外的;
#如果需要日志则需要配置在stream下面
[root@lb01 conf.c]# cat lb_domain.conf
stream {
log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log /var/log/nginx/proxy.log proxy;
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四层负载均衡端口转发
1.使用nginx四层负载均衡实现tcp的转发
请求负载均衡 5555 ---> 172.16.1.7:22;
请求负载均衡 6666 ---> 172.16.1.51:3306;
2.配置nginx四层负载均衡实现tcp的转发
[root@lb4-01 ~]# cat /etc/nginx/conf.c/lb_domain.conf
stream {
log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log /var/log/nginx/proxy.log proxy;
#定义转发ssh的22端口
upstream ssh_7 {
server 10.0.0.7:22;
}
#定义转发mysql的3306端口
upstream mysql_51 {
server 10.0.0.51:3306;
}
server {
listen 5555;
proxy_connect_timeout 3s;
proxy_timeout 300s;
proxy_pass ssh_7;
}
server {
listen 6666;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass mysql_51;
}
}
Nginx四层负载均衡概述的更多相关文章
- Nginx四层负载均衡
目录 Nginx四层负载均衡概述 Nginx如何配置四层负载均衡 使用nginx四层负载均衡实现tcp的转发 Nginx四层负载均衡概述 什么是四层负载均衡 四层负载均衡是基于传输层协议包来封装的(如 ...
- 14、Nginx四层负载均衡
1.Nginx四层负载均衡基本概述 1.1.什么是四层负载均衡 四层负载均衡基于传输层协议包来封装的(如:TCP/IP),那我们前面使用到的七层是指的应用层,它的组装在四层基础之上,无论四层还是七层都 ...
- Nginx 四层负载均衡
目录 四层负载均衡概述 配置七层负载均衡 配置四层负载均衡 四层负载均衡概述 四层负载均衡是基于IP+端口的负载均衡,七层负载均衡是基于URL或主机名等应用层信息的负载均衡. 其他层负载均衡(转载): ...
- nginx四层负载均衡配置
nginx四层负载均衡配置代理Mysql集群 环境如下: ip 192.168.6.203 Nginx ip 192.168.6.*(多台) Mysql 步骤一 查看Nginx是否安装stream模块 ...
- 安装Nginx四层负载均衡
Nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. stream模块默认不安装的,需要手动添加参数:–with-stream,官方下载地址:downlo ...
- 14.Nginx四层负载均衡
1.七层负载均衡: 根据url 调度不同的集群 url.cheng.com 10.0.0.5 10.0.0.7 /pass 10.0.0.8 /user 1.web01和web02配置 (只不过代码不 ...
- linux+asp.net core+nginx四层负载均衡
Linux Disibutaion:Ubuntu 16.04.1 LTS Web Server:Nginx.Kestrel 关于如何在linux中部署asp.net core我这里不再详细介绍,可以参 ...
- 配置Nginx四层负载均衡
nginx 支持TCP转发和负载均衡的支持 实现下面的架构: 看配置: #user nobody; worker_processes 1; #error_log logs/error.log; #er ...
- Nginx四层负载均衡1
1.Nginx负载均衡Redis 服务器 IP地址 作用 系统版本 Nginx代理服务器 10.0.0.38 负载均衡服务器 Rocky8.6 Redis服务器1 10.0.0.18 Redis服务器 ...
随机推荐
- Aery的UE4 C++游戏开发之旅(3)蓝图
目录 蓝图 蓝图命名规范 蓝图优化 暴露C++至蓝图 暴露C++类 暴露C++属性 暴露C++函数 暴露C++结构体/枚举 暴露C++接口 蓝图和C++的结合方案 使用继承重写蓝图 使用组合重写蓝图 ...
- EggJS接口开发
需求 随着Nodejs的普及,前端开发的开发场景基本可以贯穿界面交互到数据存储,无缝实现全栈开发.最近在实现一个内部项目管理工具的时候,就尝试了一把接口和数据库开发. 什么是Egg.js Egg.js ...
- jquery.i18n 网站呈现各国语言
在做网站的时候可能会遇到不同语言切换的问题,实现的方法有很多种,本篇文章按照 js 加载的方法的来实现. 应用到的 js 文件: jquery.i18n.properties.js jquery.js ...
- Elasticsearch系列---初识Elasticsearch
Elasticsearch是什么? Elasticsearch简称ES,是一个基于Lucene构建的开源.分布式.Restful接口的全文搜索引擎,还是一个分布式文档数据库.天生就是分布式.高可用.可 ...
- java 整合redis缓存 SSM 后台框架 rest接口 shiro druid maven bootstrap html5
A 调用摄像头拍照,自定义裁剪编辑头像,头像图片色度调节B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器 freemaker模版技术 ,0个代码不用写,生成 ...
- js-05-对象(object)
一.访问对象属性的两种方法 a:objectName.PropertyName 对象名.属性名 b:objectName["PropertyName"] 对象名[“ ...
- C#实现将图片设置成圆形形式显示
首先在Form中添加一个控件,然后将控件的背景BackColor设置成透明 . 然后分别设置控件的Image: Image image = Image.FromFile(UserLoginInfor. ...
- 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)
详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类 Python中函数参数定义及调用函数时传参 ...
- 如何使ElementUi中的el-dropdown传入多参数
这边因为业务的需求,觉得随着产品中心以后需要按钮的增多(图1操作栏的效果),这样会导致排版和按钮过于冗长的问题,用户体验不佳,于是想到利用el-dropdown做一个下拉按钮(图1操作1栏的效果) . ...
- 选择IT公司的雇主提问
做为IT从业人员,我们去一家公司时,判断一家公司的专业性时,可以通过以下提问获得反馈: 技术问题 1.这个项目使用了哪些技术(语言,框架,库)?2.应用程序是一体化架构还是微服务架构?3.采用了哪些设 ...