代理服务器与Heroku上的Node.js
我正在尝试使用http-proxy在Heroku上使用Node.js构build代理服务器。 一切工作正常,但我在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环境variables识别)
在发展中,我设定了
- LOCALHOST ='localhost'
- DEVICES_URI ='http:// localhost:3000'
这意味着所有到localhost:8000 / devices的请求被代理到localhost:3000 / devices,这正是我想要的。 所有的作品完美。
问题在于生产。 它给我一个重复多次的每个请求超时错误。
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
在生产环境variables被configuration为应用程序名称。
- LOCALHOST ='lelylan-api.herokuapp.com'
- DEVICES_URI ='lelylan-devices.herokuapp.com/'
我想我错了一些configuration,但整整一天后,我仍然无法弄清楚。
更新
我继续我的testing,我已经看到,代理是无法达到代理服务,完全阻止我。
在开发中,我设置了:
- LOCALHOST ='localhost'
- DEVICES_URI ='lelylan-devices.herokuapp.com/'
如果我打电话http://lelylan-devices.herokuapp.com/devices一切正常。
如果我打电话给本地主机:8000 /设备(指向http://lelylan-devices.herokuapp.com/devices)Heroku告诉我,没有这样的应用程序。 我想这个问题在路由系统中是不知何故的。
在这里你可以访问源代码 。 这里Heroku的configurationvariables。
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
我终于使用了一个稍微修改的版本proxy-by-url 。 最后的代码看起来像这样,工作正常。
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将无法识别应用程序,并且不会find它,因为它的内部路由系统似乎是基于HOST头。
我在Heroku上成功地使用了http代理。 我注意到你的日志错误的第一件事是它正在GET的URL:GET lelylan-api.herokuapp.com/tdevices
有一个错字…而不是'/ devices'它显示'/ tdevices'。 继续之前,你可以确认这是实际的日志消息?