Bird
Raised Fist0
Nginxdevops~5 mins

sendfile and tcp_nopush in Nginx - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the sendfile directive in nginx?
easy
A. To compress files before sending them
B. To speed up file sending by avoiding extra copying between kernel and user space
C. To encrypt files during transfer
D. To limit the number of simultaneous connections

Solution

  1. Step 1: Understand what sendfile does

    The sendfile directive allows nginx to send files directly from disk to the network socket without copying data to user space, which speeds up file sending.
  2. Step 2: Compare options with this behavior

    Options B, C, and D describe unrelated features like compression, encryption, or connection limits, which are not the purpose of sendfile.
  3. Final Answer:

    To speed up file sending by avoiding extra copying between kernel and user space -> Option B
  4. Quick Check:

    sendfile = avoid extra copying [OK]
Hint: Remember: sendfile skips copying to user space [OK]
Common Mistakes:
  • Thinking sendfile compresses data
  • Confusing sendfile with encryption
  • Assuming sendfile limits connections
2. Which of the following is the correct syntax to enable tcp_nopush in nginx configuration?
easy
A. tcp_nopush enable;
B. tcp_nopush = on;
C. tcp_nopush true;
D. tcp_nopush on;

Solution

  1. Step 1: Recall nginx directive syntax

    In nginx, directives are set with the format directive value;. The correct value for enabling tcp_nopush is on.
  2. Step 2: Check each option's syntax

    tcp_nopush on; uses tcp_nopush on; which is correct. Options A, C, and D use invalid keywords or syntax not supported by nginx.
  3. Final Answer:

    tcp_nopush on; -> Option D
  4. Quick Check:

    Enable directives with on; in nginx [OK]
Hint: Use 'on;' to enable nginx directives like tcp_nopush [OK]
Common Mistakes:
  • Using 'enable' instead of 'on'
  • Adding '=' sign in directive
  • Using 'true' instead of 'on'
3. Given this nginx snippet:
sendfile on;
tcp_nopush on;

What is the expected effect on static file delivery?
medium
A. Files are sent faster by sending larger TCP packets
B. Files are sent slower due to extra copying
C. Files are compressed before sending
D. Files are sent with encryption

Solution

  1. Step 1: Understand combined effect of sendfile and tcp_nopush

    sendfile on; avoids extra copying, and tcp_nopush on; allows grouping data into bigger TCP packets, improving speed.
  2. Step 2: Analyze options

    Files are sent faster by sending larger TCP packets correctly states files are sent faster by sending larger TCP packets. Options B, C, and D describe incorrect effects.
  3. Final Answer:

    Files are sent faster by sending larger TCP packets -> Option A
  4. Quick Check:

    sendfile + tcp_nopush = faster large packets [OK]
Hint: sendfile + tcp_nopush = faster static file delivery [OK]
Common Mistakes:
  • Thinking tcp_nopush compresses data
  • Assuming sendfile slows down transfer
  • Confusing tcp_nopush with encryption
4. You enabled sendfile on; but forgot to enable tcp_nopush. What issue might you observe?
medium
A. nginx configuration syntax error
B. Files fail to send completely
C. Small TCP packets causing inefficient network usage
D. Files get corrupted during transfer

Solution

  1. Step 1: Understand the role of tcp_nopush with sendfile

    tcp_nopush helps group data into larger TCP packets. Without it, sendfile sends smaller packets inefficiently.
  2. Step 2: Analyze consequences of missing tcp_nopush

    Small TCP packets causing inefficient network usage correctly identifies small TCP packets causing inefficient network usage. Options A, B, and D describe errors or corruption which do not happen just by missing tcp_nopush.
  3. Final Answer:

    Small TCP packets causing inefficient network usage -> Option C
  4. Quick Check:

    Missing tcp_nopush = small packets [OK]
Hint: Without tcp_nopush, packets stay small and inefficient [OK]
Common Mistakes:
  • Expecting syntax errors without tcp_nopush
  • Thinking files get corrupted
  • Assuming files fail to send
5. You want to optimize nginx for serving large static files efficiently. Which configuration snippet correctly uses sendfile and tcp_nopush together?
hard
A. sendfile on; tcp_nopush on;
B. sendfile on; tcp_nopush off;
C. sendfile off; tcp_nopush off;
D. sendfile off; tcp_nopush on;

Solution

  1. Step 1: Identify best practice for static file optimization

    To optimize static file delivery, sendfile should be enabled to avoid extra copying, and tcp_nopush enabled to send larger TCP packets.
  2. Step 2: Evaluate each configuration snippet

    sendfile on; tcp_nopush on; enables both sendfile and tcp_nopush, which is the correct combination. Options A, B, and C disable one or both, reducing efficiency.
  3. Final Answer:

    sendfile on; tcp_nopush on; -> Option A
  4. Quick Check:

    Enable both for best static file speed [OK]
Hint: Enable both sendfile and tcp_nopush for best speed [OK]
Common Mistakes:
  • Disabling sendfile but enabling tcp_nopush
  • Enabling sendfile but disabling tcp_nopush
  • Disabling both directives
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard