使用 Dockerfile 打包 Nginx 配置与静态页面为镜像
💬 问题
bing.html 路径 /data/nginx/static/bing.html google.html 路径 /data/nginx/static/google.html nginx子配置文件路径 /etc/nginx/conf/conf.d/backend_servers.conf
请使用dockerfile 将上面的三个配置文件打包进nginxnginx:1.30的镜像中,镜像名为nginx:1.30_sun_lb
🤖 回答
根据您的需求,我将创建一个完整的Dockerfile来打包Nginx镜像,包含所有配置文件和静态页面。
📁 项目目录结构
首先创建以下目录结构:
arduino
.
├── Dockerfile
├── conf.d/
│ └── weighted_loadbalance.conf
├── backend_conf.d/
│ └── backend_servers.conf
└── static/
├── bing.html
└── google.html
📄 配置文件内容
1. 主负载均衡配置 conf.d/weighted_loadbalance.conf
nginx
# 定义上游服务器组
upstream search_backend {
# 权重设置为 2:1,2/3流量到 bing,1/3流量到 google
server 127.0.0.1:8081 weight=2; # 对应 bing.html
server 127.0.0.1:8082 weight=1; # 对应 google.html
# 启用keepalive提升性能
keepalive 32;
}
server {
listen 80;
server_name localhost;
# 所有请求都转发到 upstream 组
location / {
proxy_pass http://search_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 启用HTTP/1.1 keepalive
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
2. 后端服务配置 backend_conf.d/backend_servers.conf
nginx
# 模拟 bing 后端服务 - 监听 8081 端口
server {
listen 8081;
server_name localhost;
location / {
try_files /bing.html =404;
}
root /data/nginx/static;
}
# 模拟 google 后端服务 - 监听 8082 端口
server {
listen 8082;
server_name localhost;
location / {
try_files /google.html =404;
}
root /data/nginx/static;
}
3. HTML页面文件
static/bing.html
html
<!DOCTYPE html>
<html>
<head>
<title>Bing</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background: #f0f0f0;
}
h1 {
font-size: 80px;
color: #0067B8;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<h1>this is bing</h1>
</body>
</html>
static/google.html
html
<!DOCTYPE html>
<html>
<head>
<title>Google</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background: #f0f0f0;
}
h1 {
font-size: 80px;
color: #4285F4;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<h1>this is google</h1>
</body>
</html>
🐳 Dockerfile
dockerfile
# 使用官方 Nginx 1.30 镜像作为基础
FROM nginx:1.30-alpine
# 设置维护者信息
LABEL maintainer="your-email@example.com"
LABEL description="Nginx with weighted load balancing for bing and google"
# 创建静态文件目录
RUN mkdir -p /data/nginx/static
# 复制配置文件
# 主负载均衡配置
COPY conf.d/weighted_loadbalance.conf /etc/nginx/conf.d/weighted_loadbalance.conf
# 后端服务配置(监听8081和8082端口)
COPY backend_conf.d/backend_servers.conf /etc/nginx/conf.d/backend_servers.conf
# 复制静态HTML文件到指定路径
COPY static/bing.html /data/nginx/static/bing.html
COPY static/google.html /data/nginx/static/google.html
# 删除默认的nginx配置文件(可选)
RUN rm -f /etc/nginx/conf.d/default.conf
# 暴露端口
# 80: 负载均衡器入口
# 8081: bing后端服务
# 8082: google后端服务
EXPOSE 80 8081 8082
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1
# 启动Nginx(前台运行)
CMD ["nginx", "-g", "daemon off;"]
🏗️ 构建和运行
1. 构建镜像
在项目根目录执行:
bash
docker build -t nginx:1.30_sun_lb .
2. 运行容器
bash
# 方式1: 直接运行(推荐)
docker run -d \
--name nginx-lb \
-p 80:80 \
-p 8081:8081 \
-p 8082:8082 \
nginx:1.30_sun_lb
# 方式2: 如需挂载外部配置文件(方便调试)
docker run -d \
--name nginx-lb \
-p 80:80 \
-p 8081:8081 \
-p 8082:8082 \
-v $(pwd)/conf.d:/etc/nginx/conf.d:ro \
-v $(pwd)/static:/data/nginx/static:ro \
nginx:1.30_sun_lb
3. 使用 docker-compose(推荐)
创建 docker-compose.yml:
yaml
name: nginx-weighted-lb
services:
nginx-lb:
image: nginx:1.30_sun_lb
container_name: nginx-lb
ports:
- "80:80"
- "8081:8081"
- "8082:8082"
restart: unless-stopped
# 如需挂载外部配置进行调试,取消注释以下内容
# volumes:
# - ./conf.d:/etc/nginx/conf.d:ro
# - ./static:/data/nginx/static:ro
启动服务:
bash
docker-compose up -d
✅ 验证测试
1. 检查容器状态
bash
docker ps | grep nginx-lb
2. 测试负载均衡效果
bash
# 发送100个请求并统计结果
for i in {1..100}; do
curl -s http://localhost | grep -o "this is [a-z]*"
done | sort | uniq -c
预期输出(示例):
kotlin
64 this is bing
36 this is google
3. 直接测试后端服务
bash
# 测试 bing 后端(应该总是返回 bing.html)
curl http://localhost:8081
# 测试 google 后端(应该总是返回 google.html)
curl http://localhost:8082
4. 查看Nginx日志
bash
# 查看访问日志
docker logs nginx-lb
# 或进入容器查看
docker exec -it nginx-lb cat /var/log/nginx/access.log
📊 架构说明
scss
客户端请求 (端口80)
↓
[负载均衡器] ← nginx:1.30_sun_lb
↓
upstream search_backend (权重 2:1)
↙ ↘
bing (8081) google (8082)
↓ ↓
bing.html google.html
🔧 注意事项
- 端口冲突:确保宿主机端口 80、8081、8082 没有被占用
- 权重调整 :如需修改流量比例,编辑
weighted_loadbalance.conf中的weight值 - 热更新 :如需修改配置而不重建镜像,可使用
-v挂载方式运行 - 日志查看 :Nginx日志位于
/var/log/nginx/access.log和/var/log/nginx/error.log
这样您就得到了一个完整打包了配置和静态文件的Nginx镜像,可以直接用于生产部署!🎉