And*_*ato 7 proxy heroku node.js
我正在尝试使用http-proxy在Heroku上使用Node.js构建代理服务器.一切都在当地运作良好,但我在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);
Run Code Online (Sandbox Code Playgroud)
正如您在示例中所看到的,我设置了一个路由对象.我所说的是:当请求与'/ devices'匹配时,请将请求路由到设备服务.(由DEVICES_URI环境变量识别)
在开发我设置
这意味着所有发往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
Run Code Online (Sandbox Code Playgroud)
在生产环境中,环境变量被配置为应用程序名称.
我想我错了一些配置,但经过一整天我仍然无法弄明白.
更新
我继续我的测试,我已经看到代理无法访问完全阻止我的代理服务.
在开发中我设置:
如果我打电话给http://lelylan-devices.herokuapp.com/devices一切正常.
如果我调用localhost:8000/devices(指向http://lelylan-devices.herokuapp.com/devices)Heroku告诉我没有这样的应用程序.我想这个问题在某种程度上是在路由系统中.
在这里,您可以访问源代码.这里是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
Run Code Online (Sandbox Code Playgroud)
我终于使用稍微修改过的版本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);
Run Code Online (Sandbox Code Playgroud)
要记住一个注意事项.该插件将标头HOST设置为目标应用程序uri.如果您不这样做,Heroku将无法识别该应用程序,也无法找到它,因为其内部路由系统似乎基于HOST标头.
我在 Heroku 上成功使用 http-proxy。我在你的日志错误中注意到的第一件事是它正在获取的网址:GET lelylan-api.herokuapp.com/tdevices
有一个拼写错误...而不是“/devices”,而是显示“/tdevices”。在继续之前,您能否确认这是实际的日志消息?
| 归档时间: |
|
| 查看次数: |
10170 次 |
| 最近记录: |