This commit is contained in:
aaron 2025-03-12 09:43:22 +08:00
parent cc98e5dccb
commit 110827a517
2 changed files with 67 additions and 83 deletions

View File

@ -4,6 +4,7 @@ FROM node:18-alpine as build-stage
# 切换 Alpine 镜像源为阿里云 # 切换 Alpine 镜像源为阿里云
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 设置工作目录
WORKDIR /app WORKDIR /app
# 设置环境变量 # 设置环境变量
@ -18,17 +19,25 @@ ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP}
RUN echo "Building for environment: ${NODE_ENV}" RUN echo "Building for environment: ${NODE_ENV}"
RUN echo "Build timestamp: ${BUILD_TIMESTAMP}" 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 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 . . COPY . .
# 创建或更新环境文件
RUN echo "VUE_APP_API_URL=${API_URL:-/api}" > .env.production
# 构建应用 # 构建应用
RUN npm run build RUN npm run build
@ -39,32 +48,32 @@ FROM nginx:stable-alpine as production-stage
RUN apk add --no-cache bash curl RUN apk add --no-cache bash curl
# 创建必要的目录并设置权限 # 创建必要的目录并设置权限
RUN mkdir -p /var/cache/nginx/client_temp \ RUN mkdir -p /var/cache/nginx/client_temp && \
/var/cache/nginx/proxy_temp \ mkdir -p /var/cache/nginx/proxy_temp && \
/var/cache/nginx/fastcgi_temp \ mkdir -p /var/cache/nginx/fastcgi_temp && \
/var/cache/nginx/uwsgi_temp \ mkdir -p /var/cache/nginx/uwsgi_temp && \
/var/cache/nginx/scgi_temp \ mkdir -p /var/cache/nginx/scgi_temp && \
&& chmod 700 /var/cache/nginx/* \ mkdir -p /var/run && \
&& chown -R nginx:nginx /var/cache/nginx 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 nginx.conf /etc/nginx/conf.d/default.conf
# 从构建阶段复制构建结果 # 复制构建文件
COPY --from=build-stage /app/dist /usr/share/nginx/html COPY --from=build-stage /app/dist /usr/share/nginx/html
# 修改目录权限 # 设置权限
RUN chown -R nginx:nginx /usr/share/nginx/html \ RUN chmod -R 755 /usr/share/nginx/html && \
&& chmod -R 755 /usr/share/nginx/html \ touch /var/run/nginx.pid && \
&& chown -R nginx:nginx /var/log/nginx \ chmod 644 /var/run/nginx.pid && \
&& chmod -R 755 /var/log/nginx \ chmod -R 755 /etc/nginx/conf.d
&& touch /var/run/nginx.pid \
&& chown -R nginx:nginx /var/run/nginx.pid \
&& chmod -R 755 /etc/nginx/conf.d
# 使用root用户运行nginx标准做法
# USER nginx
# 暴露端口
EXPOSE 80 EXPOSE 80
# 启动nginx
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]

View File

@ -1,66 +1,41 @@
# 使用标准的nginx配置结构 server {
user nginx; listen 80;
worker_processes auto; 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; root /usr/share/nginx/html;
pid /var/run/nginx.pid; index index.html;
events { # 支持 history 路由模式
worker_connections 1024; location / {
} try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
http { # 缓存静态资源
include /etc/nginx/mime.types; location /assets {
default_type application/octet-stream; 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" ' location ~ /\. {
'"$http_user_agent" "$http_x_forwarded_for"'; deny all;
access_log off;
log_not_found off;
}
access_log /var/log/nginx/access.log main; # 错误页面配置
error_page 404 /index.html;
sendfile on; error_page 500 502 503 504 /50x.html;
keepalive_timeout 65; location = /50x.html {
gzip on; root /usr/share/nginx/html;
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;
}
} }
} }