0
0
Nginxdevops~3 mins

Why sendfile and tcp_nopush in Nginx? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your server could send files faster by skipping extra steps?

The Scenario

Imagine you run a busy website and every time a user requests a file, your server reads the file piece by piece and sends it over the network manually.

This means your server is juggling many small tasks, reading and sending tiny chunks repeatedly.

The Problem

This manual way is slow because the server spends extra time switching between reading files and sending data.

It also causes more network traffic with many small packets, making the website slower and less efficient.

The Solution

Using sendfile lets the server send files directly from disk to the network without extra copying.

Adding tcp_nopush groups small packets into bigger ones before sending, reducing network chatter.

Together, they make file delivery faster and smoother.

Before vs After
Before
read file chunk
send chunk
repeat
After
sendfile on;
tcp_nopush on;
What It Enables

This lets your server deliver files quickly and efficiently, improving user experience and saving resources.

Real Life Example

A video streaming site uses sendfile and tcp_nopush to smoothly send large video files without delays or buffering.

Key Takeaways

Manual file sending is slow and chatty.

sendfile sends files directly from disk to network.

tcp_nopush bundles packets for efficient delivery.