Bad Gateway error using Socket.io Node.js over SSL

I previously had a Socket.io script working perfectly on http, but updating to https violated it. I installed the certificate on the server, but no luck. Code to configure the server:

var https = require('https'), fs = require('fs'); var options = { key: fs.readFileSync('/etc/nginx/ssl/default/54082/server.key'), cert: fs.readFileSync('/etc/nginx/ssl/default/54082/server.crt') }; var app = https.createServer(options); var io = require('socket.io').listen(app); 

However, the page cannot connect to it in a web browser, and the response of the server responded with a status of 502 (Bad Gateway) displayed on the console.

Any ideas on if the script setting is incorrect? Or something in setting up Nginx?

Thank you very much

Edit: The front-end code that I use to connect is:

 <script type="text/javascript" src="https://socket.example.com/socket.io/socket.io.js"></script> <script> var io = io('https://socket.example.com', { secure: true }); </script> 

Edit :: Nginx configuration:

 # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/socket.example.co.uk/before/*; server { listen 443 ssl; server_name socket.example.co.uk; root /home/forge/socket.example.co.uk; # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/socket.example.co.uk/server/*; location / { proxy_pass https://socket.example.co.uk:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } # FORGE CONFIG (DOT NOT REMOVE!) include forge-conf/socket.example.co.uk/after/*; 
+5
source share
3 answers
  • make sure your domain points to your server.
  • make sure that the nginx server block with ssl enabled is running for your domain.
  • remove any location blocks from your nginx configuration trying the proxy server to the port where socket.io server is running.
  • make sure your ssl certificate is valid.
  • connect to io.connect() instead of how you do it. Leave the protocol portion of the URL ( https:// ).
  • use sudo killall -9 node from the command line to kill any zombie processes that might linger and bind to your port. this sometimes happens with socket.io when it cannot shut down normally.

example from my own code:

 var socket = io.connect('dev.somedomain.com:3000',{secure:true}); 

Example server from my own domain:

 var fs = require('fs'), https = require('https'), config = JSON.parse(fs.readFileSync('./config.json','utf-8')), serverOpts = { key: fs.readFileSync(config.server.key), cert: fs.readFileSync(config.server.cert) }, server = https.createServer(serverOpts,function(req,res){}), io = require('socket.io').listen(server); io.on('connection', function(socket){ console.log('houston, we have lift off'); }); server.listen(config.port, function(){ log('listening on *:%d',config.port); }); 

Obviously, I am reading the path to my certificate and key file from the config.json file, but should you get the idea right?

+4
source

502 (Bad Gateway) indicates that the nginx service tried to contact the proxy server but failed. String in nginx configuration

  proxy_pass https://socket.example.co.uk:3000; 

seems like a problem. Failed to see from your nodejs code that port 3000 is being used.

+2
source

I have the same problem, but with apache2, I solve it by adding this line to my vhost configuration

SSLProxyEngine on SSLProxyVerify none SSLProxyCheckPeerCN off SSLProxyCheckPeerName off SSLProxyCheckPeerExpire off

-2
source

Source: https://habr.com/ru/post/1244100/


All Articles