解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404

最开始的配置:

nginx 复制代码
server {
    listen      7000;
    server_name  localhost;

    location /api {
        proxy_pass http://111.111.211.111:1234;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS , PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    }

    location / {
        root   html/test7000;
        index  index.html index.htm;
    }
}

错误现象:

http://localhost:7000正常,http://localhost:7000/map 报错404

核心结论

现有配置本身语法没问题,但逻辑不能支持 /map 访问

nginx 复制代码
location / {
    root   html/test7000;
    index  index.html index.htm;
}

location / 会匹配所有请求 :/、/map、/map/xxx

  1. 访问 [http://localhost:7000](http://localhost:7000)

    匹配 /,去 html/test7000/index.html 拿到页面,页面内部JS代码做了前端路由跳转 ,跳到 /map(不是nginx跳转!这点很关键),页面本身正常打开。

  2. 访问 [http://localhost:7000/map](http://localhost:7000/map)

    nginx 会到目录:html/test7000/map 去找文件。

    你的打包文件直接放在 test7000 下,不存在 map 这个子文件夹,所以直接404。

✅ 关键点区分:

  • / 打开页面 → 前端SPA路由在浏览器里把地址改成 /map
  • 直接访问 /map,请求发给nginx,nginx去磁盘找 test7000/map,不存在 →404

解决方案:保留原有目录,修改nginx,SPA刷新不404(推荐)

nginx 复制代码
server {
    listen      7000;
    server_name  localhost;

    location /api {
        proxy_pass http://111.111.211.111:1234;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS , PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    }

    location / {
        root   html/test7000;
        index  index.html index.htm;
       # 增加这一行:SPA兜底,任意路径都返回index.html
        try_files $uri $uri/ /index.html;**
    }
}

修改后:

  1. [http://localhost:7000](http://localhost:7000) 正常打开
  2. [http://localhost:7000/map](http://localhost:7000/map) → nginx找不到map文件,命中try_files,返回index.html,前端接管路由,页面正常渲染
  3. /map/xxx 子路由刷新也不会404

⚠️ 注意:不用改文件夹,不用alias,只加 try_files,这就是你当前场景最简单的修复!

一句话总结

/ 能打开页面是因为加载了index.html,前端JS把地址栏改成/map ;直接访问/map,nginx会去硬盘找map文件,找不到就404。加上try_files $uri $uri/ /index.html; 即可解决SPA刷新404问题。

加上try_files之后,你直接访问 [http://localhost:7000/map](http://localhost:7000/map) 就能正常打开页面了,你可以直接测试。

相关推荐
杨凯凡8 小时前
Spring Boot SSE 上线后不实时、频繁断线?Nginx 缓冲、心跳与连接回收实战
spring boot·后端·nginx
Csvn11 小时前
Nginx 缓存体系:三层 key、七态 X-Cache-Status 与锁/stale 策略
后端·nginx
Csvn1 天前
Nginx 负载均衡进阶与健康检查:哈希、keepalive 池与 DNS 自愈
后端·nginx
橘色的喵2 天前
不接 SRS,只用 Nginx 做 WebRTC 播放:架构与踩坑
nginx·webrtc·srs·rtmp
Csvn2 天前
Nginx rewrite 与 server_name:last/break 语义、多域名匹配与伪静态
后端·nginx
亚那迦基2 天前
24小时高危漏洞速报,互联网惊天危机来临!PHP、Java、Node.js、NGINX、GitLab无一幸免,看看你家是否中招
nginx·gitlab·php
tonydf3 天前
没完了,Nginx又报新漏洞!
运维·nginx
飞奔的瓜牛3 天前
Linux 下使用 OpenSSL 生成自签名证书完整指南
运维·服务器·nginx
JaguarJack3 天前
NGINX 两处漏洞可导致未经身份验证的远程代码执行(CVE-2026-123633 和 CVE-2026-123007)
后端·nginx·nginx2root
BingoGo3 天前
NGINX 两处漏洞可导致未经身份验证的远程代码执行(CVE-2026-123633 和 CVE-2026-123007)
后端·nginx