#16062·chatwoot

perf(deployment): upstream keepalive in nginx_chatwoot.conf is invalidated by Connection \ mapping

Author: md-riazCreated Sep 29, 2026Updated Sep 29, 2026

Describe the bug / Performance Issue

In deployment/nginx_chatwoot.conf, the upstream backend block defines keepalive 32; to maintain persistent TCP connections between Nginx and Puma (127.0.0.1:3000):

nginx
upstream backend {
  zone upstreams 64K;
  server 127.0.0.1:3000;
  keepalive 32;
}

However, because WebSocket upgrading and general HTTP traffic are unified under a single location / block using proxy_set_header Connection $connection_upgrade;, Nginx explicitly forwards Connection: close to Puma for every non-WebSocket request, completely invalidating the keepalive 32 connection pool.

Root Cause Analysis

In deployment/nginx_chatwoot.conf:

nginx
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
  ...
  location / {
    proxy_pass http://backend;
    ...
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    ...
  }
}
  1. For standard HTTP/REST requests (e.g. /api/v1/profile, /api/v1/conversations, dashboard assets), the browser does not send an Upgrade header.
  2. Therefore, $http_upgrade evaluates to '' (empty string).
  3. According to the map, $connection_upgrade is set to "close".
  4. Nginx sends the upstream request to Puma with the header Connection: close.
  5. Per RFC 7230 / HTTP specifications and Puma server behavior, receiving Connection: close forces Puma to tear down the TCP socket immediately upon returning the response.
  6. As specified in the official Nginx upstream keepalive documentation:

    "For HTTP, the proxy_http_version 1.1; directive should be specified, and the 'Connection' header field should be cleared (proxy_set_header Connection "";). Additionally, 'close' connection header field should not be passed to the proxied server."

Because Connection: close is transmitted for all standard HTTP traffic, the keepalive 32 pool remains perpetually unused (dead code) for regular requests.

Impact on Self-Hosted Instances

On page refresh or dashboard mount, Chatwoot's frontend dispatches 30–40 parallel API requests (validate_token, profile, conversations, agents, labels, custom_attributes, etc.).

Because keepalive is suppressed:

  • Nginx and Puma must perform 30–40 individual TCP 3-way handshakes (SYN, SYN-ACK, ACK) and socket closures in quick succession.
  • Local loopback sockets pile up in TIME_WAIT status.
  • Puma worker listener threads incur unnecessary connection accept overhead on high-frequency bursts.

Proposed Solution

Separate ActionCable WebSockets (/cable) from standard HTTP traffic in deployment/nginx_chatwoot.conf:

nginx
  # ActionCable WebSockets
  location /cable {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    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;
    proxy_read_timeout 36000s;
  }

  # Standard HTTP / REST APIs / Dashboard assets
  location / {
    proxy_pass http://backend;
    proxy_redirect off;

    proxy_pass_header Authorization;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_set_header Connection ""; # Clears header to enable upstream keepalive pool

    client_max_body_size 0;
    proxy_read_timeout 36000s;
  }

This preserves WebSocket functionality on /cable while allowing all standard API and asset requests to benefit from persistent keepalive sockets to Puma.