Question

NGINX Optimization for Server-Sent Events (SSE)

I have developed a group chat app using Node.js and have load tested with Gatling. When I test from either my laptop or directly on my droplet, it can handle 382 connections. However, when I set Gatling in the droplet to bypass NGINX and connect directly to Node.js, it can handle more than 8,000 connections.

Any ideas why NGINX maxes out at precisely 382 connections?

Here is my sites-available:

server {

  server_name chat.quadspace.tv;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/letsencrypt/live/chat.quadspace.tv/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/chat.quadspace.tv/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

  location / {

    # https://serverfault.com/questions/801628/for-server-sent-events-sse-what-nginx-proxy-configuration-is-appropriate

    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    
  }

}

server {

  if ($host = chat.quadspace.tv) {
    return 301 https://$host$request_uri;
  } # managed by Certbot

  server_name chat.quadspace.tv;
  listen 80;
  return 404; # managed by Certbot

}

And the headers sent from Node.js:

res.writeHead(200, {
  'Content-Type': 'text/event-stream',
  'Connection': 'keep-alive',
  'Cache-Control': 'no-cache',
  'X-Accel-Buffering': 'no'
});

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

I got it solved! Just needed to tweak my nginx.conf, increasing both worker_connections and worker_rlimit_nofile. Now my group chat app is handling more than 7,000 connections!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel