6

My setup is cloudflare -> nginx -> node.js server. I have confirmed that it is not the node.js server. The problem seems to be the combination of cloudflare and nginx. It works just fine with only nginx and node.js.

This is what nginx logs for the websocket connection: 162.158.75.125 - - [29/Oct/2016:00:01:47 +0000] "GET / HTTP/1.1" 101 1047 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

nginx config:

server {
    listen 443 ssl;
    server_name vectorwar.io;
    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;

    location / {
        proxy_pass       http://localhost:7000;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 31104000s;
        proxy_connect_timeout 31104000s;
        proxy_send_timeout 31104000s;

    }
}
  • [Can I use webSockets with CloudFare](https://support.cloudflare.com/hc/en-us/articles/200169466-Can-I-use-CloudFlare-with-WebSockets-)? – jfriend00 Oct 29 '16 at 19:23
  • That doesn't solve my problem. –  Oct 29 '16 at 22:40
  • Maybe a timeout somewhere in the pipe? How long is it staying open for when the websocket is silent? Does sending a ping/pong style message down the WS connection periodically prevent it disconnecting? – Brian Nov 07 '16 at 02:40

2 Answers2

2

Have you tried setting up the Websocket without SSL?

I've had experiences where Websockets made over SSL automatically disconnect after ~45 seconds. Using an insecure connection fixed that. Not ideal obviously, but you could try to see if that at least solves the disconnection issues.

Robba
  • 7,684
  • 12
  • 48
  • 76
  • It seems I can just send a packet every few seconds and that stops it from timing out. –  Nov 17 '16 at 00:57
0
proxy_pass       http://localhost:7000;

Try adding an s, to http. Then reload nginx and try again,

service nginx reload

Also Websockets and Cloudflare don't play well together: Websocket disconnects with 1006 error, without a reason

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168