Socket not able to connect on EC2 instance

0

We have deployed our reactJS frontend, NodeJS backend and NodeJS chat(which uses socket programming for communication) on AWS using nginx as webserver. This is what I have added my /etc/nginx/nginx.conf file, I have changed the server name to <value> for confidentiality:

    server {
        listen 80;
        listen [::]:80;
        server_name <value>.in www.<value>.in;

        # Redirect all HTTP requests to HTTPS
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name <value>.in www.<value>.in;

        root /var/www/html/dist;
        index index.html;

        ssl_certificate /etc/letsencrypt/live/<value>.in/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/<value>.in/privkey.pem;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /api {
            proxy_pass http://localhost:5000;
            proxy_set_header Host $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;
        }

        location /socket {
            proxy_pass http://localhost:3001;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";
        }
       }

Our frontend, backend and chat is deployed on t2.medium EC2 instance. It is working fine on my local but the chat socket is failing to establish a connection from the frontend client to socket server. I have done all the configuration and I am really clueless what can be done next. Here is what I have done apart from the config:

  1. I have allowed all traffic in my security group
  2. My port is open, since I am able to telnet via the port. What could I be doing wrong here? Am I missing something?
gefragt vor einem Monat99 Aufrufe
1 Antwort
0

Here are a few things you can try to troubleshoot the issue with the chat socket failing to connect from the frontend to the socket server:

  • Double check that the socket server is running and listening on the correct port (3001 based on your config). Try telnetting directly to the server on that port to verify.

  • Make sure the nodejs chat server is binding to 0.0.0.0 or localhost, not 127.0.0.1. It needs to listen on all interfaces.

  • Check the browser console for any errors or connection failures. This can provide clues on where the issue is occurring.

  • Try simplifying the proxy config for the /socket location. Remove extra headers like X-Real-IP etc and see if the basic proxying works.

  • Verify that websockets are enabled in the nginx config using proxy_http_version 1.1; and proxy_set_header Upgrade $http_upgrade;

  • Check that the EC2 security group allows incoming connections on port 3001 from the frontend subnet/IP range.

  • Look for any CORS or protocol mismatches between frontend and backend. They should use the same protocol (HTTP vs HTTPS).

  • See if connecting directly to the socket server URL from the browser works rather than going through the proxy. This can determine if Nginx is the issue.

  • Check Nginx error logs for any clues on connection failures or proxy issues.

AWS
beantwortet vor einem Monat
profile picture
EXPERTE
überprüft vor einem Monat
  • Hey Adeleke, Thanks for the response, we have tried everything that you suggested here but it's still not working. Let me know if I can share something with you which can make it easier for you to help us

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen