From 110827a51740dc44fce314a1804488184f9d63bd Mon Sep 17 00:00:00 2001 From: aaron <> Date: Wed, 12 Mar 2025 09:43:22 +0800 Subject: [PATCH] update --- Dockerfile | 57 +++++++++++++++++++-------------- nginx.conf | 93 ++++++++++++++++++++---------------------------------- 2 files changed, 67 insertions(+), 83 deletions(-) diff --git a/Dockerfile b/Dockerfile index 85c587e..c477683 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ FROM node:18-alpine as build-stage # 切换 Alpine 镜像源为阿里云 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories +# 设置工作目录 WORKDIR /app # 设置环境变量 @@ -18,17 +19,25 @@ ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP} RUN echo "Building for environment: ${NODE_ENV}" RUN echo "Build timestamp: ${BUILD_TIMESTAMP}" -# 复制 package.json 和 package-lock.json -COPY package*.json ./ +# 复制依赖文件 +COPY package.json package-lock.json* ./ RUN npm config set registry http://mirrors.cloud.tencent.com/npm/ # 安装依赖 -RUN npm install --production=false +RUN npm ci && \ + echo "Installed dependencies:" && \ + npm list --depth=0 | grep -E '@babel|webpack|vue' -# 复制源代码 +# 确保babel配置文件存在 +RUN echo '{ "presets": ["@babel/preset-env"] }' > .babelrc + +# 复制项目文件 COPY . . +# 创建或更新环境文件 +RUN echo "VUE_APP_API_URL=${API_URL:-/api}" > .env.production + # 构建应用 RUN npm run build @@ -39,32 +48,32 @@ FROM nginx:stable-alpine as production-stage RUN apk add --no-cache bash curl # 创建必要的目录并设置权限 -RUN mkdir -p /var/cache/nginx/client_temp \ - /var/cache/nginx/proxy_temp \ - /var/cache/nginx/fastcgi_temp \ - /var/cache/nginx/uwsgi_temp \ - /var/cache/nginx/scgi_temp \ - && chmod 700 /var/cache/nginx/* \ - && chown -R nginx:nginx /var/cache/nginx +RUN mkdir -p /var/cache/nginx/client_temp && \ + mkdir -p /var/cache/nginx/proxy_temp && \ + mkdir -p /var/cache/nginx/fastcgi_temp && \ + mkdir -p /var/cache/nginx/uwsgi_temp && \ + mkdir -p /var/cache/nginx/scgi_temp && \ + mkdir -p /var/run && \ + mkdir -p /usr/share/nginx/html && \ + chmod -R 755 /var/cache/nginx /var/run /usr/share/nginx/html -# 复制 nginx 配置 +# 删除默认配置 +RUN rm -f /etc/nginx/conf.d/default.conf + +# 复制自定义nginx配置 COPY nginx.conf /etc/nginx/conf.d/default.conf -# 从构建阶段复制构建结果 +# 复制构建文件 COPY --from=build-stage /app/dist /usr/share/nginx/html -# 修改目录权限 -RUN chown -R nginx:nginx /usr/share/nginx/html \ - && chmod -R 755 /usr/share/nginx/html \ - && chown -R nginx:nginx /var/log/nginx \ - && chmod -R 755 /var/log/nginx \ - && touch /var/run/nginx.pid \ - && chown -R nginx:nginx /var/run/nginx.pid \ - && chmod -R 755 /etc/nginx/conf.d - -# 使用root用户运行nginx(标准做法) -# USER nginx +# 设置权限 +RUN chmod -R 755 /usr/share/nginx/html && \ + touch /var/run/nginx.pid && \ + chmod 644 /var/run/nginx.pid && \ + chmod -R 755 /etc/nginx/conf.d +# 暴露端口 EXPOSE 80 +# 启动nginx CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 7b1fe69..d68e7a4 100644 --- a/nginx.conf +++ b/nginx.conf @@ -1,66 +1,41 @@ -# 使用标准的nginx配置结构 -user nginx; -worker_processes auto; +server { + listen 80; + server_name localhost; + + # 添加 gzip 压缩 + gzip on; + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; + + # 设置客户端最大body大小 + client_max_body_size 20M; -error_log /var/log/nginx/error.log warn; -pid /var/run/nginx.pid; + root /usr/share/nginx/html; + index index.html; -events { - worker_connections 1024; -} + # 支持 history 路由模式 + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache, no-store, must-revalidate"; + } -http { - include /etc/nginx/mime.types; - default_type application/octet-stream; + # 缓存静态资源 + location /assets { + expires 1y; + add_header Cache-Control "public, no-transform"; + access_log off; + } - log_format main '$remote_addr - $remote_user [$time_local] "$request" ' - '$status $body_bytes_sent "$http_referer" ' - '"$http_user_agent" "$http_x_forwarded_for"'; + # 禁止访问 . 文件 + location ~ /\. { + deny all; + access_log off; + log_not_found off; + } - access_log /var/log/nginx/access.log main; - - sendfile on; - keepalive_timeout 65; - gzip on; - - server { - listen 80; - server_name localhost; - - # 添加 gzip 压缩 - gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; - - # 设置客户端最大body大小 - client_max_body_size 20M; - - root /usr/share/nginx/html; - index index.html; - - # 支持 history 路由模式 - location / { - try_files $uri $uri/ /index.html; - add_header Cache-Control "no-cache, no-store, must-revalidate"; - } - - # 缓存静态资源 - location /assets { - expires 1y; - add_header Cache-Control "public, no-transform"; - access_log off; - } - - # 禁止访问 . 文件 - location ~ /\. { - deny all; - access_log off; - log_not_found off; - } - - # 错误页面配置 - error_page 404 /index.html; - error_page 500 502 503 504 /50x.html; - location = /50x.html { - root /usr/share/nginx/html; - } + # 错误页面配置 + error_page 404 /index.html; + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; } } \ No newline at end of file