1

I have the following nginx configuration to run node.js with websockets behind an nginx reverse proxy:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  gzip on;

  upstream nodejsserver {
    server 127.0.0.1:3456;
  }

  server {
    listen 443 ssl;
    server_name myserver.com;

    error_log /var/log/nginx/myserver.com-error.log;
    ssl_certificate /etc/ssl/myserver.com.crt;
    ssl_certificate_key /etc/ssl/myserver.com.key;

    location / {
      proxy_pass https://nodejsserver;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_read_timeout 36000s;
    }
  }
}

The node.js server uses the same certificates as specified in the nginx configuration.

My issue is that in my browser (Firefox, though this issue occurs in other browsers too), my websocket connection resets every few minutes with a 1006 code. I have researched the reason for this error in this particular (or similar) constellation, and most of the answers here as well as on other resources point to the proxy_read_timeout nginx configuration variable not being set or being set too low. But this is not the case in my configuration.

Worthy of note is also that when I run node.js and access it directly, I do not experience these disconnects, both locally and on the server.

In addition, I've tried running nginx and node.js insecurely (port 80), and accessing ws:// instead of wss:// in my client. The issue remains the same.

1 Answers1

0

There are a few things you need to do to keep a connection alive.

You should stablish a keepalive connection count per worker proccess, and the documentation states you need to be explicit about your protocol as well. Other than that, you maybe running into other kinds of timeouts, so edit your upstream and server blocks:

upstream nodejsserver {
  server 127.0.0.1:3456;
  keepalive 32;
}

server {
  #Stuff...
  location / {
    #Stuff...
    # Your time can be different for each timeout, you just need to tune into your application's needs
    proxy_read_timeout 36000s; 
    proxy_connect_timeout 36000s;
    proxy_send_timeout 36000s;
    send_timeout 36000s; # This is stupid, try a smaller number
  }
}

There are a number of other discussions in SO about the same subject, check this answer out

ErvalhouS
  • 4,178
  • 1
  • 22
  • 38