docker部署nginx
当然可以!下面是使用 Docker Compose 部署 Nginx,分别服务 a.com 和 b.com,并挂载不同的 index.html 和独立日志的完整示例。
一、文件结构推荐如下:
project/
├── docker-compose.yml
├── nginx/
│ ├── default.conf
│ └── html/
│ ├── a.com/
│ │ └── index.html
│ └── b.com/
│ └── index.html
└── logs/
二、
nginx/default.conf
server {
listen 80;
server_name a.com;
root /usr/share/nginx/html/a.com;
index index.html;
access_log /var/log/nginx/a.access.log combined;
error_log /var/log/nginx/a.error.log;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name b.com;
root /usr/share/nginx/html/b.com;
index index.html;
access_log /var/log/nginx/b.access.log combined;
error_log /var/log/nginx/b.error.log;
location / {
try_files $uri $uri/ =404;
}
}
三、
docker-compose.yml
version: '3.8'
services:
nginx:
image: nginx:latest
container_name: multi-nginx
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./nginx/html:/usr/share/nginx/html
- ./logs:/var/log/nginx
restart: unless-stopped
四、hosts 文件配置(本地测试)
请将下列内容添加到你本地的 hosts 文件中,让浏览器能识别这两个域名指向本地:
127.0.0.1 a.com
127.0.0.1 b.com
五、启动
在 project/ 目录下运行:
docker-compose up -d
然后在浏览器访问:
http://a.com → 看到 a.com 的页面
http://b.com → 看到 b.com 的页面
日志会分别写入 ./logs/a.access.log、./logs/b.access.log 等文件
需要我再加 HTTPS(比如用自签证书或 Let’s Encrypt)或反向代理上游服务吗?