nginx反向代理没有转发http请求?

开发阶段,前端的服务器是localhost:8080,后端服务器是localhost:8088,涉及跨域,所以用nginx做反向代理使http://localhost:8080/api开头的http请求都转变成http://localhost:8088/api,nginx配置如下
图片描述

结果一直是404
图片描述

用postman测试后端接口,显示正常
图片描述

查看任务管理器,nginx处于运行状态

nginx的访问日志无记录,错误日志也无记录,以下为错误日志最后的内容
图片描述
不知道到底是哪里出了问题

阅读 13.9k
3 个回答

少了一个 /api

根据题主的要求
需要把 http://localhost:8080/api => http://localhost:8088/api

但是

location ^~ /api/ {
    proxy_pass http://localhost:8088/;
    ...
}

实现的是 http://localhost:8080/api => http://localhost:8088/
所以需要访问 http://localhost:8080/api/api 才可以访问到真实的 端点.
改成

location ^~ /api/ {
    proxy_pass http://localhost:8088/api;
    ...
}

即可

你那个server_name 不是127.0.0.1吗?
localhost不一定就代表127.0.0.1吧?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题