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

相关推荐
盈创力和200716 分钟前
物联网 “神经” 之以太网:温湿度传感器的工业级 “高速干道”
运维·服务器·网络·嵌入式硬件·以太网温湿度传感器
eddy-原23 分钟前
阿里云核心服务解析与应用实践
linux·运维·阿里云·云计算
路由侠内网穿透.26 分钟前
外网访问可视化工具 Grafana (Linux版本)
linux·运维·服务器·grafana·远程工作
爱吃糖的小秦同学32 分钟前
Docker爆红且安装非C盘处理方案
运维·docker·容器
睿本云40 分钟前
产品月报|睿本云10月产品功能迭代
运维·服务器·apache
白榆!1 小时前
Linux 基本指令入门:从基础操作到实战应用
linux·运维·服务器
IDOlaoluo1 小时前
SQL Server 2017 Developer 中文版安装教程(64位 ISO 文件详细步骤)
服务器·数据库·负载均衡
会飞的小蛮猪1 小时前
Skywalking运维之路(Skywalking服务搭建)
java·运维·监控
大锦终2 小时前
【Linux】TCP协议
linux·运维·tcp/ip
宁雨桥3 小时前
Debian 服务器环境搭建全指南:从工具选型到项目部署实战
运维·服务器·debian