diff --git a/Dockerfile b/Dockerfile index 63b42c78..720e4dcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,32 @@ -FROM nginx -RUN mkdir /app +FROM node:16-alpine + +# 安装Node.js依赖和构建工具 +WORKDIR /app + +# 先复制package.json和package-lock.json +COPY ./web/package*.json ./ + +# 安装依赖 +RUN npm install + +# 复制项目文件 COPY ./index.html /app/ COPY ./dist /app/dist/ -COPY nginx.conf /etc/nginx/nginx.conf \ No newline at end of file +COPY ./web /app/web/ + +# 复制nginx配置 +COPY nginx.conf /etc/nginx/nginx.conf + +# 安装nginx +RUN apk add --no-cache nginx + +# 创建nginx和app目录 +RUN mkdir -p /var/www/html /var/log/nginx /var/lib/nginx/tmp /run/nginx + +# 复制启动脚本 +COPY ./docker-entrypoint.sh /docker-entrypoint.sh +RUN chmod +x /docker-entrypoint.sh + +EXPOSE 80 3456 + +CMD ["/docker-entrypoint.sh"] \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 00000000..c1f63390 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# 启动AI服务 +node /app/web/scripts/ai.js & +AI_PID=$! + +# 启动nginx +nginx -g 'daemon off;' & +NGINX_PID=$! + +# 等待任意一个进程退出 +wait $AI_PID +EXIT_CODE=$? + +# 清理:停止另一个进程 +kill $NGINX_PID 2>/dev/null || true + +exit $EXIT_CODE \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 385df250..e38e0110 100644 --- a/nginx.conf +++ b/nginx.conf @@ -14,14 +14,34 @@ http { access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; + upstream ai_backend { + server localhost:3456; + } server { listen 80; server_name localhost; + + # 前端静态文件 location / { root /app; index index.html; try_files $uri $uri/ /index.html; } + + # AI后端服务反向代理 + location /ai/ { + proxy_pass http://ai_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; + # SSE支持 + proxy_buffering off; + proxy_cache off; + proxy_set_header Connection ''; + proxy_http_version 1.1; + } + error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html;