求助nginx子路径代理到本机的指定端口服务的配置

场景是用 Nodejs 做 SSR 渲染,服务是 7001 端口,希望客户端访问 https://myaliyun/aaa/xxx 代理到本机的 Nodejs 服务,对于 Nodejs 接收到的请求路径是 /bbb/xxx。

阅读 3.7k
1 个回答

nginx 代理的 proxy_pass 指令根据以下情况,有不同的结果:

  • proxy_pass 后带的是 URI,如 http://localhost/resource(即协议+主机名+资源)这样的内容,将会截取 location 匹配的内容后,添加至代理位置后,例如:

    location /hello {
        proxy_pass http://localhost/resource;
    }

    匹配 http://domain/hello/world 时,转发至代理时将会变成 http://localhost/resource/world

    注意 / 也是一个资源,所以http://localhost/也会截断 location
  • proxy_pass 后带的是单纯的协议+主机名(不加资源),将会直接把客户端的绝对路径转发至代理后端

所以,你的proxy_pass 应该为

location /aaa {
  proxy_pass http://localhost:7001/bbb
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进