From 0b958a0ebb97f09091d02e6369bdaef5c620b5e6 Mon Sep 17 00:00:00 2001 From: 2910410219 <2910410219@qq.com> Date: Tue, 9 Jun 2026 09:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Nginx=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E8=B7=AF=E5=BE=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E7=BB=9F=E4=B8=80=E6=9C=AA=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E8=B7=AF=E5=BE=84=E8=BF=94=E5=9B=9E=E7=BD=91=E9=A1=B5?= =?UTF-8?q?=E5=85=A5=E5=8F=A3=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E4=BB=A3=E7=90=86=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ngnix.conf | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/ngnix.conf b/ngnix.conf index aa5cfea..3e4061e 100644 --- a/ngnix.conf +++ b/ngnix.conf @@ -1,4 +1,4 @@ -# Nginx 静态文件服务 + 智能代理 +# Nginx 静态文件服务 + 前端入口兜底 # HTTP 重定向到 HTTPS server { @@ -19,11 +19,23 @@ server { ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; + # 域名下任意未命中路径都不要落到 nginx 默认 404 + error_page 404 = /web/index.html; + # 根路径默认进入网页端 location = / { return 302 /web/index.html; } + # 兼容无斜杠访问 + location = /web { + return 302 /web/; + } + + location = /sys { + return 302 /sys/; + } + # 网页端(public/web/index.html -> dist/web/index.html) location /web/ { alias /usr/share/nginx/html/web/; @@ -36,27 +48,8 @@ server { try_files $uri $uri/ /sys/index.html; } - # 1. 先尝试作为静态文件查找 + # 其他任意路径统一兜底到网页端入口,避免出现 nginx 404 页面 location / { - try_files $uri $uri/ @proxy; - } - - # 2. 无法找到的请求(API路径)代理到后端 - location @proxy { - # 判断 URI 最后一段是否有扩展名 - # 有扩展名返回 404,无扩展名则代理 - if ($uri ~ \.[^./]+$) { - return 404; - } - - proxy_pass http://116.204.74.41:8000; - proxy_http_version 1.1; - 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_set_header X-Forwarded-Proto $scheme; - proxy_connect_timeout 30s; - proxy_send_timeout 30s; - proxy_read_timeout 30s; + try_files $uri $uri/ /web/index.html; } }