首页 > 系统相关 >Nginx实现四层/七层负载均衡

Nginx实现四层/七层负载均衡

时间:2024-02-18 14:55:38浏览次数:36  
标签:负载 Nginx 七层 server 四层 proxy 均衡 nginx

Nginx实现四层负载均衡

什么是四层负载均衡

四层、七层都是指OSI网络模型的。四层就是在传输层(TCP、UDP那一层)做端口转发(端口映射)

四层负载均衡应用场景

1、4层+7层来做负载均衡,四层可以保证七层负载均衡的高可用。

2、TCP协议的负载均衡。有些请求是TCP协议的(mysql、ssh),或者说这些请求只需要使用四层进行端口的转发就可以了,所以使用四层负载均衡。

 

 

总结:

1、四层负载均衡仅能转发TCP、UDP协议、通常用来转发端口,如:tcp/22、udp/53; 2、四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号) 3、四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同时的使用)4、四层的转发效率比七层的高得多,但仅支持tcp协议,不支持http和https协议; (无法识别域名)5、通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。

nginx实现四层负载

  • 七层负载均衡:upstream模块
  • 四层负载均衡:stream模块(nginx 1.9版本后才有stream模块,可以支持四层负载均衡)

 

nginx四层负载均衡实践

 

 

环境准备

可以在之前实现博客7层负载均衡的环境基础上部署

主机名角色应用IP
lb01 七层负载均衡 Nginx 10.0.0.5
lb02 四层负载均衡 Nginx 10.0.0.6
web01 web网站 nginx php wordpress博客网站 10.0.0.7
web02 web网站 nginx php wordpress博客网站 10.0.0.8
db01 数据库 mysql 10.0.0.51

 

部署

1、web01和web02还是之前的配置,都可以访问http://xxxx.xxx的状态

2、lb01的环境是之前的博客七层负载均衡

[root@lb01 /etc/nginx/conf.d]# cat blog_lb.conf
upstream blog_pools{
      server 172.16.1.7 weight=2;
      server 172.16.1.8 weight=1;
}
server {
      listen 80;
      server_name blog.andrew.com;
      location / {
              proxy_pass http://blog_pools;
              include /etc/nginx/conf.d/proxy_params;
      }
}

# 前面做实验重新源码装了nginx,可以把配置文件include进来即可
[root@lb01 /app/nginx/conf]# vim nginx.conf

