From f008d59b4575fd7958f4a6d214546e37b50a63a6 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Wed, 28 Feb 2024 10:51:39 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20nginx=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.production | 3 +- Dockerfile | 2 +- docker-compose.yml | 7 ++++ nginx.conf | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 nginx.conf diff --git a/.env.production b/.env.production index a657646..6ae7d68 100644 --- a/.env.production +++ b/.env.production @@ -3,7 +3,8 @@ ENV = 'production' # base api url # VITE_BASE_API_URL = 'https://nest-api.buqiyuan.site' -VITE_BASE_API_URL = 'http://123.249.107.55' +# VITE_BASE_API_URL = 'http://123.249.107.55' +VITE_BASE_API_URL = 'http://localhost' # 公共基础路径, 所有资源的路径都将据此配置重写。 VITE_BASE_URL = /api diff --git a/Dockerfile b/Dockerfile index 2da473c..16c05c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,6 +24,6 @@ FROM nginx:alpine as production ARG PROJECT_DIR COPY --from=builder $PROJECT_DIR/dist/ /usr/share/nginx/html -# COPY --from=builder $PROJECT_DIR/nginx.conf /etc/nginx/nginx.conf +COPY --from=builder $PROJECT_DIR/nginx.conf /etc/nginx/nginx.conf EXPOSE 80 diff --git a/docker-compose.yml b/docker-compose.yml index 96ece10..5c2b893 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,15 @@ services: context: . dockerfile: Dockerfile target: production + container_name: huaxin-base-frontend ports: - '80:80' env_file: - .env - .env.production + networks: + - nest_front_net +networks: + nest_front_net: + external: + name: nest_admin_net diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..301cf4d --- /dev/null +++ b/nginx.conf @@ -0,0 +1,90 @@ +user root; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + map $time_iso8601 $logdate { + '~^(?\d{4}-\d{2}-\d{2})' $ymd; + default 'date-not-found'; + } + + log_format main '$remote_addr [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer"'; + + root /var/log/nginx; + access_log /var/log/nginx/access-$logdate.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + #gzip on; + + server { + listen 80; + absolute_redirect off; #取消绝对路径的重定向 + sendfile on; + default_type application/octet-stream; + + gzip on; + gzip_http_version 1.1; + gzip_disable "MSIE [1-6]\."; + gzip_min_length 256; + gzip_vary on; + gzip_proxied expired no-cache no-store private auth; + gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript; + gzip_comp_level 9; + + root /usr/share/nginx/html; + + location / { + # same docker config + root /usr/share/nginx/html; + index index.html; + # support history mode + try_files $uri $uri/ /index.html; + } + + + + # 后端服务 + location ^~ /api/ { + proxy_pass http://nest-admin-server:7001/api/; # 转发规则 + proxy_set_header Host $proxy_host; # 修改转发请求头,让目标应用可以受到真实的请求 + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # websocket服务 + location ^~ /ws-api/ { + proxy_pass http://nest-admin-server:7002/ws-api/; + proxy_read_timeout 300s; + proxy_send_timeout 300s; + + proxy_set_header Host $host; + proxy_set_header X-real-ip $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + proxy_http_version 1.1; + } + # 文件访问 + location ^~ /upload/ { + proxy_pass http://nest-admin-server:7001/upload/; + proxy_set_header Host $proxy_host; # 修改转发请求头,让目标应用可以受到真实的请求 + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + } +}