4

I am trying to use SSL on my application running socket.io with express and nginx but I can't make it work. I have done my research but none of what I found worked.

I keep having the error : ERR_CONNECTION_CLOSED with not http status code on client side.

GET https://subdomain.mywebsite.com:1339/socket.io/?EIO=3&transport=polling&t=LIgxHmz net::ERR_CONNECTION_CLOSED

Here is my nginx configuration :

server {
listen 443;
server_name subdomain.mywebsite.com;

root /usr/share/nginx/html;
index index.html index.htm;

ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/subdomain.mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.mywebsite.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:1339/;
    proxy_redirect off;

    # Socket.IO Support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

Here is the server side :

var express = require('express'),

var app = express();

var server = require('http').createServer(app);
var io = require('socket.io')(server);

server.listen(1339);

...

Here is the client side : subdomain.mywebsite.com

var socket = io.connect("https://subdomain.mywebsite.com:1339");

The page loads nicely, no error on server side but no connection to socket.io. Everything worked flawlessly before I tried to switch to SSL.

What am I doing wrong ?

Sharp'
  • 93
  • 1
  • 7
  • Depending on the version of nginx that you're using it might be as simple as moving the ssl flag. http://stackoverflow.com/questions/30317831/socket-io-nodejs-nginx-ssl – aembke May 13 '16 at 17:26
  • I tried but nothing changed. Express is serving my html pages in https just fine. Only socket.io is failing with it. – Sharp' May 13 '16 at 17:36
  • Aren't you supposed to connect from the client using the `wss` protocol? – nothankyou May 14 '16 at 01:37
  • Tried that too, still the same. – Sharp' May 14 '16 at 17:02
  • I have had the same requirements in the past, and I had managed to find the solution that works. Please refer to my question and answer here for my nginx+express+client configuration that might help you: http://stackoverflow.com/q/30673664/1874155 – Andrew Kapunin Aug 16 '16 at 22:30
  • same error here, did you ever find a solution> – Iannazzi Feb 21 '18 at 18:40

1 Answers1

1

Try this configuration. Please make sure to have a correct SSL certificate and key. If you test on local you can easily use mkcert tool to generate SSL certificate to local testing.

server {
   listen 80;
   server_name <your server_name>;
   return 301 https://<your server_name>$request_uri;
}

server {
   listen 443 ssl;


   ssl_certificate <your certificate path>;  # better if you put them at /etc/nginx/ssl/
   ssl_certificate_key <your certificate_key path >;

   server_name <your server_name>;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Client-Verify SUCCESS;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_pass http://localhost:3000;
      proxy_redirect off;
      proxy_buffering off;
   }
}
Umanda
  • 4,737
  • 3
  • 23
  • 28