mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-05-06 20:42:52 +08:00
feat(deploy): 添加 nginx 反向代理配置并优化前端构建流程
- 新增 nginx 配置模板,用于前端服务的反向代理 - 更新 Dockerfile,使用 pnpm 替代 npm 并添加 nginx 配置 - 添加启动脚本,实现后端健康检查和动态配置 nginx - 更新 .env.example,为后端主机添加注释说明 - 优化 docker-compose.yml,明确版本号并调整服务依赖
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"]
|
||||
18
BillNote_frontend/deploy/default.conf.template
Normal file
18
BillNote_frontend/deploy/default.conf.template
Normal 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;
|
||||
}
|
||||
}
|
||||
20
BillNote_frontend/deploy/start.sh
Normal file
20
BillNote_frontend/deploy/start.sh
Normal 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;'
|
||||
@@ -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"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user