首页  >  问答  >  正文

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

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

结果一直是404

用postman测试后端接口,显示正常

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

nginx的访问日志无记录,错误日志也无记录,以下为错误日志最后的内容

不知道到底是哪里出了问题

天蓬老师天蓬老师2713 天前534

全部回复(2)我来回复

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 17:09:08

    少了一个 /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 => http://localhost:8088/
    所以需要访问 http://localhost:8080/api/api所以需要访问 http://localhost:8080/api/api 才可以访问到真实的 端点.
    改成

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

    即可

    回复
    0
  • 黄舟

    黄舟2017-05-16 17:09:08

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

    回复
    0
  • 取消回复