From 58d5713ec11a1a3566f67354581ef2c0c7a10c85 Mon Sep 17 00:00:00 2001 From: 2910410219 <2910410219@qq.com> Date: Tue, 9 Jun 2026 15:27:30 +0800 Subject: [PATCH] =?UTF-8?q?nginx=E9=85=8D=E7=BD=AE=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0=E7=9A=84=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=EF=BC=88API=E8=B7=AF=E5=BE=84=EF=BC=89=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E5=88=B0=E5=90=8E=E7=AB=AF=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ngnix.conf | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/ngnix.conf b/ngnix.conf index 50cb987..6f9e83b 100644 --- a/ngnix.conf +++ b/ngnix.conf @@ -1,4 +1,4 @@ -# Nginx 静态文件服务 + 自定义 404 页面 +# Nginx 静态文件服务 + 智能代理 + 自定义 404 页面 # HTTP 重定向到 HTTPS server { @@ -19,7 +19,7 @@ server { ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; - # 使用项目自定义静态 404 页面,保持 nginx 返回 404 状态码 + # 自定义 404 页面,保持 404 状态码 error_page 404 /404.html; location = /404.html { @@ -43,17 +43,36 @@ server { # 网页端(public/web/index.html -> dist/web/index.html) location /web/ { alias /usr/share/nginx/html/web/; - try_files $uri $uri/ =404; + try_files $uri $uri/ /web/index.html; } # 后台管理端(dist/index.html,前缀 /sys/) location /sys/ { alias /usr/share/nginx/html/; - try_files $uri $uri/ =404; + try_files $uri $uri/ /index.html; } - # 其他静态资源或任意路径,未命中时统一交给 nginx 404,再由自定义 404 页面展示 + # 1. 先尝试作为静态文件查找 location / { - try_files $uri $uri/ =404; + 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; } }