Tom
Tom

Reputation: 2645

Cross-domain WebSocket connection failed during WebSocket handshake: Unexpected response code: 400

I am trying to use socket.io in a Node js application. I have the application sitting on a subdomain and the front-end running on the www version of the domain.

Running the front-end and Node js service on the same domain is not an option.

Sending data back and forth from the client to the server seems to be working. I have sent data both ways and it has worked fine.

However, In the console of the browser I get the following error.

WebSocket connection to 'wss://subdomain.domain.com/socket.io/?EIO=3&transport=websocket&sid=6bNHWyXcCdlMI0HHAAAB' failed: Error during WebSocket handshake: Unexpected response code: 400

My Nginx configuration looks like this:

# HTTP - redirect all requests to HTTPS:
server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        return 301 https://$host$request_uri;
}

# HTTPS - proxy requests on to local Node.js app:
server {
        listen 443;
        server_name subdomain.domain.com;

        ssl on;
        # Use certificate and key provided by Let's Encrypt:
        ssl_certificate /etc/letsencrypt/live/subdomain.domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.com/privkey.pem;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers '*******';

        # Pass requests for / to localhost:3000:
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:3000/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Connection "upgrade";

       }
}

Both the client and the Node js are using https.

Does anyone know what is causing this issue and how it could be fixed?

Thank you

Upvotes: 0

Views: 2230

Answers (1)

Thunderbird
Thunderbird

Reputation: 233

It looks like you forgot about Upgrade header. It's required if you want to use Nginx as a reverse proxy for WebSockets.

As said here, just try to add one more header: proxy_set_header Upgrade $http_upgrade;

Upvotes: 4

Related Questions