Skip to content

How to serve node Socket IO connections with NGINX reverse Proxy

Last updated on June 4, 2022

In this post, I would like to show you the configuration of Nginx web server to serve Socket IO connections with Nginx reverse proxy.

Open your Nginx configuration file with vi or any other editor:

$ sudo vi /etc/nginx/sites-available/default

Now update your server block same as below shown. It should look something like this:

server {
    
    listen 80;
 
    server_name your_domain.com;
 
    # Requests for socket.io are passed on to Node on port 3000
    location ~* \.io {
      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://private_ip_address:3000;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

}

Replace your_domain.com with your ec2 domain name and private_ip_address with the private ip address associated ( which you obtained previously)

Save and quit file using :wq vi command.

Test the configuration of Nginx using the command below:

$ sudo nginx -t

Then reload Nginx if OK is displayed.

$ sudo /etc/init.d/nginx reload
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments