Merge pull request #8 from JefferyHcool/hotfix

feat(deploy): 添加 nginx 反向代理配置并优化前端构建流程
This commit is contained in:
Jianwu Huang
2025-04-16 02:05:05 +08:00
committed by GitHub
5 changed files with 63 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
# 通用端口配置
BACKEND_PORT=8001
FRONTEND_PORT=3015
BACKEND_HOST=0.0.0.0
BACKEND_HOST=0.0.0.0 # 默认为 0.0.0.0,表示监听所有 IP 地址 不建议动
# 前端访问后端用(生产环境建议写公网或宿主机 IP
VITE_API_BASE_URL=http://127.0.0.1:8001

View File

@@ -1,13 +1,32 @@
# === 前端构建阶段 ===
FROM node:18-alpine AS build
# 安装 pnpm
RUN npm install -g pnpm
# 设置工作目录
WORKDIR /app
# 拷贝前端源码
COPY ./BillNote_frontend /app
RUN npm install && npm run build
# 安装依赖并构建
RUN pnpm install && pnpm run build
# === nginx 运行阶段 ===
FROM nginx:alpine
# 拷贝模板配置
COPY ./BillNote_frontend/deploy/default.conf.template /etc/nginx/templates/default.conf.template
# 拷贝构建产物
COPY --from=build /app/dist /usr/share/nginx/html
# 拷贝启动脚本
COPY ./BillNote_frontend/deploy/start.sh /start.sh
RUN chmod +x /start.sh
EXPOSE 80
# 使用启动脚本启动容器
CMD ["/start.sh"]

View File

@@ -0,0 +1,18 @@
server {
listen 80;
resolver 127.0.0.11 valid=10s;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://backend:${BACKEND_PORT};
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;
}
}

View File

@@ -0,0 +1,20 @@
#!/bin/sh
###
# @Author: Jefferyhcool 1063474837@qq.com
# @Date: 2025-04-16 01:57:05
# @LastEditors: Jefferyhcool 1063474837@qq.com
# @LastEditTime: 2025-04-16 01:59:37
# @FilePath: /hotfix-dev/BillNote_frontend/deploy/start.sh
# @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
###
# 等待后端健康检查通过
until curl -s "http://backend:${BACKEND_PORT}/health" > /dev/null; do
echo "等待后端服务就绪..."
sleep 2
done
# 生成 nginx 配置文件(动态变量替换)
envsubst '${BACKEND_HOST} ${BACKEND_PORT}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf
# 启动 Nginx在前台运行
exec nginx -g 'daemon off;'

View File

@@ -1,3 +1,4 @@
version: "3.9"
services:
backend:
@@ -14,6 +15,7 @@ services:
- ./backend:/app
ports:
- "${BACKEND_PORT}:${BACKEND_PORT}"
depends_on:
- frontend
@@ -26,3 +28,5 @@ services:
- .env
ports:
- "${FRONTEND_PORT}:80"