GV.

Monday, September 27 2021

How To Connect Socket IO when using Nginx proxy in AWS BeanStalk

Is your socket.io server is connecting in localhost but not in the deployed server in AWS Beanstalk?

Well the problem is, Beanstalk uses proxy for your NodeJS application, in the default case which will be Nginx. We need allow the web socket connection to pass through the Nginx and connect to our NodeJs application to fix this issue.

Solution

Create a server conf file to enable pass through socket connection.

File Path and name from your project root
.ebextensions/nginx/conf.d/elasticbeanstalk/my-server-conf.conf

server {
    # Proxy Config
    location / {
        proxy_pass http://strapi;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }
}