Sendfile tcp_nopush tcp_nodelay nginx parameter interpretation

Label NginxTcp_nodelayTcp_nopushTcp_corkNagle
6626 people read Comment(2) Collection Report


Now the popular web server which provides the sendfile option to improve server performance, and that in the end sendfileWhat is it, how does it affect performance?SendfileIn fact Linux2.0+After the launch of a system call,WebThe server can decide whether to use the configuration to adjust its ownSendfileThis system call. Take a look at the first. SendfileTraditional network transmission process:

Read (file, tmp_buf, len);

Write (socket, tmp_buf, len);

Hard drive buffer user buffer>> kernel socket buffer kernelProtocol stack

Generally speaking, a network application is through reading hard disk data, and then write data to the socket to complete the network transmission. Above 2 lines with the code to explain this, but the above 2 lines of simple code to cover up the bottom of a lot of operations. Look at the bottom layer is how to perform the above 2 lines of code:

1, system call read () to generate a context switch: from mode user to mode kernel, and then DMA to perform copy, the file data from the hard disk read to a buffer kernel.

2, data from the buffer kernel to buffer user, and then the system call read () to return, then generate a context switch: from mode kernel to mode user.

Three System callWrite ()Produce a context switch: fromMode userSwitch to Mode kernelThen take the stepsTwoReadBuffer userCopy of the data to Buffer kernel(dataTwoCopy toBuffer kernel), but this time is different. Buffer kernelThisBufferAndSocketCorrelation.

4, the system call write () to return to produce a context switch: from mode kernel to mode user (fourth switch), and then DMA from the buffer kernel copy data to the protocol stack (fourth copies).

The above 4 steps have 4 context switches, with 4 copies, we find that if we can reduce the number of times and copy number will be effective to improve performance. In the kernel2.0+ version, the system calls sendfile () to simplify the performance of the above steps. Sendfile () not only can reduce the number of the number of times and can also reduce the number of copies.

And then look at the process of using sendfile () to carry out the network transmission:

Sendfile (socket, file, len);

Hard drive buffer kernel (Fast copy toBuffer kernelsocket)Protocol stack

One System callSendfile ()ThroughDMACopy hard disk data to Buffer kernel, then data isKernelDirectly copy to anotherSocketRelated Buffer kernel. There is noMode userAndMode kernelSwitch between, in KernelDirectly completed in aBufferTo anotherBufferCopy.

2, DMA to the data from the kernelbuffer directly copied to the protocol stack, no switch, it does not need data from the mode user copy to mode kernel, because the data is in kernel.

The steps are reduced, the switch is reduced, the copy is reduced, and the natural performance is improved. This is what said in the Nginx configuration file to open the on sendfile option to improve the performance of the server web.





Tcp_nopush on Syntax: off

Off Default:

Http Context:



Tcp_nopush Reference:


Directive permits or forbids the use of thesocket options TCP_NOPUSH on FreeBSD or option on TCP_CORK This Linux. is onlyavailable when using

This option causes nginx to attempt to sendit HTTP s response headers in one packet on Linux and FreeBSD 4.x Setting

Can read more about the TCP_NOPUSH and TCP_CORKsocket options here. You


Linux IsTcp_cork,The meaning is to say,When usedSendfileFunction,Tcp_nopushTo play a role, it and instructionTcp_nodelayIs mutually exclusive.Tcp_corkIsLinuxUnder theTcp/ipTransmission of a standard, this standard is probably the meaning is, in general, inTCPThe interactive process, when the application receives the data packet thillerUpload and send out, don't wait, andTcp_corkOption is the data packets will not be sent immediately, until the maximum data packets, a one-time transfer out, which helps to solve the network congestion, has been the default.

That is to say, tcp_nopush = on will set the call to the tcp_cork method, which is the default, the result isPackets will not be sent immediately, until the maximum data packets, a one-time transfer out, which helps to solve the network congestion.

Express delivery for example (the following is my understanding, perhaps is not correct), when the express delivery, the courier received a package, immediately delivered, so that the immediate, but will consume a lot of manpowerForce, the performance of the network will cause network congestion, and when the courier received a parcel, the parcel to the distribution center, and so a certain number of unified delivery, so thatTcp_corkThe option of doing things, like thisWords, will maximize the use of network resources, although there is a little delay.

For nginx configuration file in the tcp_nopush, the default is tcp_nopush, do not need to specify, this option for WWW, FTP and other large files are very helpful



TCP_NODELAYAndTCP_CORKBasically controlled the package "NagleChemical",NagleThe implication here is the use ofNagleThe algorithm assembles smaller packages into larger frames. Nagle JohnIsNagleThe inventor of the algorithm, the latter is named after his name, he is in1984In the first time in this way to try to solve the problem of network congestion Ford Motor Company (for details please refer toSeeIETF RFC 896). He solved the problem is calledWindow syndrome silly, the Chinese called "stupid window syndrome", the specific meaning is, because the general terminal application each generation of a hit key operation will send a packet, and the typical case of a packet will have a byte of data loadFourtyBaotou, a byte long, then produce4000%The overload, which is easy to make the network congestion,. NagleLater becomes a standard and can be realized on the Internet. It has now become the default configuration, but in our view, there are some occasions to turn off this option is also in need of.

       Now let us assume that an application has sent a request,Want to send small data. We can choose to send the data immediately or wait to generate more data and then send the two strategies again. If we send it right awaySend data, so interactive and customer/Server based applications will greatly benefit.If the request is sent immediately, the response time will be faster. Above operationBy setting the socketTCP_NODELAY = on option to complete, so you can disableNagle Algorithm.

       In the other case, we need to wait until the amount of data to reach the maximum, only through the network to send all the data, the data transmission mode is useful for a large number of data communication performance, the typical application is the file server. ShouldUse NagleIn this case, the algorithm will produce problems. However, if you are sending a large amount of data, you can set theTCP_CORKOption disabledNagleAnd the way is just the same. TCP_NODELAYOn the contraryTCP_CORKAndTCP_NODELAYIs mutually exclusive).

Guess what you're looking for.
View comments
* above the user's comments only represent their personal views, do not represent the views or position of the CSDN website
    Personal data
    • Visit88232 times
    • Integral:1238
    • Grade
    • Rank:19651st
    • Original38
    • Reprint:31
    • Translation:0
    • Comment:9
    Article classification
    Latest comments