spring boot 接口返回 404 时,先把应用的 context-path、控制器类级路径和方法级路径拼起来,用 curl 直连服务。直连成功后,再检查反向代理转发的路径。

核对控制器和应用前缀
将控制器放在 Spring Boot 启动类所在包或其子包。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
class ProbeController {
@GetMapping("/probe")
String probe() {
return "ok";
}
}

如果 application.properties 配置了 server.servlet.context-path=/demo,且 servlet 使用默认路径,完整路径是 /demo/api/probe。
直连服务定位问题所在
以本地端口 8080 为例,对比完整路径和省略应用前缀的路径。
curl -i http://127.0.0.1:8080/demo/api/probe curl -i http://127.0.0.1:8080/api/probe
第一条应返回 200 和 ok。第二条在上述配置下通常返回 404。如果完整路径仍然失败,可核对监听端口、启动配置和控制器扫描范围。
验证 Nginx 转发前后的路径
若对外路径是 /gateway/api/probe,可以将前缀映射为应用需要的 /demo/。
location /gateway/ {
proxy_pass http://127.0.0.1:8080/demo/;
}
在 Nginx 所在机器检查配置并重新加载后,用对外域名请求 /gateway/api/probe。若直连成功且代理返回 404,核对命中的 location 和转发后的 URI。











