更新 Nginx 配置,优化路径处理逻辑,统一未命中路径返回网页入口,移除不必要的代理设置

This commit is contained in:
2026-06-09 09:30:43 +08:00
parent 04a8912307
commit 0b958a0ebb

View File

@@ -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;
}
}