Bird
Raised Fist0
Nginxdevops~10 mins

sendfile and tcp_nopush in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - sendfile and tcp_nopush
Client requests file
↓
Nginx checks sendfile setting
Yes↓
Use sendfile syscall to send file
↓
Check tcp_nopush setting
Yes↓
Hold TCP packets to send header + file data together
↓
Send combined packets to client
↓
Request complete
No↓
Send file data normally without sendfile
↓
Request complete
This flow shows how nginx uses sendfile to send files efficiently and tcp_nopush to combine packets for better network performance.
Execution Sample
Nginx
sendfile on;
tcp_nopush on;

location /file {
  root /var/www/html;
}
Nginx configuration enabling sendfile and tcp_nopush to optimize file delivery.
Process Table
StepActionsendfile Enabled?tcp_nopush Enabled?Network BehaviorResult
1Client requests fileYesYesPrepare to send file using sendfileReady to send file efficiently
2Nginx uses sendfile syscallYesYesFile data sent directly from disk to network bufferReduced CPU usage
3tcp_nopush holds packetsYesYesTCP packets held to combine header and file dataFewer packets sent
4Send combined packetsYesYesHeader and file data sent in one packetImproved network efficiency
5Request completeYesYesFile fully sentClient receives file quickly
6If sendfile offNoYesFile data copied to user space then sentMore CPU usage, slower
7If tcp_nopush offYesNoPackets sent separatelyMore packets, less efficient
💡 Request completes after file is sent using sendfile and tcp_nopush optimizations
Status Tracker
VariableInitialAfter Step 2After Step 3After Step 4Final
sendfileoffonononon
tcp_nopushoffoffononon
Network Packetsmultiple smallmultiple smallheldcombinedcombined
CPU Usagehighlowlowlowlow
Key Moments - 3 Insights
Why does tcp_nopush hold packets before sending?
tcp_nopush holds packets to combine the HTTP header and file data into fewer TCP packets, reducing network overhead as shown in execution_table step 3.
What happens if sendfile is off but tcp_nopush is on?
If sendfile is off, nginx copies file data to user space before sending, increasing CPU usage and reducing efficiency, even if tcp_nopush tries to combine packets (execution_table step 6).
Does tcp_nopush work without sendfile enabled?
tcp_nopush is most effective with sendfile because it holds packets at the TCP layer; without sendfile, packet combining is less efficient (execution_table steps 6 and 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does tcp_nopush hold TCP packets?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Check the 'Network Behavior' column in execution_table row for Step 3.
According to variable_tracker, what happens to CPU usage after enabling sendfile?
AIt increases
BIt stays the same
CIt decreases
DIt becomes unpredictable
💡 Hint
Look at the 'CPU Usage' row from Initial to After Step 2 in variable_tracker.
If tcp_nopush is off but sendfile is on, how does network packet sending change?
APackets are combined
BPackets are sent separately
CPackets are delayed indefinitely
DPackets are dropped
💡 Hint
See execution_table step 7 under 'Network Behavior' and 'Result'.
Concept Snapshot
sendfile on;  # Enables kernel-level file sending
 tcp_nopush on; # Holds TCP packets to combine header and file data
Use together for efficient file delivery
sendfile reduces CPU by avoiding user-space copy
 tcp_nopush reduces network packets for better throughput
Full Transcript
This visual execution trace shows how nginx uses the sendfile and tcp_nopush settings to optimize file delivery. When a client requests a file, nginx checks if sendfile is enabled. If yes, it uses the kernel's sendfile syscall to send file data directly from disk to the network buffer, reducing CPU usage. Then, if tcp_nopush is enabled, nginx holds TCP packets to combine the HTTP header and file data into fewer packets, improving network efficiency. The execution table details each step, showing network behavior and results. The variable tracker shows how sendfile and tcp_nopush settings change, along with CPU usage and packet behavior. Key moments clarify common confusions about how these settings interact. The quiz tests understanding of when packets are held, CPU usage changes, and packet sending behavior when tcp_nopush is off. The snapshot summarizes the syntax and benefits of these settings for quick reference.

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