diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e4f9eb8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,31 @@ +# 依赖目录 +node_modules + +# 版本控制 +.git +.gitignore + +# 构建产物 +/dist + +# 开发工具配置 +.vscode/ +.idea/ +.trae/ +.cursor/ + +# 其他配置文件 +.github/ +.husky/ + +# 日志文件 +logs/ + +# 缓存文件 +.cache/ + +*.swp +*.swo + +# 操作系统文件 +.DS_Store diff --git a/.vscode/settings.json b/.vscode/settings.json index c2a5ac8..1c88538 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,7 @@ "explorer.fileNesting.expand": false, "explorer.fileNesting.patterns": { "README.md": "index.html,favicon.ico,robots.txt,CHANGELOG.md", + "docker.md": "Dockerfile,docker*.md,nginx*,.dockerignore", "pages.config.ts": "manifest.config.ts,openapi-ts-request.config.ts", "package.json": "tsconfig.json,pnpm-lock.yaml,pnpm-workspace.yaml,LICENSE,.gitattributes,.gitignore,.gitpod.yml,CNAME,.npmrc,.browserslistrc", "eslint.config.mjs": ".commitlintrc.*,.prettier*,.editorconfig,.commitlint.cjs,.eslint*" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0cf05c7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# 前端打包 +FROM node:24 AS build +WORKDIR /app +# 安装pnpm +RUN npm install -g pnpm +# 设置pnpm镜像源 +RUN pnpm config set registry https://registry.npmmirror.com +# 复制依赖文件 +COPY package.json pnpm-lock.yaml ./ +# 安装依赖(类似npm ci的严格模式) +RUN pnpm install --frozen-lockfile +# 复制源代码 +COPY . . +# 构建项目 +RUN pnpm run build + +# 内容组装 +FROM nginx:1.29.1 AS final + +COPY --from=build /app/dist/build/h5 /usr/share/nginx/html +COPY ./nginx.conf /etc/nginx/nginx.conf + +CMD nginx -g "daemon off;" diff --git a/docker.md b/docker.md new file mode 100644 index 0000000..030df2b --- /dev/null +++ b/docker.md @@ -0,0 +1,25 @@ +## Docker + +根据提供的 `Dockerfile`,可以通过以下步骤构建并运行镜像: + +### 1. 构建Docker镜像 + +在项目根目录执行以下命令: + +- `-t unibest:v1-2025091701`:为镜像指定名称和标签 +- `.`:表示使用当前目录的Dockerfile + +```bash +docker build -t unibest:v1-2025091701 . +``` +### 2. 运行Docker容器 +使用以下命令运行容器: + +```bash +docker run -d -p 80:80 unibest:v1-2025091701 +``` + +- `-d`:表示在后台运行容器 +- `-p 80:80`:将容器的80端口映射到主机的80端口 + + diff --git a/env/.env b/env/.env index 549025f..94a59ce 100644 --- a/env/.env +++ b/env/.env @@ -17,7 +17,7 @@ VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' # h5是否需要配置代理 VITE_APP_PROXY_ENABLE = true -# 下面的不用修改,只要不跟你后台的统一前缀冲突就行 +# 下面的不用修改,只要不跟你后台的统一前缀冲突就行。如果修改了,记得修改 `nginx` 里面的配置 VITE_APP_PROXY_PREFIX = '/fg-api' # 第二个请求地址 (目前alova中可以使用) diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e6e5ecb --- /dev/null +++ b/nginx.conf @@ -0,0 +1,128 @@ +# 配置工作进程数,通常设置为 CPU 核心数 +worker_processes auto; + +# 错误日志配置 +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; + # 开启多路复用 + use epoll; +} + +http { + # 日志格式定义 + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + # 访问日志配置 + access_log /var/log/nginx/access.log main; + + # 高效文件传输设置 + sendfile on; + tcp_nopush on; + tcp_nodelay on; + + # 连接超时设置 + keepalive_timeout 65; + keepalive_requests 100; + + # gzip 压缩优化 + gzip on; + gzip_vary on; + gzip_comp_level 6; + gzip_min_length 1000; + gzip_buffers 16 8k; + gzip_http_version 1.1; + # 增加更多文件类型 + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon; + + # 全局设置 + # 合理限制请求体大小,根据实际需求调整 + client_max_body_size 10m; + client_body_buffer_size 128k; + client_header_timeout 60s; + client_body_timeout 60s; + + # 文件描述符限制 + worker_rlimit_nofile 65535; + + server { + listen 80; + server_name _; + gunzip on; + gzip_static always; + include /etc/nginx/mime.types; + absolute_redirect off; + root /usr/share/nginx/html; + + # 安全相关响应头 + add_header X-Frame-Options SAMEORIGIN; + add_header X-XSS-Protection "1; mode=block"; + add_header X-Content-Type-Options nosniff; + # 根据实际情况调整 CSP + # add_header Content-Security-Policy "default-src 'self'"; + + # 处理 SPA 应用路由 + location / { + try_files $uri $uri/ /index.html; + index index.html index.htm; + } + + # HTML 和 JSON 文件 - 短缓存策略 + location ~ .*\.(html|json)$ { + add_header Cache-Control "public, max-age=300, must-revalidate"; + } + + # 静态资源 - 长缓存策略 + location ~ .*\.(jpg|jpeg|png|gif|bmp|webp|svg|ico|ttf|woff|woff2|eot|mp4|mp3|swf)$ { + add_header Cache-Control "public, max-age=31536000, immutable"; + expires 365d; + access_log off; + } + + # JS 和 CSS - 带版本号的长缓存 + location ~ .*\.(js|css)$ { + add_header Cache-Control "public, max-age=31536000, immutable"; + expires 365d; + access_log off; + } + + # 接口转发 - 替换为实际后端地址 + location ^~ /fg-api { + proxy_http_version 1.1; + 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_set_header Host $host; + # TODO:替换为实际后端服务地址 + # proxy_pass http://your-backend-service; + + proxy_connect_timeout 60s; + proxy_send_timeout 60s; + proxy_read_timeout 60s; + + proxy_buffers 8 32k; + proxy_buffer_size 64k; + proxy_busy_buffers_size 128k; + + proxy_next_upstream error timeout http_500 http_502 http_503 http_504; + } + + # 错误页面配置 + error_page 404 /index.html; + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # 禁止访问隐藏文件 + location ~ /\. { + deny all; + access_log off; + log_not_found off; + } + } +}