Question

Nginx blocking Socket.io

Posted March 7, 2020 114 views
NginxNode.js

Hello! I used to run my website without Nginx. So, it is just Nodejs who taking care of the socket.io connection and web server task. Then I learn that Nginx would be a better webserver than Nodejs and I can install Certbot to it.
I follow these two tutorials: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04 (to install Nginx) and https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04 (to install Certbot) I got my site up and running. Then I noticed that my socket.io communication is being blocked or I am getting error in the console Failed to load resource: the server responded with a status of 404 (Not Found). I did some research and I found out that I need to complete some steps to get WebSocket to work with Nginx. I tried a few codes I found online but nothing makes sense to me and it still not working.

This what mydomain.com looks like after I install Certbot successfully.

server{
        root /var/www/mydomain.com/html;
        index index.html;

        server_name mydomain.com www.mydomain.com;

        location / {
                 try_files $uri $uri/ =404;
        }

   listen [::]:443 ssl ipv6only=on; #managed by Certbot
   listen 443 ssl;
   ssl_certificate ~too lazy to type~
   ssl_certificate_key ~too lazy to type~
   include ~too lazy to type~
   ssl_dhparam ~too lazy to type~

}
server {
  if($host = www.mydomain.com){
      return 301 https://$host$request_uri;
  } #manage by certbot

  if($host = mydomain.com){
      return 301 https://$host$request_uri;
  } #manage by certbot

  listen 80;
  listen [::]:80;
  server_name mydomain.com www.mydomain.com;
  return 404; #manage by Certbot
}

my server.js look like this:


var express = require('express');
var app2 = express();
var server2 = app2.listen(3700);
var socket = require('socket.io');
var io = socket(server2);
io.sockets.on('connection', onNewConnections);

function onNewConnections(socket){

     socket.on('sendToAll', sendToAllExceptToSender);



    function sendToAllExceptToSender(data){
              socket.broadcast.emit('sendToAllClients',data);

        }
}

I need help with mydomain.com configuration so I can get my socket.io to work. thanks!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @SephiaSky,

What you need to do is to configure your Nginx as a reverse proxy so that it could proxy your HTTP traffic to your backend application.

You can take a look at this tutorial here on how to setup Nginx as a reverse proxy:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Regards,
Bobby

by Brennen Bearnes
Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
  • thanks for the reply! I follow these steps to make nginx reverse proxy

    sudo nano /etc/nginx/sites-available/default
    

    then added this in default

    location /socket.io {
            proxy_pass http://localhost:3700;
            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;
        }
    

    It doesn’t work. I am still getting

    Failed to load resource: the server responded with a status of 404 (Not Found)
    

    Should I edit mydomain.com instead of default?

    • Hi there @SephiaSky,

      This looks correct, however with the current configuration, your application would only be available when accessing yourdomain.com/socket.io.

      If you would like this to be available directly via your domain name your IP address without having to type /socket.io after the hostname, then you can change this line:

      location /socket.io {
      

      To:

      location / {
      ...
      

      Hope that this helps!
      Regards,
      Bobby

Submit an Answer