title: nginx, node.js + socket.io with SSL
body: <p>I'm having a problem configuring nginx and node to support socket.io over SSL.</p>
  
  <p>My nginx config:</p>
  
  <pre><code>map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
  }
  
  server {
      listen 80;
      listen 443 ssl;
      listen [::]:80;
      listen [::]:443 ssl;
  
      access_log /var/log/nginx/livetest.log;
      server_name live-test.dev www.live-test.dev;
  
      ssl_certificate /etc/nginx/ssl/nginx.crt;
      ssl_certificate_key /etc/nginx/ssl/nginx.key;
      ssl_session_cache shared:SSL:50m;
      ssl_session_timeout 5m;
  
      if ($ssl_protocol = "") {
          rewrite ^ https://$host$request_uri? permanent;
      }
  
      location / {
          proxy_pass https://live_test;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
      }
  }
  </code></pre>
  
  <p>live_test is upstream for node.js running on port 6020. When testing in chrome it does stop on polling with (failed) status. When using wscat:</p>
  
  <p>wscat --connect wss://live-test.dev</p>
  
  <p>I receive:
  <code>error: Error: self signed certificate</code></p>
  
  <p>I'm wondering what may be wrong here? Here is my node.js app:</p>
  
  <pre><code>var express = require('express');
  var cookie = require('cookie');
  var app = express();
  var http = require('http').Server(app);
  var socketIo = require('socket.io');
  var redis = require('redis');
  var redisClient = client = redis.createClient();
  
  io.on('connection', function(socket){
      var cookies = cookie.parse(socket.handshake.headers.cookie);
  
      console.log(cookies);
  });
  
  http.listen(6020, function(){
      console.log('listening on 6020');
  });
  </code></pre>
  
  <p>I have a feeling I'm missing something in my node.js app. I thought that since nginx handle SSL node.js does not have to anymore, but perhaps I'm mistaken.</p>
  
  <p>And yes, I'm using self-signed certificate for SSL. Will node.js / socket.io work with self-signed cert?</p>
  
  <p>@UPDATE</p>
  
  <p>Following some reading I changed my node.js app:</p>
  
  <pre><code>var express = require('express');
  var cookie = require('cookie');
  var fs = require('fs');
  var app = express();
  var https = require('https').Server(app, {
      key: fs.readFileSync('/etc/nginx/ssl/nginx.key'),
          cert: fs.readFileSync('/etc/nginx/ssl/nginx.crt'),  
  });
  var socketIo = require('socket.io');
  var redis = require('redis');
  var redisClient = client = redis.createClient();
  
  var io = new socketIo(https);
  
  io.on('connection', function(socket){
      var cookies = cookie.parse(socket.handshake.headers.cookie);
  
      console.log(cookies);
  });
  
  https.listen(6020, function(){
      console.log('listening on 6020');
  });
  </code></pre>
  
  <p>@UPDATE2</p>
  
  <p>Following the comment by <a href="https://stackoverflow.com/questions/42648879/nginx-node-js-socket-io-with-ssl#comment72425593_42648879" title="abcdn">abcdn</a> I did try wscat with -n flag, now getting error:
  <code>error: Error: unexpected server response (502)</code></p>
  
  <p>while nginx error.log contains:
  <code>2017/03/07 13:44:10 [error] 10556#10556: *140 upstream prematurely closed connection while reading response header from upstream</code></p>
  
  <p>@UPDATE 3</p>
  
  <p>After further reading, I turned my app.js back to http.</p>