Nginx sendfile Explained: Configuration, Performance, and Best Practices

Published: 2026-07-14

Introduction

The sendfile directive enables Nginx to use the operating system's sendfile() system call when serving static files.

Without sendfile, file data is copied multiple times between the disk, kernel, and user space before being sent to the client. With sendfile enabled, the operating system transfers file data directly from the kernel to the network socket, reducing CPU usage and improving throughput.

For most production environments, enabling sendfile provides better performance when serving static content such as HTML, CSS, JavaScript, images, videos, and downloadable files.

This guide explains how sendfile works, how to configure it, recommended settings, and common issues.

Test Environment

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

What Is sendfile?

The sendfile directive tells Nginx whether to use the operating system's sendfile() system call when transmitting static files.

Example:

http {
    sendfile on;
}

When enabled, Nginx avoids unnecessary data copying between user space and kernel space, making file delivery more efficient.

How It Works

Without sendfile:

Disk
 │
 ▼
Kernel
 │
 ▼
Nginx Process
 │
 ▼
Kernel
 │
 ▼
Network

The file is copied multiple times before reaching the client.

With sendfile enabled:

Disk
 │
 ▼
Kernel
 │
 ▼
Network

The operating system transfers the file directly to the network socket, reducing memory copies and CPU overhead.

Why Use sendfile?

Enabling sendfile provides several benefits:

Lower CPU usage.

Faster static file delivery.

Reduced memory copying.

Better performance under high traffic.

Improved scalability for websites serving many static assets.

These advantages are most noticeable for:

Images

CSS files

JavaScript files

Fonts

PDF documents

Software downloads

Video files

Check the Current Configuration

The directive is usually configured in:

/etc/nginx/nginx.conf

Search for it:

grep sendfile /etc/nginx/nginx.conf

Example:

sendfile on;

Change sendfile

Edit the configuration file.

sudo nano /etc/nginx/nginx.conf

Enable it:

http {
    sendfile on;
}

Or disable it:

http {
    sendfile off;
}

Save the file.

Test the Configuration

Always verify the configuration before reloading.

sudo nginx -t

Expected output:

nginx: the configuration file syntax is ok

nginx: configuration file test is successful

Reload Nginx

Apply the changes.

sudo systemctl reload nginx

Verify:

sudo systemctl status nginx

Expected output:

Active: active (running)

When Does sendfile Help?

sendfile primarily improves the delivery of static files.

Examples include:

index.html

style.css

app.js

logo.png

banner.webp

manual.pdf

video.mp4

Dynamic PHP pages still require PHP-FPM to execute the application logic before Nginx sends the generated response.

Relationship with PHP-FPM

A typical request flow looks like this:

Browser
    │
    ▼
Nginx
    │
    ├── Static File
    │         │
    │         ▼
    │    sendfile()
    │
    └── PHP Request
              │
              ▼
         PHP-FPM
              │
              ▼
        Execute PHP
              │
              ▼
        Return Response

Only static files benefit directly from sendfile.

Recommended Configuration

For most Linux servers:

http {
    sendfile on;
}

Many administrators also combine it with:

tcp_nopush on;
tcp_nodelay on;

These directives help optimize packet transmission, particularly when serving larger files.

Common Issues

Static Files Do Not Update

In some environments, especially when using network file systems (such as NFS) or shared folders in virtual machines, file changes may not appear immediately after updating.

Possible causes include:

Network-mounted storage.

File synchronization delays.

Virtual machine shared folders.

If this occurs, temporarily disabling sendfile can help determine whether it is contributing to the issue.

No Improvement for PHP Performance

A common misconception is that enabling sendfile speeds up PHP execution.

It does not.

PHP scripts are processed by PHP-FPM first. sendfile only affects how Nginx transmits static files after they have been read.

Disabling sendfile Unnecessarily

Some users disable sendfile while troubleshooting unrelated problems.

On most Linux production servers, keeping it enabled provides better performance for static content.

Only disable it if you have identified a compatibility issue with your storage or deployment environment.

Best Practices

Enable sendfile on production servers.

Use it together with tcp_nopush where appropriate.

Test configuration changes with nginx -t.

Reload Nginx after modifying the configuration.

Disable it only when troubleshooting verified file synchronization issues.

Conclusion

The sendfile directive enables Nginx to use the operating system's efficient sendfile() system call for serving static files. By reducing unnecessary memory copying between user space and kernel space, it lowers CPU usage and improves file transfer performance.

For nearly all Linux production servers, enabling sendfile is considered a best practice. When combined with properly configured worker processes, connection limits, and HTTP compression, it helps build a fast, scalable, and resource-efficient Nginx deployment.

Explore More

Technology Guides →

Nginx Explained: Essential Configuration Directives and Best Practices

Nginx worker_processes Explained: Configuration, Performance, and Best Practices

Nginx worker_connections Explained: Configuration, Limits, and Best Practices

Nginx keepalive_timeout Explained: Configuration, Performance, and Best Practices

Southeast Asia Insights →

Best Areas to Live in Bangkok in 2026: A Complete Guide for Expats and Digital Nomads

Best Coworking Spaces in Bangkok 2026: A Guide for Digital Nomads and Remote Workers

Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained

Best eSIM for Southeast Asia in 2026: Compare the Top Travel eSIMs