简体   繁体   中英

Socket.IO, SSL, nginx, directory path

So I use a standalone version of socket.io (server) -> I don't declare any "http server" It looks pretty much like this:

var io = require('socket.io')();
io.on('connection', function(socket){});
io.listen(3001);

The problem: my website in production environment is fully HTTPS . When I tried to connect to it ( io.connect("https://www.mysite.com:3001") ) I got ERR_SSL_PROTOCOL_ERROR (tested in Chrome browser).

When I turned off the server, I got ERR_CONNECTION_REFUSED. So I came to a conclusion that Node.JS cannot handle the HTTPS/SSL connection.

My development environment is HTTP-based, so I have no problem there - it works like a charm.

So, then I decided to try proxy-passing the HTTPS to HTTP connection, using nginx.

AFAIK, here I have only couple of options in order to distinguish between Node.JS connection and main app connection:

a) Subdomain (~ node.mysite.com)

b) Directory (~ mysite.com/node)

I chose the latter option, because it felt much easier to implement.

So I added the following inside of a main "server" config (before the main app):

location /node {
   proxy_pass http://127.0.0.1:3001;
   proxy_redirect off;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
}

And then I tried connecting: io.connect("https://www.mysite.com/node") , but no matter what I get 404 (Not Found) . I even tried this: io.connect("https://www.mysite.com", {path : /node/}) (w and w/o slashes) and tried renaming path to resource and many more.

But no avail.

When I visit this page in the browser (and when the server is up and running) - I see a white page.

I'm I doing something terribly wrong? Can this be fixed? Thank you all in advance

I also used the socket.IO connection on SSL like this .. it work fine to me ...i am using Express Engine

 var options = {
  key: fs.readFileSync('./key.pem', 'utf8'),
  cert: fs.readFileSync('./server.crt', 'utf8')
 };

 var app = require('./app');
 var server = require('https').createServer(options, app),
 io = require('socket.io').listen(server);
 server.listen(port);
 io.sockets.on('connection', function (socket) {
 });

and on Client side

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('https://localhost:2406');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){});
</script>

Decided to go with a subdomain. Because I wanted to delegate the connection handling to Nginx instead of doing it with the Node.JS itself. :)

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