0
0
NginxHow-ToBeginner · 3 min read

How to Use tcp_nopush in Nginx for Better Network Performance

In Nginx, you enable tcp_nopush by setting it to on inside the http or server block. This option helps Nginx send headers and data in fewer packets, improving network performance by reducing overhead.
📐

Syntax

The tcp_nopush directive controls whether Nginx uses the TCP_CORK option on Linux to optimize packet sending. It accepts two values:

  • on: Enables TCP_CORK to delay sending packets until enough data is ready.
  • off: Disables TCP_CORK, sending packets immediately.

This directive is placed inside the http, server, or location blocks.

nginx
tcp_nopush on | off;
💻

Example

This example shows how to enable tcp_nopush in the http block of your Nginx configuration to improve performance when sending large files.

nginx
http {
    tcp_nopush on;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
            index index.html;
        }
    }
}
⚠️

Common Pitfalls

Common mistakes when using tcp_nopush include:

  • Enabling tcp_nopush without sendfile enabled, which reduces its effectiveness.
  • Using tcp_nopush on non-Linux systems where it has no effect.
  • Not combining tcp_nopush with tcp_nodelay off for optimal packet coalescing.

Always ensure sendfile on; is set to benefit from tcp_nopush.

nginx
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
            index index.html;
        }
    }
}
📊

Quick Reference

DirectiveDescriptionTypical Value
tcp_nopushEnables TCP_CORK to optimize packet sendingon
sendfileEnables zero-copy file transferon
tcp_nodelayControls Nagle's algorithm; usually off with tcp_nopushoff

Key Takeaways

Enable tcp_nopush in the http or server block to optimize packet sending.
Always use tcp_nopush with sendfile on for best performance.
Set tcp_nodelay off when tcp_nopush is on to allow packet coalescing.
tcp_nopush works only on Linux systems using TCP_CORK.
Misconfiguration can cause delays or no performance gain.