0
0
Nginxdevops~5 mins

sendfile and tcp_nopush in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a web server sends files to users, it needs to do this quickly and efficiently. The sendfile and tcp_nopush settings in nginx help the server send files faster by reducing extra steps and combining data before sending it over the network.
When your nginx server hosts large static files like images or videos and you want to speed up delivery.
When you notice high CPU usage on your server while sending files and want to reduce it.
When you want to improve network efficiency by sending fewer, larger packets instead of many small ones.
When serving files over slow or busy networks and want to reduce delays caused by packet fragmentation.
When you want to optimize nginx performance for better user experience on your website.
Config File - nginx.conf
nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    sendfile        on;
    tcp_nopush      on;

    server {
        listen       8080;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

sendfile on; tells nginx to use the operating system's sendfile feature to send files directly from disk to the network, skipping copying data between user space and kernel space.

tcp_nopush on; tells nginx to wait and send the HTTP headers and file data together in one packet, reducing the number of packets sent and improving network efficiency.

The server block sets up a simple web server listening on port 8080 serving files from /usr/share/nginx/html.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Request the headers of the index.html file to verify the server is responding correctly.
Terminal
curl -I http://localhost:8080/index.html
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx/1.24.0 Date: Thu, 01 Jun 2024 12:00:00 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Wed, 31 May 2024 10:00:00 GMT Connection: keep-alive ETag: "609c-5a1b3f4a2c400" Accept-Ranges: bytes
Key Concept

If you remember nothing else from this pattern, remember: enabling sendfile and tcp_nopush lets nginx send files faster by reducing data copying and combining packets.

Common Mistakes
Setting sendfile on without enabling tcp_nopush.
This can cause nginx to send many small packets, reducing network efficiency and speed.
Always enable tcp_nopush along with sendfile to combine headers and file data into fewer packets.
Forgetting to test nginx configuration with 'nginx -t' before reloading.
Reloading with a bad config can cause nginx to fail or stop serving requests.
Always run 'sudo nginx -t' to check syntax before reloading or restarting nginx.
Using sendfile on with files on network file systems that do not support it.
sendfile may not work correctly and cause errors or slowdowns.
Use sendfile only with local file systems or test carefully if using network file systems.
Summary
Enable sendfile in nginx to send files directly from disk to network, improving speed.
Enable tcp_nopush to combine HTTP headers and file data into fewer packets for better network efficiency.
Always test nginx configuration with 'nginx -t' before reloading to avoid errors.