簡體   English   中英

在heroku上為Django / NodeJS應用設置node-http-proxy

[英]Setting up node-http-proxy for Django/NodeJS app on heroku

我正在部署一個也使用nodeJS的Django應用程序,我偶然發現

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

我能夠設置buildpacks和procfile,但是在設置node-http-proxy時遇到問題

所以我想這部分使我感到困惑(從上面的鏈接):

當然,Web dyno上仍然只有1個進程可以綁定到PORT。 通過添加環境變量(我們將其稱為DJANGO_PORT)並將node-http-proxy添加到我們的節點腳本中,我們能夠將節點綁定到PORT,並將所有正常的網絡流量代理到127.0.0.1之上的Django。 產生的Procfiles看起來像這樣:

我已經將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)

需要幫助,至少要知道我的想法或理解是否正確。 只想知道我是否在正確的思考方向上

提前致謝!

解決了

對於想在heroku上的單個dyno上運行Django和NodeJS的任何人。

這就是我所做的

我通過Heroku CLI將自定義buildpack連同heroku / python和heroku / nodejs(多個buildpack)一起添加到了我的應用程序中

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

創建的Procfile

web: bin/runsvdir-dyno

Procfile.web ,其中DJANGO_PORT在Heroku的環境中

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')
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM