From 80614734f9bacb702118d3cdff2962cf07ef6958 Mon Sep 17 00:00:00 2001 From: chaorenluo <1243357953@qq.com> Date: Mon, 29 Apr 2024 18:00:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=89=8D=E7=AB=AF=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=BD=BF=E7=94=A8nginx=E6=9C=8D=E5=8A=A1=E4=BB=A3=E7=90=86,?= =?UTF-8?q?=E4=B8=8D=E5=9C=A8=E4=BD=BF=E7=94=A8=E5=90=8E=E7=AB=AF=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E4=BB=A3=E7=90=86=20(#100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:前端项目使用nginx服务代理,不在使用后端服务代理 * fix:修改备注 --- Dockerfile | 16 ++++++++++---- docker-compose.yaml | 2 +- docker-run.sh | 4 ++++ nginx/nginx.conf | 54 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 nginx/nginx.conf diff --git a/Dockerfile b/Dockerfile index 7a0bffee..cc3d1cd1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,17 +7,25 @@ WORKDIR /xiaoju-survey # 复制文件到工作区间 COPY . /xiaoju-survey +# 安装nginx +RUN apt-get update && \ + apt-get install -y nginx + RUN npm config set registry https://registry.npmjs.org/ # 安装项目依赖 RUN cd /xiaoju-survey/web && npm install && npm run build # 用了后端服务代理启动,建议使用nginx启动 -RUN cd /xiaoju-survey && cp -af ./web/dist/* ./server/public/ +#RUN cd /xiaoju-survey && cp -af ./web/dist/* ./server/public/ +# 覆盖nginx配置文件 +COPY ./nginx/nginx.conf /etc/nginx/nginx.conf RUN cd /xiaoju-survey/server && npm install && npm run build -# 暴露端口 需要跟server的port一致 -EXPOSE 3000 +# 暴露端口 需要跟nginx的port一致 +# EXPOSE 3000 +EXPOSE 8080 -# docker入口文件,运行pm2启动,并保证监听不断 + +# docker入口文件,启动nginx和运行pm2启动,并保证监听不断 CMD ["sh","docker-run.sh"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 24c159b7..79cee71d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -19,7 +19,7 @@ services: container_name: xiaoju-survey restart: always ports: - - "8080:3000" # API端口 + - "8080:8080" # API端口 environment: XIAOJU_SURVEY_MONGO_URL: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@xiaoju-survey-mongo:27017 # docker-compose 会根据容器名称自动处理 links: diff --git a/docker-run.sh b/docker-run.sh index 347a7dc3..6fa554ad 100644 --- a/docker-run.sh +++ b/docker-run.sh @@ -1,3 +1,7 @@ #! /bin/bash + +# 启动nginx +nginx -g 'daemon on;' +# 启动后端服务 cd /xiaoju-survey/server npm run start:prod \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 00000000..73612e7e --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,54 @@ +# 启动的 worker 进程数量 +worker_processes auto; + +# 错误日志路径和级别 +error_log /var/log/nginx/error.log warn; + +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; + server { + listen 8080; + # IPv6端口 + listen [::]:8080; + server_name localhost; + # gzip config + gzip on; + gzip_min_length 1k; + gzip_comp_level 9; + gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; + gzip_vary on; + gzip_disable "MSIE [1-6]\."; + root /xiaoju-survey/web/dist; + + location / { + try_files $uri $uri /management.html; + } + + location /management/ { + try_files $uri $uri/ /management.html; + } + + location /render/ { + try_files $uri $uri/ /render.html; + } + + location /api { + proxy_pass http://127.0.0.1:3000; + } + + error_page 500 502 503 504 /500.html; + client_max_body_size 20M; + } + +}