如何使用http-proxy在端口80上运行node.js app?

时间:2013-03-14 13:44:59

标签: node.js express reverse-proxy http-proxy node-http-proxy

我想在Apache服务器上的端口80上运行我的node.js应用程序。我通过Apache尝试了两种方法:

<VirtualHost *:80>
    ServerName domainName.com
    ServerAlias www.domainName.com

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location />
        ProxyPass http://domainName:8080
        ProxyPassReverse http://domainName:8080
    </Location>
</VirtualHost>

当我使用此功能时,我会在Chrome控制台中获得502 proxy error。服务器找不到我的CSSSocket.io以及其他JS个文件。

更新:我通过将http://domainName.com:8080/放在链接前面来解决CSS和普通.js文件的此错误。但socket.io仍存在问题!无法找到Socket.io!

第二种方法是使用http-proxy模块(这是我找到的例子,请看这篇文章下面的评论):

var http = require('http'),
httpProxy = require('http-proxy'),
proxyServer = httpProxy.createServer ({
    hostnameOnly: true,
    router: {
        'domain.com':       '127.0.0.1:81',
        'domain.co.uk':     '127.0.0.1:82',
        '127.0.0.1':        '127.0.0.1:83'
    }
});

proxyServer.listen(80);

这里解释了:how to put nodejs and apache in the same port 80

我不知道如何让这个代码适用于我的代码,因为我正在使用Express

这是我相关的代码部分:

var express = require('express');
var http = require('http');
var io = require('socket.io');
var connect = require('connect');
var sanitize = require('validator').sanitize;
var app = express();
var MemoryStore = express.session.MemoryStore;
var server = http.createServer(app);

var sessionStore = new MemoryStore();
var io = io.listen(server);
app.configure(function() {
    //app.use(express.logger());
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.session({ 
            store: sessionStore, 
            secret: 'someSecret',
            key: 'cookie.sid', maxAge: null }));
            });
app.use(app.router);
app.use("/styles", express.static(__dirname + '/styles'));
app.use("/images", express.static(__dirname + '/styles/images'));
app.use("/js", express.static(__dirname + '/js'));
// routing
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

server.listen(8080);

我更喜欢第二种方法,所以如果有人能帮助我开始工作,我真的很感激。

1 个答案:

答案 0 :(得分:3)

为了让socket.io通过具有多个域的节点http-proxy工作,这是我的设置。我不确定它是否是最好的(我希望它能与路由器对象一起使用)但是它可以工作。

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

httpProxy.setMaxSockets(4096);

var server1 = new httpProxy.HttpProxy({
  target: {
    host: '127.0.0.1',
    port: '3000'
  }
});

var server2 = new httpProxy.HttpProxy({
  target: {
    host: '127.0.0.1',
    port: '3001'
  }
});

var server = http.createServer(function(req ,res){
  switch(req.headers.host){
    case 'server1.com':
      server1.proxyRequest(req, res);
    break;
    case 'server2.com':
      server2.proxyRequest(req, res);
    break;
  }
});

server.on('upgrade', function(req, socket, head){
  // Cases need only for servers that actually use websockets
  switch(req.headers.host){
    case 'server1.com':
      server1.proxyWebSocketRequest(req, socket, head);
    break;
    case 'server2.com':
      server2.proxyWebSocketRequest(req, socket, head);
    break;
  }
});

server.listen(80);