How to enable keepalive connections in nginx?

Published on Aug. 22, 2023, 12:19 p.m.

To enable keepalive connections in Nginx, you can use the keepalive directive in your configuration file. Here is an example configuration that enables keepalive connections for an upstream server:

http {
  upstream myapp {
    server backend1.example.com;
    server backend2.example.com;
    keepalive 64;
  }

  server {
    listen 80;
    location / {
      proxy_pass http://myapp;
    }
  }
}

In this configuration, the keepalive directive is used to define the maximum number of idle keepalive connections that Nginx is allowed to keep open to each backend server (64 in this example). The proxy_pass directive is used to pass requests to the upstream server.

Note that the specific configuration details may vary depending on your application and server setup. Also, enabling keepalive connections can help to optimize server performance by reducing the number of new TCP connections that need to be established for each request.

Tags: