node.jsnginxproxy

Websockets nginx reverse proxy + Cloudflare


I'm using NGINX to proxy a node.js websockets server with my subdomain which is using CloudFlare service.

But when I'm connecting to the websocket server, I have this error

WebSocket connection to 'ws://xxxx.xxxx.tld/' failed: Error during WebSocket handshake: Unexpected response code: 200

PS: I don't have this error when I'm accessing directly by IP adress:port without NGINX reverse proxy.

Here's my NGINX config file:

server {
    listen 80;
    server_name xxxx.xxxx.tld;

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:89;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

For information, I'm using the latest version of NGINX.

What's wrong? Thanks


Solution

  • Edit: As Nickoplier noted, CloudFlare now supports WebSockets for the free plan too. Be advised that the WebSocket support on the free plan is meant only for low-volume websites (per the support article).

    This does not work because currently CloudFlare only supports WebSockets (in a beta stage) for their business and enterprise customers. See the support article for more information.

    One of the ways to workaround this is to create a new subdomain that is not CloudFlare-enabled. For example ws.xxxx.xxxx.tld, and point your websockets client against that.