update
This commit is contained in:
parent
cc98e5dccb
commit
110827a517
57
Dockerfile
57
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;"]
|
||||
27
nginx.conf
27
nginx.conf
@ -1,33 +1,9 @@
|
||||
# 使用标准的nginx配置结构
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
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;
|
||||
keepalive_timeout 65;
|
||||
gzip on;
|
||||
|
||||
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大小
|
||||
@ -63,4 +39,3 @@ http {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user