【问题标题】:Setting up node-http-proxy for Django/NodeJS app on heroku在 heroku 上为 Django/NodeJS 应用程序设置 node-http-proxy
【发布时间】:2018-03-15 21:17:55
【问题描述】:

我正在部署一个也使用 nodeJS 的 Django 应用程序 就这样,我偶然发现了

https://blog.heroku.com/heroku-django-node

我能够设置 buildpacks 和 procfiles 但我在设置 node-http-proxy 时遇到问题

所以我想这部分让我感到困惑(来自上面的链接):

当然,web dyno 上仍然只有 1 个进程可以绑定到 PORT。通过添加一个环境变量(我们称之为 DJANGO_PORT)并将 node-http-proxy 添加到我们的节点脚本中,我们能够将节点绑定到 PORT 并将所有正常的 Web 流量代理到 Django 超过 127.0.0.1。生成的 Procfile 看起来像这样:

我已经将 env 变量添加到我的 heroku 应用程序中。

2 个问题

  • 这是您在 server.js 中正确绑定 PORT 的方式吗? 我有:
const PORT = process.env.PORT
httpProxy.createProxyServer({target:'app_url:8080'}).listen(PORT);
  • 8080 是我的 DJANGO_PORT,我希望这也会将流量路由到我的 django 服务器?

我收到上述错误:

    /app/node_modules/http-proxy/lib/http-proxy/index.js:119
     throw err;
     ^
 
 Error: connect ECONNREFUSED {ip_address}:8080
     at Object._errnoException (util.js:1022:11)
     at _exceptionWithHostPort (util.js:1044:20)
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)

需要帮助,至少要知道我的想法或理解是否正确。 只是想知道我的思维方向是否正确

【问题讨论】:

    标签: node.js django heroku proxy


    【解决方案1】:

    解决了

    对于任何想要在 heroku 上的单个 dyno 上运行 Django 和 NodeJS 的人。

    这就是我所做的

    我通过 Heroku CLI 以及 heroku/python 和 heroku/nodejs(多个 buildpacks)将自定义 buildpack 添加到我的应用程序中

    http://www.github.heroku.com/heroku/heroku_buildpack_runit

    创建Procfile

    web: bin/runsvdir-dyno
    

    Procfile.web,其中 DJANGO_PORT 在 Heroku 的 Envs 上

    django: gunicorn appname.wsgi:application --bind 127.0.0.1:$DJANGO_PORT
    node: node server.js
    

    最终使用node-http-proxy如下。这是我在 3000 端口上运行的节点应用程序

    var webpack = require('webpack')
    var WebpackDevServer = require('webpack-dev-server')
    var config = require('./webpack.local.config')
    var httpProxy = require('http-proxy')
    const PORT = process.env.PORT
    // ::::::::::::::::::Starting from here
    var NODE_PORT = 3000;
    var http = require('http')
    var proxy = httpProxy.createProxyServer({});
    
    http.createServer(function(req, res) {
        // For all URLs beginning with /channel proxy to the Node.JS app, for all other URLs proxy to the Django app running on DJANGO_PORT
        if(req.url.indexOf('/channel') === 0) {
            // Depending on your application structure you can proxy to a node application running on another port, or serve content directly here
            proxy.web(req, res, { target: 'http://localhost:' + NODE_PORT });
    
            // Proxy WebSocket requests if needed
            proxy.on('upgrade', function(req, socket, head) {
                proxy.ws(req, socket, head, { target: 'ws://localhost:' + NODE_PORT });
            });
        } else {
            proxy.web(req, res, { target: 'http://localhost:' + process.env.DJANGO_PORT });
        }
    }).listen(process.env.PORT);
    // :::::::::::::::::::To HERE
    new WebpackDevServer(webpack(config), {
      publicPath: config.output.publicPath,
      hot: true,
      inline: true,
      historyApiFallback: true,
      watchOptions: {
        aggregateTimeout: 300,
        poll: 1000
      },
      headers: { "Access-Control-Allow-Origin": "*" },
    }).listen(3000, config.ip, function (err, result) {
      if (err) {
        console.log(err)
      }
    
      console.log('Listening at ' + config.ip + ':3000')
    })
    

    【讨论】:

      猜你喜欢
      • 2014-11-09
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 2014-05-05
      相关资源
      最近更新 更多