Nginx 负载均衡案例配置

负载均衡案例

基于 docker 进行 案例测试

1、创建三个 Nginx 实例

创建目录结构

为每个 Nginx 实例创建单独的目录,用于存储 HTML 文件和配置文件

bash 复制代码
mkdir -p data/nginx1/html
mkdir -p data/nginx2/html
mkdir -p data/nginx3/html

添加自定义 HTML 文件

在每个目录中创建一个 index.html 文件,用于区分不同的 Nginx 实例

  • /root/data/nginx1/html/index.html
html 复制代码
<h1>Welcome to Nginx 1</h1>
  • /root/data/nginx2/html/index.html
html 复制代码
<h1>Welcome to Nginx 2</h1>
  • /root/data/nginx3/html/index.html
html 复制代码
<h1>Welcome to Nginx 3</h1>

分别启动三个 Nginx 容器 ,并绑定到 不同的 端口

bash 复制代码
docker run -d -p 801:80 --name nginx1 -v /root/data/nginx1/html:/usr/share/nginx/html nginx:latest

docker run -d -p 802:80 --name nginx2 -v /root/data/nginx2/html:/usr/share/nginx/html nginx:latest

docker run -d -p 803:80 --name nginx3 -v /root/data/nginx3/html:/usr/share/nginx/html nginx:latest

配置负载均衡服务器

创建一个目录(nginx4)并添加配置文件

bash 复制代码
mkdir -p data/nginx4/conf

/root/data/nginx4/conf/nginx.conf 中添加以下内容

nginx 复制代码
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    upstream backend_servers {
        # 权重
        server 192.168.159.130:801 weight=4;
        server 192.168.159.130:802 weight=2;
        server 192.168.159.130:803 weight=2;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend_servers;
        }
    }
}

启动一个新的容器作为负载均衡和代理服务器

bash 复制代码
docker run -d -p 80:80 --name nginx4 -v /root/data/nginx4/conf/nginx.conf:/etc/nginx/nginx.conf nginx:latest

访问 地址 http://192.168.159.130/ ,会发现响应分别来自 192.168.159.130:801; 192.168.159.130:802; 192.168.159.130:803

相关推荐
Freed&21 分钟前
Ansible 生产级自动化指南:Playbook、Handlers、Jinja2 全解析
运维·自动化·ansible
b***251123 分钟前
储能电池包的自动化产线探秘|深圳比斯特自动化
运维·自动化
ZeroNews内网穿透24 分钟前
新版发布!“零讯”微信小程序版本更新
运维·服务器·网络·python·安全·微信小程序·小程序
工控小楠25 分钟前
涡街流量计温度数据的协议桥梁:Modbus RTU 转 Profinet 网关的自动化应用
运维·自动化
<但凡.37 分钟前
Linux 修炼:进程控制(一)
linux·运维·服务器·bash
m0_464608262 小时前
Ansible实现自动化运维
运维·自动化·ansible
m0_464608262 小时前
Ansible Playbook:自动化配置管理的利器
运维·自动化·ansible
DDC楼宇自控与IBMS集成系统解读2 小时前
DDC 楼宇自控系统 + 3D 可视化运维管理平台融合解决方案
运维·3d
乌萨奇也要立志学C++3 小时前
【Linux】进程概念(六):进程地址空间深度解析:虚拟地址与内存管理的奥秘
linux·运维
tritone9 小时前
我在阿贝云免费服务器上搭建RustDesk自建服务器(Self-Hosting)的真实体验【推荐】
运维·服务器