1

I want to run my node.js app on port 80 on Apache server. I have tried 2 methods one via 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>

When I use this I get 502 proxy error in Chrome console. The server cannot find my CSS and Socket.io and other JS files.

UPDATE: I solved this error for CSS and normal .js files by putting http://domainName.com:8080/ in front of the links. But the problem still exist for socket.io! Socket.io cannot be found!

And the second method is using http-proxy module (this is the example I found, please see the comment below this post):

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

Which was explained here: how to put nodejs and apache in the same port 80

I don't know how to get this one working for my code, since I'm using Express.

This is my relevant part of code:

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

I prefer the second method, so if someone could help me get it to work I'd really appreciate it.

Community
  • 1
  • 1
Loolooii
  • 8,588
  • 14
  • 66
  • 90
  • Are you using that http-proxy code literally? You have to configure it to listen to port 80 and proxy to port 8080. – Andreas Hultgren Mar 14 '13 at 13:49
  • @AndreasHultgren no, not literally. I did change it to listen to 80 and proxy to 8080. If you look in the link I provided, the example doesn't contain anything about express, which I'm using. – Loolooii Mar 14 '13 at 13:51
  • @AndreasHultgren this is the link to that example: http://stackoverflow.com/a/11174042/163799 – Loolooii Mar 14 '13 at 13:52
  • Yes, but what framework your app uses doesn't matter to the proxy. As far as the app is concerned, it receives traffic on port 8080 and replies to port 8080. However for using websockets you'll need some additional proxy-stuff, see [the docs on websockets](https://github.com/nodejitsu/node-http-proxy#proxying-websockets). – Andreas Hultgren Mar 14 '13 at 14:00

1 Answers1

3

For getting socket.io to work through node http-proxy with multiple domains, here's my setup. I'm not sure if it's the best one (I would like it to work with a router object) but it works.

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);
Andreas Hultgren
  • 14,763
  • 4
  • 44
  • 48
  • setMaxSockets means max number of users logged in right? In my case? – Loolooii Mar 18 '13 at 13:53
  • And in this line: var server = http.createServer(app); I give my express app as an argument to createServer(), but how do I do this in this case? Thanks. – Loolooii Mar 18 '13 at 13:56
  • 1
    setMaxSockets is the maximum number of socket connections at the same time. I think it's the best to run the proxy as a separate app, especially if it's proxying to different apps/domains. Thus you would put the above in say `start.js` and run it with `node start.js` and run you own app as you usually do. You shouldn't need to change anything in the app itself. – Andreas Hultgren Mar 18 '13 at 14:34