From 877d7c035b5aecf310d0428c9923ff8580dccf78 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Mon, 13 Jan 2025 22:10:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8D=E5=90=8C=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 32 +++++++++++++++++--------------- nginx.conf | 18 +++++++++--------- src/config/env.js | 13 +++++++++++++ src/utils/request.js | 5 +++-- 4 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 src/config/env.js diff --git a/Dockerfile b/Dockerfile index fbb082c..7f530b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,14 @@ # 构建阶段 -FROM node:16-alpine as builder +FROM node:16 as builder + +# 设置构建参数 +ARG BUILD_ENV=testing +ARG API_URL + +# 设置环境变量 +ENV NODE_ENV=${BUILD_ENV} +ENV VUE_APP_API_URL=${API_URL} -# 设置工作目录 WORKDIR /app # 复制 package.json 和 package-lock.json @@ -13,23 +20,18 @@ RUN npm install # 复制源代码 COPY . . -# 构建项目 +# 构建应用 RUN npm run build # 生产阶段 -FROM node:16-alpine +FROM nginx:alpine -# 设置工作目录 -WORKDIR /app +# 复制 nginx 配置 +COPY nginx.conf /etc/nginx/conf.d/default.conf -# 安装 serve 包来提供静态文件服务 -RUN npm install -g serve +# 从构建阶段复制构建结果 +COPY --from=builder /app/dist /usr/share/nginx/html -# 从构建阶段复制构建产物 -COPY --from=builder /app/dist ./dist +EXPOSE 80 -# 暴露端口 -EXPOSE 3000 - -# 启动服务 -CMD ["serve", "-s", "dist", "-l", "3000"] \ No newline at end of file +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index eacb99e..89a90b6 100644 --- a/nginx.conf +++ b/nginx.conf @@ -3,21 +3,21 @@ server { server_name localhost; root /usr/share/nginx/html; - index index.html index.htm; + index index.html; + # 支持 history 路由模式 location / { try_files $uri $uri/ /index.html; } - # 配置 API 代理(根据实际需求修改) - location /api { - proxy_pass http://backend-service; # 替换为实际的后端服务地址 - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; + # 缓存静态资源 + location /assets { + expires 1y; + add_header Cache-Control "public, no-transform"; } - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root /usr/share/nginx/html; + # 禁止访问 . 文件 + location ~ /\. { + deny all; } } \ No newline at end of file diff --git a/src/config/env.js b/src/config/env.js new file mode 100644 index 0000000..3888e2a --- /dev/null +++ b/src/config/env.js @@ -0,0 +1,13 @@ +const env = { + development: { + API_URL: 'http://localhost:8080' + }, + testing: { + API_URL: process.env.VUE_APP_API_URL || 'https://api-dev.ibtc.work' + }, + production: { + API_URL: process.env.VUE_APP_API_URL || 'http://api.example.com' + } +} + +export default env[process.env.NODE_ENV || 'development'] \ No newline at end of file diff --git a/src/utils/request.js b/src/utils/request.js index 2bd4d83..ac563c3 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,9 +1,10 @@ import axios from 'axios' import { message } from 'ant-design-vue' +import config from '@/config/env' const request = axios.create({ - baseURL: 'http://127.0.0.1:8000', - timeout: 5000, + baseURL: config.API_URL, + timeout: 10000, headers: { 'Content-Type': 'application/json' }