From 2ff548f378512676e06e3ab0a8610e200342d3c9 Mon Sep 17 00:00:00 2001 From: Kris <2893855659@qq.com> Date: Wed, 9 Apr 2025 14:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90feat=E3=80=91=E5=A2=9E=E5=8A=A0docker?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=96=87=E4=BB=B6=E5=92=8Cnginx=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 24 ++++++++++++++++++++++++ nginx.conf | 31 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 Dockerfile create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9546dd8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +# 第一阶段 +# 使用alpine镜像,可以减少构建后docker镜像文件的体积 +FROM node:14-alpine as build-stage + +# 设置工作目录 +WORKDIR /ruoyi_ui +# 先copy package.json文件到工作目录,一般我们的项目中依赖是不会变的,这样可以充分利用缓存减少部署时的构建时间 +COPY package*.json /ruoyi_ui/ +# 安装node_modules +RUN npm config set registry=https://registry.npmmirror.com/ +RUN npm install +# 将所有文件copy到工作目录 +COPY . /ruoyi_ui +# 开始打包 +RUN npm run build:prod +# 第二阶段 +# 拉取nginx镜像文件 +FROM nginx +# 这里的dist文件就是打包好的文件,project是我们上面设置的工作目录 +COPY --from=build-stage /ruoyi_ui/dist /usr/share/nginx/ruoyi_ui +# default.conf就是我们项目下面的nginx配置文件,我们需要copy到nginx的相应目录 +COPY --from=0 /ruoyi_ui/nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..01cc1d7 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,31 @@ + +server{ + # 监听本地的80端口 + listen 80; + server_name localhost; + # file_upload_size + client_max_body_size 10M; + # request_size + client_body_buffer_size 128k; + + # 对应的打包文件目录 + #root /usr/share/nginx/food_ui; + + # history路由需要配置,不然刷新会404 + #try_files $uri $uri/ /index.html; + + location / { + root /usr/share/nginx/ruoyi_ui; + try_files $uri $uri/ /index.html; # 按此顺序查找请求的文件 + index index.html index.htm; + } + + # 反向代理,解决跨域 + location /prod-api/ { + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header REMOTE-HOST $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_pass http://116.62.56.41:8080/; + } + }