简体   繁体   中英

nodejs + socket.io over an apache proxy

I am trying since some time to make a nodejs / socket.io server work over an Apache (2.4) reverse proxy. The server works well on my local and on Apache without the proxy. It also works with the reverse proxy without socket.io. So I think I am mostly having trouble with the socket/io proxy config. I tried many many things already ( issues running socket.io over an apache proxy Running Socket.io over Apache Reverse Proxy https://github.com/socketio/socket.io/issues/1696 ) but none of them worked, most probably because my understanding of proxy redirection and socket.io is very shallow. Here is the latest configuration of my files (simplified):

server.js (in /nodejs)

var serverPort =3003
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
});

var io = require('socket.io')(server);

server.listen(serverPort, '127.0.0.1');

io.on('connection', function(socket) {
  console.log('new connection');
  socket.emit('message', 'This is a message from the dark side.');
});

index.html (in /nodejs)

<script src="/socket.io/socket.io.js"></script>
      var socket = io.connect();
      socket.on('message', function(data) {
        alert(data);
      });

This gives some issue already as not able to reach the socket.io.js file (first line) and when provided through direct link then the problem comes on the connect part (as it is not able to reach the server side).

Here is the. bit of my https.conf file that I think is relevant:

<VirtualHost xx.xx.xx.xx:443>

<Proxy balancer://mongrel3003>
BalancerMember http://localhost:3003
</Proxy>
ProxyPass /nodejs balancer://mongrel3003
ProxyPass /socket.io balancer://mongrel3003
ProxyPassReverse /nodejs balancer://mongrel3003
ProxyPassReverse /socket.io balancer://mongrel3003
</VirtualHost>

Any help much appreciated. Thanks

As in the post you linked , You are not actually proxying the websocket....

The Answer is mostly complete:

  1. You need to have the right apache module installed
  2. Follow the guidance in the post you've linked. The apache setup for socket.io is a bit complex but it the posts are mostly correct. IF you tired it and it didn't work, its likely you didn't have the module installed.

If all this doesn't work for you, reach out on twitter or comment on my reply:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM