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

相关推荐
墨香幽梦客5 分钟前
企业IT运维流程标准化:从事件管理到问题闭环的自动化实践
运维·自动化
运维管理8 分钟前
DELL R740服务器上,安装Windows Server 2022--学习篇
运维·服务器·windows
我的golang之路果然有问题9 分钟前
linux 个人笔记导出之网络,防火墙,定时,权限,后台
linux·运维·服务器·网络·笔记·个人笔记
Sapphire~10 分钟前
odoo-087 安装 npm (node ok npm not)
linux·运维·npm
花果山总钻风27 分钟前
在 Debian 10.x 安装Chrome浏览器和ChromeDriver
运维·chrome·debian
艾莉丝努力练剑27 分钟前
【优选算法必刷100题:专题五】(位运算算法)第033~38题:判断字符是否唯一、丢失的数字、两整数之和、只出现一次的数字 II、消失的两个数字
java·大数据·运维·c++·人工智能·算法·位运算
唐装鼠28 分钟前
linux vscode解压版 AI账号无法登陆问题(浏览器无法打开vscode)
linux·运维·vscode
db_murphy41 分钟前
学习篇 | 服务器的睿频
运维·服务器·学习
Levin__NLP_CV_AIGC41 分钟前
Ubuntu部署Dufs
linux·运维·服务器·ubuntu·ssh
oMcLin1 小时前
如何在 Debian 11 上实现基于 BGP 路由的动态负载均衡,提升跨地域数据中心的连接稳定性
debian·php·负载均衡