docs: 新增网络和Nginx配置文档
- 新增network-config-guide.md网络配置指南 - 新增nginx-config-guide.md Nginx配置指南 - 新增nginx-fix-guide.md Nginx修复指南 - 新增nginx.conf示例配置
This commit is contained in:
138
docs/network-config-guide.md
Normal file
138
docs/network-config-guide.md
Normal file
@@ -0,0 +1,138 @@
|
||||
# 网络配置指南
|
||||
|
||||
## 配置说明
|
||||
|
||||
### 当前配置
|
||||
|
||||
1. **Nginx 配置**(已连接到外部网络):
|
||||
```yaml
|
||||
networks:
|
||||
life-echo-network:
|
||||
external: true
|
||||
name: lifecho_life-echo-network
|
||||
```
|
||||
|
||||
2. **Life-Echo API 配置**(已更新为使用相同外部网络):
|
||||
```yaml
|
||||
networks:
|
||||
life-echo-network:
|
||||
external: true
|
||||
name: lifecho_life-echo-network
|
||||
```
|
||||
|
||||
3. **Nginx upstream 配置**(使用服务名):
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server api:8000;
|
||||
}
|
||||
```
|
||||
|
||||
## 部署步骤
|
||||
|
||||
### 1. 确保网络存在
|
||||
|
||||
首先检查网络是否存在:
|
||||
|
||||
```bash
|
||||
docker network ls | grep lifecho_life-echo-network
|
||||
```
|
||||
|
||||
如果网络不存在,需要先创建:
|
||||
|
||||
```bash
|
||||
docker network create lifecho_life-echo-network
|
||||
```
|
||||
|
||||
### 2. 启动 Life-Echo 服务
|
||||
|
||||
```bash
|
||||
cd /home/ubuntu/production/lifecho/api
|
||||
docker-compose down
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
### 3. 验证网络连接
|
||||
|
||||
检查容器是否在正确的网络中:
|
||||
|
||||
```bash
|
||||
# 检查网络中的容器
|
||||
docker network inspect lifecho_life-echo-network
|
||||
|
||||
# 应该能看到:
|
||||
# - nginx 容器
|
||||
# - life-echo-api-prod 容器
|
||||
# - life-echo-postgres 容器
|
||||
# - life-echo-redis 容器
|
||||
# - life-echo-celery-worker 容器
|
||||
```
|
||||
|
||||
### 4. 测试连接
|
||||
|
||||
从 nginx 容器内测试:
|
||||
|
||||
```bash
|
||||
# 进入 nginx 容器
|
||||
docker exec -it nginx sh
|
||||
|
||||
# 测试 DNS 解析
|
||||
nslookup api
|
||||
# 或
|
||||
ping api
|
||||
|
||||
# 测试 HTTP 连接
|
||||
wget -O- http://api:8000/health
|
||||
```
|
||||
|
||||
### 5. 重启 Nginx
|
||||
|
||||
```bash
|
||||
docker restart nginx
|
||||
```
|
||||
|
||||
## 故障排查
|
||||
|
||||
### 问题 1:网络名称不匹配
|
||||
|
||||
如果遇到网络名称错误,检查实际网络名称:
|
||||
|
||||
```bash
|
||||
# 查看所有网络
|
||||
docker network ls
|
||||
|
||||
# 查看 life-echo 服务使用的网络
|
||||
docker inspect life-echo-api-prod | grep -A 10 Networks
|
||||
```
|
||||
|
||||
然后更新 docker-compose.yml 中的网络名称。
|
||||
|
||||
### 问题 2:服务名无法解析
|
||||
|
||||
如果 `api:8000` 不工作,可以尝试使用容器名:
|
||||
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server life-echo-api-prod:8000;
|
||||
}
|
||||
```
|
||||
|
||||
### 问题 3:网络连接失败
|
||||
|
||||
如果容器无法连接到网络,手动连接:
|
||||
|
||||
```bash
|
||||
# 连接现有容器到网络
|
||||
docker network connect lifecho_life-echo-network life-echo-api-prod
|
||||
docker network connect lifecho_life-echo-network life-echo-postgres
|
||||
docker network connect lifecho_life-echo-network life-echo-redis
|
||||
docker network connect lifecho_life-echo-network life-echo-celery-worker
|
||||
```
|
||||
|
||||
## 验证清单
|
||||
|
||||
- [ ] 网络 `lifecho_life-echo-network` 存在
|
||||
- [ ] Life-Echo 服务使用外部网络配置
|
||||
- [ ] Nginx 配置使用服务名 `api:8000`
|
||||
- [ ] 所有容器都在同一网络中
|
||||
- [ ] Nginx 可以解析 `api` 主机名
|
||||
- [ ] HTTP 健康检查通过:`curl https://lifecho.worldsplats.com/health`
|
||||
109
docs/nginx-config-guide.md
Normal file
109
docs/nginx-config-guide.md
Normal file
@@ -0,0 +1,109 @@
|
||||
# Nginx 配置调整指南
|
||||
|
||||
## 问题说明
|
||||
|
||||
根据 `docker-compose.yml` 配置:
|
||||
- **服务名**:`api`
|
||||
- **容器名**:`life-echo-api-prod`
|
||||
- **网络名**:`life-echo-network`
|
||||
- **端口映射**:`8000:8000`
|
||||
|
||||
原配置中使用的是 `lifecho-api:8000`,需要根据实际部署方式调整。
|
||||
|
||||
## 配置方案
|
||||
|
||||
### 方案 1:Nginx 在同一个 Docker Compose 网络中(推荐)
|
||||
|
||||
如果 nginx 容器也在 `life-echo-network` 网络中,使用**服务名**:
|
||||
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server api:8000;
|
||||
}
|
||||
```
|
||||
|
||||
**如何实现**:
|
||||
1. 在 nginx 的 docker-compose 配置中添加网络:
|
||||
```yaml
|
||||
services:
|
||||
nginx:
|
||||
# ... 其他配置
|
||||
networks:
|
||||
- life-echo-network
|
||||
|
||||
networks:
|
||||
life-echo-network:
|
||||
external: true # 使用已存在的网络
|
||||
```
|
||||
|
||||
### 方案 2:Nginx 在外部容器但连接到同一网络
|
||||
|
||||
如果 nginx 容器连接到 `life-echo-network` 网络,可以使用**容器名**:
|
||||
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server life-echo-api-prod:8000;
|
||||
}
|
||||
```
|
||||
|
||||
**如何实现**:
|
||||
```bash
|
||||
# 将 nginx 容器连接到网络
|
||||
docker network connect life-echo-network <nginx-container-name>
|
||||
```
|
||||
|
||||
### 方案 3:Nginx 在主机上(通过端口映射)
|
||||
|
||||
如果 nginx 直接运行在主机上,使用**本地端口**:
|
||||
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server 127.0.0.1:8000;
|
||||
}
|
||||
```
|
||||
|
||||
这是最简单的方案,因为 `docker-compose.yml` 已经将容器的 8000 端口映射到主机的 8000 端口。
|
||||
|
||||
## 推荐配置
|
||||
|
||||
根据最常见的部署场景,**推荐使用方案 1(服务名)**,因为:
|
||||
1. 更灵活,不依赖容器名
|
||||
2. Docker Compose 会自动处理服务发现
|
||||
3. 如果容器重启,服务名保持不变
|
||||
|
||||
## 验证配置
|
||||
|
||||
配置完成后,测试连接:
|
||||
|
||||
```bash
|
||||
# 测试健康检查端点
|
||||
curl https://lifecho.worldsplats.com/health
|
||||
|
||||
# 测试 API 端点
|
||||
curl https://lifecho.worldsplats.com/api/your-endpoint
|
||||
```
|
||||
|
||||
## 故障排查
|
||||
|
||||
如果连接失败,检查:
|
||||
|
||||
1. **网络连接**:
|
||||
```bash
|
||||
# 检查容器是否在同一网络
|
||||
docker network inspect life-echo-network
|
||||
|
||||
# 检查容器 IP
|
||||
docker inspect life-echo-api-prod | grep IPAddress
|
||||
```
|
||||
|
||||
2. **端口映射**:
|
||||
```bash
|
||||
# 检查端口是否映射
|
||||
docker ps | grep life-echo-api-prod
|
||||
```
|
||||
|
||||
3. **Nginx 日志**:
|
||||
```bash
|
||||
# 查看错误日志
|
||||
tail -f /var/log/nginx/error.log
|
||||
```
|
||||
121
docs/nginx-fix-guide.md
Normal file
121
docs/nginx-fix-guide.md
Normal file
@@ -0,0 +1,121 @@
|
||||
# Nginx 配置修复指南
|
||||
|
||||
## 错误信息
|
||||
```
|
||||
host not found in upstream "life-echo-api-prod:8000"
|
||||
```
|
||||
|
||||
## 问题原因
|
||||
nginx 容器无法解析 `life-echo-api-prod` 主机名,说明:
|
||||
1. nginx 容器不在 `life-echo-network` 网络中
|
||||
2. 或者应该使用服务名而不是容器名
|
||||
|
||||
## 解决方案
|
||||
|
||||
### 方案 1:使用主机端口(最简单,推荐)
|
||||
|
||||
如果 nginx 在主机上运行,或通过主机端口访问,使用:
|
||||
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server 127.0.0.1:8000;
|
||||
}
|
||||
```
|
||||
|
||||
**优点**:不需要配置网络,因为 `docker-compose.yml` 已经将容器的 8000 端口映射到主机的 8000 端口。
|
||||
|
||||
### 方案 2:将 nginx 连接到同一网络
|
||||
|
||||
如果 nginx 在容器中运行,需要将其连接到 `life-echo-network` 网络:
|
||||
|
||||
#### 步骤 1:检查网络是否存在
|
||||
```bash
|
||||
docker network ls | grep life-echo-network
|
||||
```
|
||||
|
||||
#### 步骤 2:将 nginx 容器连接到网络
|
||||
```bash
|
||||
# 如果 nginx 容器正在运行
|
||||
docker network connect life-echo-network <nginx-container-name>
|
||||
|
||||
# 或者重启 nginx 容器时添加网络
|
||||
docker run -d \
|
||||
--name nginx \
|
||||
--network life-echo-network \
|
||||
-v /path/to/nginx.conf:/etc/nginx/nginx.conf \
|
||||
nginx:latest
|
||||
```
|
||||
|
||||
#### 步骤 3:使用服务名配置
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server api:8000; # 使用服务名,不是容器名
|
||||
}
|
||||
```
|
||||
|
||||
### 方案 3:在 docker-compose.yml 中添加 nginx 服务
|
||||
|
||||
如果希望统一管理,可以在 `docker-compose.yml` 中添加 nginx 服务:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
# ... 其他服务 ...
|
||||
|
||||
nginx:
|
||||
image: nginx:alpine
|
||||
container_name: life-echo-nginx
|
||||
ports:
|
||||
- "80:80"
|
||||
- "443:443"
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./ssl:/etc/nginx/ssl:ro
|
||||
depends_on:
|
||||
- api
|
||||
networks:
|
||||
- life-echo-network
|
||||
restart: always
|
||||
```
|
||||
|
||||
然后使用服务名:
|
||||
```nginx
|
||||
upstream lifecho_api_backend {
|
||||
server api:8000;
|
||||
}
|
||||
```
|
||||
|
||||
## 验证修复
|
||||
|
||||
1. **检查配置语法**:
|
||||
```bash
|
||||
nginx -t
|
||||
```
|
||||
|
||||
2. **重启 nginx**:
|
||||
```bash
|
||||
# 如果 nginx 在容器中
|
||||
docker restart <nginx-container-name>
|
||||
|
||||
# 如果 nginx 在主机上
|
||||
sudo systemctl restart nginx
|
||||
# 或
|
||||
sudo nginx -s reload
|
||||
```
|
||||
|
||||
3. **测试连接**:
|
||||
```bash
|
||||
# 测试健康检查
|
||||
curl http://127.0.0.1:8000/health
|
||||
|
||||
# 测试通过 nginx
|
||||
curl https://lifecho.worldsplats.com/health
|
||||
```
|
||||
|
||||
## 当前推荐配置
|
||||
|
||||
根据错误信息,建议先使用**方案 1**(主机端口),因为:
|
||||
- 最简单,不需要配置网络
|
||||
- `docker-compose.yml` 已经映射了端口
|
||||
- 适用于大多数部署场景
|
||||
|
||||
如果方案 1 不工作(nginx 在容器中且无法访问主机网络),再使用方案 2 或 3。
|
||||
210
docs/nginx.conf
Normal file
210
docs/nginx.conf
Normal file
@@ -0,0 +1,210 @@
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
# WebSocket 连接升级映射
|
||||
map $http_upgrade $connection_upgrade {
|
||||
default upgrade;
|
||||
'' close;
|
||||
}
|
||||
|
||||
sendfile on;
|
||||
tcp_nopush on;
|
||||
tcp_nodelay on;
|
||||
keepalive_timeout 65;
|
||||
types_hash_max_size 2048;
|
||||
client_max_body_size 20M;
|
||||
|
||||
# Gzip 压缩
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_proxied any;
|
||||
gzip_comp_level 6;
|
||||
gzip_types text/plain text/css text/xml text/javascript
|
||||
application/json application/javascript application/xml+rss
|
||||
application/rss+xml font/truetype font/opentype
|
||||
application/vnd.ms-fontobject image/svg+xml;
|
||||
|
||||
# 上游 API 服务器 - AIMeetingRoom API
|
||||
upstream api_backend {
|
||||
server aimetingroom-api:8000;
|
||||
}
|
||||
|
||||
# 上游 API 服务器 - Lifecho API
|
||||
# 根据 docker-compose.yml:
|
||||
# - 服务名:api
|
||||
# - 容器名:life-echo-api-prod
|
||||
# - 网络名:life-echo-network
|
||||
# - 端口映射:8000:8000
|
||||
upstream lifecho_api_backend {
|
||||
# nginx 已连接到 lifecho_life-echo-network 网络
|
||||
# 使用服务名 'api' 进行服务发现(推荐)
|
||||
server api:8000;
|
||||
|
||||
# 备选方案:如果服务名不工作,可以使用容器名
|
||||
# server life-echo-api-prod:8000;
|
||||
}
|
||||
|
||||
# HTTP 服务器配置 - 重定向到 HTTPS
|
||||
server {
|
||||
listen 80;
|
||||
server_name meeting.worldsplats.com lifecho.worldsplats.com;
|
||||
|
||||
# 将所有 HTTP 请求重定向到 HTTPS
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
|
||||
# HTTPS 服务器配置 - meeting.worldsplats.com
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name meeting.worldsplats.com;
|
||||
|
||||
# SSL 证书配置
|
||||
ssl_certificate /etc/nginx/ssl/meeting.worldsplats.com.crt;
|
||||
ssl_certificate_key /etc/nginx/ssl/meeting.worldsplats.com.key;
|
||||
|
||||
# SSL 协议和加密套件配置
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 10m;
|
||||
ssl_session_tickets off;
|
||||
|
||||
# 安全头
|
||||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "no-referrer-when-downgrade" always;
|
||||
|
||||
# AIMeetingRoom API 代理
|
||||
location /api/ {
|
||||
proxy_pass http://api_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;
|
||||
|
||||
# WebSocket 支持(如果需要)
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
|
||||
# 超时设置
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
}
|
||||
|
||||
# Lifecho API 代理(支持 WebSocket)
|
||||
location /lifecho-api/ {
|
||||
proxy_pass http://lifecho_api_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;
|
||||
|
||||
# WebSocket 支持(ws/wss)
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
|
||||
# 禁用缓冲以确保 WebSocket 实时通信
|
||||
proxy_buffering off;
|
||||
proxy_cache off;
|
||||
|
||||
# WebSocket 超时设置(长时间保持连接)
|
||||
proxy_connect_timeout 7d;
|
||||
proxy_send_timeout 7d;
|
||||
proxy_read_timeout 7d;
|
||||
}
|
||||
|
||||
# 健康检查 - AIMeetingRoom API
|
||||
location /health {
|
||||
proxy_pass http://api_backend/api/health;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# 健康检查 - Lifecho API
|
||||
location /lifecho-health {
|
||||
proxy_pass http://lifecho_api_backend/health;
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# 根路径
|
||||
location / {
|
||||
return 200 'AIMeetingRoom API Gateway';
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
}
|
||||
|
||||
# HTTPS 服务器配置 - lifecho.worldsplats.com
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name lifecho.worldsplats.com;
|
||||
|
||||
# SSL 证书配置
|
||||
ssl_certificate /etc/nginx/ssl/lifecho.worldsplats.com.crt;
|
||||
ssl_certificate_key /etc/nginx/ssl/lifecho.worldsplats.com.key;
|
||||
|
||||
# SSL 协议和加密套件配置
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 10m;
|
||||
ssl_session_tickets off;
|
||||
|
||||
# 安全头
|
||||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "no-referrer-when-downgrade" always;
|
||||
|
||||
# Lifecho API 代理(支持 WebSocket)
|
||||
location / {
|
||||
proxy_pass http://lifecho_api_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;
|
||||
|
||||
# WebSocket 支持(ws/wss)
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
|
||||
# 禁用缓冲以确保 WebSocket 实时通信
|
||||
proxy_buffering off;
|
||||
proxy_cache off;
|
||||
|
||||
# WebSocket 超时设置(长时间保持连接)
|
||||
proxy_connect_timeout 7d;
|
||||
proxy_send_timeout 7d;
|
||||
proxy_read_timeout 7d;
|
||||
}
|
||||
|
||||
# 健康检查 - Lifecho API
|
||||
location /health {
|
||||
proxy_pass http://lifecho_api_backend/health;
|
||||
access_log off;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user