diff --git a/.env.example b/.env.example index cca15ba..86e0f3b 100644 --- a/.env.example +++ b/.env.example @@ -28,8 +28,11 @@ BACKEND_PORT=8000 FRONTEND_PORT=8080 # ==================== 前端配置 ==================== -# API 基础 URL(根据实际部署地址修改) -VITE_API_BASE_URL=http://localhost:8000 +# API 基础 URL(根据实际部署方式选择) +# 方式1(推荐):使用 Nginx 反向代理,前后端同域名同端口 +VITE_API_BASE_URL=/api +# 方式2:直接访问后端端口(需开放后端端口,可能有跨域问题) +# VITE_API_BASE_URL=http://yourdomain.com:8001 # ==================== 存储配置 ==================== # 文件存储路径(宿主机路径,用于存储项目文档和上传文件) diff --git a/DEPLOY.md b/DEPLOY.md index 334edeb..324ad20 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -54,8 +54,11 @@ ADMIN_USERNAME=admin ADMIN_PASSWORD=Your_Secure_Password_123 ADMIN_EMAIL=admin@yourdomain.com -# API 地址(根据实际域名修改) -VITE_API_BASE_URL=http://yourdomain.com:8000 +# API 地址配置 +# 推荐:使用 Nginx 反向代理(前后端同域名同端口,无跨域问题) +VITE_API_BASE_URL=/api +# 或:直接访问后端端口(需开放后端端口到外网) +# VITE_API_BASE_URL=http://yourdomain.com:8001 ``` **⚠️ 重要提示:** @@ -63,6 +66,9 @@ VITE_API_BASE_URL=http://yourdomain.com:8000 - 生产环境必须修改所有默认密码 - `DEBUG` 设置为 `false` - `STORAGE_PATH` 可配置为绝对路径,便于数据管理和备份 +- **API 访问配置**: + - 使用 `VITE_API_BASE_URL=/api` 时,前端通过 Nginx 反向代理访问后端,无需开放后端端口 + - 使用 `VITE_API_BASE_URL=http://IP:8001` 时,直接访问后端,需要开放 8001 端口 ## 📋 部署脚本使用 diff --git a/docker-compose.yml b/docker-compose.yml index 4b2492a..5bbe1f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,10 +17,9 @@ services: - ./backend/scripts/init.sql:/docker-entrypoint-initdb.d/init.sql:ro ports: - "${MYSQL_PORT:-3306}:3306" - command: + command: - --character-set-server=utf8mb4 - --collation-server=utf8mb4_unicode_ci - - --default-authentication-plugin=mysql_native_password healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD:-root_password_change_me}"] interval: 10s diff --git a/forntend/nginx.conf b/forntend/nginx.conf index 062b269..a79e87d 100644 --- a/forntend/nginx.conf +++ b/forntend/nginx.conf @@ -11,6 +11,25 @@ server { gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml+rss; + # API 反向代理(代理到后端服务) + location /api/ { + proxy_pass http://backend:8000/; + 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; + + # 超时设置 + proxy_connect_timeout 60s; + proxy_send_timeout 60s; + proxy_read_timeout 60s; + + # WebSocket 支持 + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + # 前端路由支持 location / { try_files $uri $uri/ /index.html;