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

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

问题描述

我试图使用http-proxy在Heroku上使用Node.js构建代理服务器。
本地一切正常,但在Heroku上遇到一些麻烦。

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

设置= {
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环境变量识别)

在开发中,我设置了
$ b $ ul

  • LOCALHOST ='localhost'

  • DEVICES_URI ='http:// localhost:3000'



  • 这意味着所有到localhost:8000 / devices的请求都会被代理到
    localhost:3000 / devices,这正是我想要的。所有作品完美。



    问题在于生产。它给了我一个重复多次
    的超时错误。

    +00:00 heroku [路由器]:错误H12(请求超时) - > GET lelylan-api.herokuapp.com/devices dyno = web.1 queue = wait = service = 30000ms status = 503 bytes = 0

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




    • LOCALHOST ='lelylan-api.herokuapp .com'

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



    我我猜错了是一些配置,但在整整一天后,我还是
    无法弄清楚。



    更新 strong>



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



    在开发过程中,我设置了:


    • LOCALHOST ='localhost'

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



    如果我打电话给 http://lelylan-devices.herokuapp.com/devices 一切正常。



    如果我调用localhost:8000 / devices(指向 http:// lelylan- devices.herokuapp.com/devices )Heroku告诉我,没有这样的应用程序。我猜这个问题在路由系统中是不知道的。



    在这里您可以访问源代码
    这里Heroku的配置变量。

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


    解决方案

    我终于做到了使用稍微修改后的版本代理网址。最终的代码看起来像这样并且工作正常。

      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标头。

    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);
    

    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)

    In development I set

    • LOCALHOST = 'localhost'
    • DEVICES_URI = 'http://localhost:3000'

    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.

    Update

    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.

    In development I set:

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

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

    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.

    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
    

    解决方案

    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);
    

    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天全站免登陆