13

我正在尝试将流量从我的测试应用程序的 /api/* url 重定向到我在 Heroku 上托管的 api。

因此,应该将 localhost/api/hello 代理到 testapp.heroku.com/hello 并返回响应。

使用 node-http-proxy 在 localhost 到 localhost 上完美运行,但是当我将它指向 myapp.heroku.com 时,我收到此错误:

Heroku | No such app
There is no app configured at that hostname.
Perhaps the app owner has renamed it, or you mistyped the URL.

我有一种感觉是 Heroku 的路由系统在伪造我的代理请求,我还没有找到解决它的方法。有任何想法吗?

4

1 回答 1

17

在将请求代理到不同域时,我看到了类似的情况。我使用的解决方法是修改代理请求上的主机头以匹配远程站点期望的域名。因此,在您的情况下,代码如下所示:

var http = require('http'),
    httpProxy = require('http-proxy');


var server = httpProxy.createServer(function (req, res, proxy) {
  req.headers.host = 'myapp.heroku.com';
  proxy.proxyRequest(req, res, {
    port: 80,
    host: 'myapp.heroku.com'
  });
}).listen(9000);

我很想知道这是否适合你。

于 2011-06-22T19:59:03.160 回答