Search code examples
node.jsapachesocket.ioproxypass

Apache ProxyPass and Node.js - not serving up socket.io


I'm trying to run a Node.js alongside my existing apache and I have a ProxyPass problem when serving content from my Express.js server. What's seems to be the trouble is the request that the Node server sees when it is forwarded by Apache.

I tried this config:

<VirtualHost *:80>
    DocumentRoot "/var/www/html"
    ProxyPreserveHost on
    ProxyPass /node http://localhost:3000/
    ProxyPassReverse /node http://localhost:3000/
</VirtualHost>

Node is setup the server like such:

var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require("socket.io").listen(server) 

app.use(function(req, res, next){
    console.log(req.method, req.url); // log the method and route
    next();
});
app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/components'));
app.use('/js', express.static(__dirname + '/js'));

I get this logged by node:

GET /
GET //components/bootstrap/dist/css/bootstrap.css
GET //components/font-awesome/css/font-awesome.css
GET //css/flags.css
GET //css/app.css
GET //components/jquery/dist/jquery.js
GET //components/bootstrap/dist/js/bootstrap.js
GET //socket.io/socket.io.js
GET //js/client.js
GET //components/bootstrap/dist/css/bootstrap.css.map

Question: I am getting an extra slash when apache sends the request and the socket.io library is not served. How can I make Apache remove leading slashes before they get to node.js?


Solution

  • I ended up going with HAProxy to sit in front of node and apache; as suggested in the comments above.

    These are the settings I went with:

     frontend main
        bind *:80
        mode http
        default_backend apache
    
        acl is_node path_beg -i /node/
        use_backend node if is_node
    
        acl is_node_too path_beg -i /node
        use_backend node if is_node_too
    
        acl is_socket path_beg -i /socket.io/
        use_backend socketio if is_socket
    
        acl is_socketio_too path_beg -i /socket.io
        use_backend socketio if is_socketio_too
    
    backend apache
        mode http
        balance     roundrobin
        server      apache localhost:81
    
    backend node
        mode http
        balance     roundrobin
        server      node localhost:3000
        reqrep ^([^\ :]*)\ /node/(.*)     \1\ /\2
        reqrep ^([^\ :]*)\ /node(.*)     \1\ /\2
    
    backend socketio
        mode http
        balance     roundrobin
        server      node localhost:3000
        reqrep ^([^\ :]*)\ /node/(.*)     \1\ /\2
        reqrep ^([^\ :]*)\ /node(.*)     \1\ /\2