在 Heroku 上使用 Node.js 的代理服务器 [英] Proxy server with Node.js on Heroku

查看:43
本文介绍了在 Heroku 上使用 Node.js 的代理服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http-proxy 在 Heroku 上使用 Node.js 构建代理服务器.在本地一切正常,但我在 Heroku 上遇到了一些问题.

I'm trying to build a proxy server on with Node.js on Heroku using http-proxy. Everything works fine locally, but I'm having some troubles on Heroku.

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

settings = {
  "localhost": process.env.LOCALHOST,
  "devices":   process.env.DEVICES_URI
}

var options = { router: { } }
options.router[settings.localhost + '/devices']  = settings.devices + '/devices';

var port   = process.env.PORT || 8000;
var server = httpProxy.createServer(options).listen(port);

正如您在示例中看到的,我设置了一个路由对象.我要说的是:当请求与/devices"匹配时,将请求路由到设备服务.(由 DEVICES_URI 环境变量标识)

As you can see the in the example I set a routing object. What I say is this: when a request matches '/devices' then route the request to the the device service. (identified by the DEVICES_URI environmental var)

在开发中我设置

  • LOCALHOST = '本地主机'
  • DEVICES_URI = 'http://localhost:3000'

这意味着所有发往 localhost:8000/devices 的请求都被代理到localhost:3000/devices 这是我想要的.一切正常.

This means that all requests going to localhost:8000/devices are proxied to localhost:3000/devices which is what I want. All works perfectly.

问题出在生产中.它给了我多次重复的超时错误对于每个请求.

The problem is in production. It gives me a timeout error repeated multiple times for every request.

2012-08-23T20:18:20+00:00 heroku[router]: Error H12 (Request timeout) -> GET lelylan-api.herokuapp.com/devices dyno=web.1 queue= wait= service=30000ms status=503 bytes=0

在生产环境变量被配置为应用程序名称.

In the production the environment vars are configured to the app names.

  • LOCALHOST = 'lelylan-api.herokuapp.com'
  • DEVICES_URI = 'lelylan-devices.herokuapp.com/'

我想我错的是一些配置,但一整天后我仍然无法弄清楚.

I guess I'm wrong is some configurations, but after the whole day I'm still not able to figure it out.

更新

我继续进行测试,发现代理无法访问代理服务,这完全阻止了我.

I've continued with my tests and I've seen that the proxy is not able to reach the proxied service which totally stops me.

在开发中我设置:

  • LOCALHOST = '本地主机'
  • DEVICES_URI = 'lelylan-devices.herokuapp.com/'

如果我调用 http://lelylan-devices.herokuapp.com/devices 一切正常很好.

If I call http://lelylan-devices.herokuapp.com/devices everything works fine.

如果我调用 localhost:8000/devices(指向 http://lelylan-devices.herokuapp.com/devices) Heroku 告诉我没有这样的应用程序.我想问题出在路由系统中.

If I call localhost:8000/devices (which points to http://lelylan-devices.herokuapp.com/devices) Heroku tells me there is no such an app. I guess the problem is somehow in the routing system.

您可以在此处访问源代码.这里是 Heroku 的配置变量.

Here you can access at the source code. Here the configuration vars for Heroku.

NODE_ENV      => production
LOCALHOST     => lelylan-api.herokuapp.com
DEVICES_URI   => lelylan-devices.herokuapp.com
TYPES_URI     => lelylan-types.herokuapp.com
LOCATIONS_URI => lelylan-locations.herokuapp.com

推荐答案

我终于使用稍微修改的版本让它工作了 按 url 代理.最终代码看起来像这样并且工作正常.

I finally made it work using a slightly modified version proxy-by-url. The final code looks something like this and works fine.

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

var port = process.env.PORT || 8000;

var routing = {
  '/devices': { port: process.env.DEVICES_PORT || 80, host: process.env.DEVICES_URI }
}

var server = httpProxy.createServer(
  require('./lib/uri-middleware')(routing)
).listen(port);

要记住的一个注意事项.该插件将标头 HOST 设置为目标应用程序 uri.如果您不这样做,Heroku 将无法识别该应用程序并且找不到它,因为其内部路由系统似乎基于 HOST 标头.

One note to remember. The plugin sets the header HOST to the destination application uri. If you do not do so, Heroku will not recognize the app and will not find it, as its internal routing system seems to be based on the HOST header.

这篇关于在 Heroku 上使用 Node.js 的代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