1

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

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

当我使用它时,我会502 proxy error进入 Chrome 控制台。服务器找不到我的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);

我更喜欢第二种方法,所以如果有人可以帮助我让它工作,我会非常感激。

4

1 回答 1

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);
于 2013-03-15T08:02:41.613 回答