include /app/nginx/conf/conf.d/*.conf;
include /etc/nginx/conf.d/*.conf;

3、lb02:四层负载均衡(核心部分)

# 官方源安装nginx
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

# 修改配置文件:添加stream层
ngx_stream_core_module
配置可参考https://nginx.org/en/docs/stream/ngx_stream_core_module.html

#//在events层和http层之间加入stream层:(注意stream层不可以放在http层里面,放http层是七层,会报错)
[root@lb02 ~]# cd /etc/nginx
[root@lb02 /etc/nginx]# vim nginx.conf  

events {
  worker_connections  1024;
}

stream{
      upstream backend{              
              server 172.16.1.5:80;  #//拿lb02(172.16.1.6)代理lb01(172.16.1.5)的80端口
      }
      server{
              listen 80;  
              proxy_pass backend;    #//前面叫backend这里也叫backend
      }
}

http{} # 注释掉http层的内容
...

# 重载nginx
[root@lb02 /etc/nginx]# nginx -t
[root@lb02 /etc/nginx]# systemctl reload nginx

# 域名解析
#10.0.0.5 blog.andrew.com
10.0.0.6 blog.andrew.com

# 访问

 

 

 

 

# 修改配置
stream{
      upstream backend{              
              server 172.16.1.5:80;  
      }
      server{
              listen 90;    # 开放90端口
              proxy_pass backend;    
      }
}

# 再次访问测试

 

 

 

更复杂的架构:两台lb做四层负载均衡

 

 

# 注:lb01配置同lb02,lb4配置示例如下
stream {
  upstream lbserver {
      server 10.0.0.4:80;
      server 10.0.0.5:80;
  }

  server {
      listen 80;
      proxy_pass lbserver;
      proxy_connect_timeout 1s;
      proxy_timeout 3s;
  }
}

 

四层负载均衡配置日志

#四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置是在http以外的;

#如果需要日志则需要配置在stream下面
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.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 lbserver {
      server 10.0.0.4:80;
      server 10.0.0.5:80;
  }

  server {
      listen 80;
      proxy_pass lbserver;
      proxy_connect_timeout 1s;
      proxy_timeout 3s;
  }
}

#查看所有web服务器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log

 

 

四层负载端口转发

 

测试环境

# 比如web02
[root@web02 ~]# ifdown eth0 //关闭eth0网卡。这时xshell再连web02便连不上了
​
# 如何用外网连上呢?
虽然web02外网关了,里面nginx,22端口还在
​
# 在lb02里面起一个456端口映射22端口,配置如下:
[root@lb02 /etc/nginx]# vim nginx.conf 
...
stream{
        upstream backend{
                server 172.16.1.5:80;  
        }
        server{
                listen 90;
                proxy_pass backend;
        }
        # 加入以下配置
        upstream web02_ssh {
                server 172.16.1.8:22;
        }
        server{
                listen 456;
                proxy_pass web02_ssh;
        }
}
​
# 检查语法,重载nginx

 

这时在xshell便可以连web02:

 

 

结束实验,恢复设置:

标签:负载,Nginx,七层,server,四层,proxy,均衡,nginx
From: https://www.cnblogs.com/beyang/p/18019316

相关文章

  • nginx 配置ipv6
    nginx 配置ipv6注意:从Nginx1.3的某个版本起,默认ipv6only是打开的。ipv6兼容ipv4端口模式不可以复用,复用会显示端口被占用,意思是不能部署两个server,要想复用,用本文中最后那个方法。一、只监听IPV61、方法一server{....listen[::]:80;...}2、方法二server{..........
  • Nginx系列--rewrite的使用
    原文网址:​​Nginx系列--rewrite的使用_IT利刃出鞘的博客-CSDN博客​​简介本文介绍Nginx中rewrite的使用。分享Java技术星球(自学精灵):​​learn.skyofit.com​​语法rewriteregexURL[flag];flag标志位last:停止处理rewrite,并对配更改后的URI重新进行搜索(再从server......
  • Nginx系列--转发请求的方法
    原文网址:​​Nginx系列--转发请求的方法_IT利刃出鞘的博客-CSDN博客​​简介说明本文介绍Nginx转发请求的方法。分享Java技术星球(自学精灵):​​https://learn.skyofit.com/​​需求用户访问aaa.com/bbb时,实际访问的是bbb123.com。方案1:return方法server{listen......
  • Django+nginx+uwsgi
    在云服务器上搭建web网站服务器的系统是CentOS7.6一、安装Python3.8.181、安装gccyuminstallgcc-y2、安装编译python的依赖yuminstallzlibzlib-devel-yyuminstallbzip2bzip2-devel-yyuminstallncursesncurses-devel-yyuminstallreadlinereadline-d......
  • nginx容器配置方案
    nginx容器配置可以参考docker官网nginx页面的默认配置docker-nginx官网howtouseimage这里面提供了一些demo,可以帮助我们快速的启动nginx容器,并且除了常规的挂载、端口等基本配置外,还提供了以只读方式启动、以debug模式启动、设置日志等级环境变量等演示。下文仅是博主根据......
  • Ubuntu服务器使用 Daphne + Nginx + supervisor部署Django项目
    视频:https://www.bilibili.com/video/BV1e6421G7uM/?vd_source=36191bed2c30378060ff2efe6831b331Django从3.0版开始加入对ASGI的支持,使Django开始具有异步功能。截止目前的5.0版,对异步支持逐步也越来越好,相信在未来的版本中异步将会支持的更加完善。所以说,我们也需要适时的......
  • nginx里alias,root,try_files笔记
    先说结果:try_files一共有三个值$uri,$uri//index.html,前两个值取决于alias,最后一个值和alias无关,取决于root,即如果最后一个值/index.html,则实际地址是root/index.html,不是alias/index.html,和alias一点关系没有,还有root,alias可以说不是一个东西,root中文意思根路径,限定......
  • Nginx白名单IP限制、国家城市IP访问限制
    文章来源:https://spring4all.com/forum-post/6059.html1.方法一:allow、denydeny和allow指令属于ngx_http_access_module,nginx默认加载此模块,所以可直接使用。这种方式,最简单,最直接。设置类似防火墙iptable,使用方法:直接配置文件中添加:#白名单设置,allow后面为可访问IPlocatio......
  • (填坑)Nginx是万能的
    前几天接到将老项目从HTTP迁移到HTTPS的活,其中,很多Jsp页面访问不到了,很多访问后台控制器的相对路径XXXController.do都被加上了目前Url的前缀——/swdp111/PSGLXT/尝试多种方法,最直接的方式是:   不去管为啥被加上Url,直接给错误的地址做一个代理,最后采用了以下规则来匹配......
  • CentOS安装配置Nginx详细教程
    CentOS安装配置Nginx详细教程一、预先安装额外的依赖yum-yinstallgccgcc-c++yum-yinstallpcre-develyum-yinstallopensslopenssl-devel二、下载Nginx压缩包(这里以Nginx1.24.0为例)下载地址https://nginx.org/en/download.html三、创建nginx文件夹,并上传压缩......