This commit is contained in:
Baobhan Sith
2025-04-24 20:13:08 +08:00
parent a930f43477
commit e3139457ff
2 changed files with 72 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
version: '3.8' # 使用较新的 Compose 文件格式版本
services:
backend:
build:
context: . # 构建上下文是项目根目录
dockerfile: packages/backend/Dockerfile # 指定后端 Dockerfile 路径
container_name: nexus_backend # 给容器一个明确的名字
ports:
- "3001:3001" # 将容器的 3001 端口映射到宿主机的 3001 端口
environment:
# 这里需要添加后端运行所需的环境变量
# 例如:
# NODE_ENV: production
# PORT: 3001 # 确保容器内端口与 EXPOSE 一致
# DATABASE_PATH: /app/data/database.sqlite # 如果使用 SQLite
# ENCRYPTION_KEY: your_strong_encryption_key # 需要设置强密钥
# SESSION_SECRET: your_strong_session_secret # 需要设置强密钥
# JWT_SECRET: your_strong_jwt_secret # 如果使用 JWT
# ... 其他后端需要的环境变量
# 注意:敏感信息最好通过 .env 文件或 Docker secrets 管理
NODE_ENV: production
PORT: 3001
# 示例:如果数据库文件在容器内的 /app/data 目录下
DATABASE_PATH: /app/data/database.sqlite
# !! 请务必替换下面的示例密钥为强随机值 !!
ENCRYPTION_KEY: replace_with_strong_random_key_32_bytes
SESSION_SECRET: replace_with_strong_random_secret
JWT_SECRET: replace_with_strong_random_jwt_secret
volumes:
# 如果后端需要持久化数据(例如 SQLite 数据库),则挂载卷
# 将宿主机上的 ./data 目录映射到容器内的 /app/data 目录
- ./data:/app/data # 确保宿主机上的 ./data 目录存在或 Docker 会创建它
networks:
- nexus_network # 将服务连接到自定义网络
frontend:
build:
context: . # 构建上下文是项目根目录
dockerfile: packages/frontend/Dockerfile # 指定前端 Dockerfile 路径
container_name: nexus_frontend # 给容器一个明确的名字
ports:
- "8080:80" # 将容器的 80 端口 (Nginx) 映射到宿主机的 8080 端口
depends_on:
- backend # 确保后端服务先启动(但不保证完全就绪)
networks:
- nexus_network # 将服务连接到自定义网络
networks:
nexus_network: # 定义自定义网络
driver: bridge # 使用默认的 bridge 驱动
volumes:
# 如果上面定义了卷,这里可以进一步配置(可选)
data: {} # 定义一个名为 data 的卷
+17
View File
@@ -11,6 +11,23 @@ server {
try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.html;
} }
# Proxy API requests to the backend service
location /api/ {
rewrite ^/api(/.*)$ $1 break; # Remove /api prefix before proxying
proxy_pass http://backend:3001; # Proxy to backend root
# Standard proxy headers
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 support (if needed by the backend API)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# Optional: Add headers for caching, security, etc. # Optional: Add headers for caching, security, etc.
# Example: Cache assets aggressively # Example: Cache assets aggressively
location ~* \.(?:css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { location ~* \.(?:css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {