TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Nginx optimization: Understanding sendfile, tcp_nodelay and tcp_nopush

143 点作者 dirtyaura超过 10 年前

9 条评论

Animats超过 10 年前
<i>To avoid network congestion, the TCP stack implements a mechanism that waits for the data up to 0.2 seconds so it won’t send a packet that would be too small. This mechanism is ensured by Nagle’s algorithm, and 200ms is the value of the UNIX implementation.</i><p>Sigh. If you&#x27;re doing bulk file transfers, you never hit that problem. If you&#x27;re sending enough data to fill up outgoing buffers, there&#x27;s no delay. If you send all the data and close the TCP connection, there&#x27;s no delay after the last packet. If you do send, reply, send, reply, there&#x27;s no delay. If you do bulk sends, there&#x27;s no delay. If you do send, send, reply, there&#x27;s a delay.<p>The real problem is ACK delays. The 200ms &quot;ACK delay&quot; timer is a bad idea that someone at Berkeley stuck into BSD around 1985 because they didn&#x27;t really understand the problem. A delayed ACK is a bet that there will be a reply from the application level within 200ms. TCP continues to use delayed ACKs even if it&#x27;s losing that bet every time.<p>If I&#x27;d still been working on networking at the time, that never would have happened. But I was off doing stuff for a startup called Autodesk.<p>John Nagle
评论 #9049090 未加载
评论 #9049121 未加载
评论 #9050046 未加载
dugmartin超过 10 年前
One note about sendfile - if you are using VirtualBox and serving files out of a shared folder with nginx (or I assume Apache) you&#x27;ll need to disable it or you won&#x27;t see any updates you do to files once the first version is sent.
评论 #9047385 未加载
评论 #9048145 未加载
onn超过 10 年前
I always cringe a bit inside, when reading articles like this.<p>nodelay and cork are different, indeed, but opposites? They both try to achieve the same effect, put more data in before sending a packet.<p>&gt; [...] This mechanism is ensured by Nagle’s algorithm, and 200ms [...]<p>Absolutely not. Nagle&#x27;s algorithm does not have any delay or timer build in. It simply holds back non-full packets, when there is data in flight (not acked). The second half of the problem is delayed acks, but this is not mentioned in the article, instead it goes on saying<p>&gt; [...] but Nagle is not relevant to the modern Internet [...]<p>which is indeed popular belief, but a very superficial analysis that holds no water if you study it further.<p>The feeling I always get from articles like this is they border on &quot;technical religion&quot;. It sounds correct, it is technical, it isn&#x27;t even false, but it doesn&#x27;t paint a clear picture, instead it mystifies things further.<p>The problems nginx had:<p>1. nagle and http keepalive don&#x27;t play nice together, the last bit of data might be artificially delayed, especially when delayed acks come into play. nodelay seems needed here. (It is not though, see that Minshall bit.)<p>2. how to send headers and use sendfile for the body, and fill the first packet with more then just the headers? nopush (tcp_cork) is a solution.
justincormack超过 10 年前
The &quot;modern&quot; way to do things is to use the splice syscall and related calls instead of sendfile, which can deal with copying between sockets and appending headers more directly.<p>Igor said in a talk I went to that Nginx 1.x was written for FreeBSD first, while 2.0 will be written for Linux first so perhaps some of these things may change (hence &quot;nopush&quot; in the config file, the freebsd term).
评论 #9049628 未加载
评论 #9047549 未加载
pjwal超过 10 年前
Does anyone else use and&#x2F;or have thoughts on some of the boilerplate nginx configuration projects?<p><a href="https://github.com/Umkus/nginx-boilerplate" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Umkus&#x2F;nginx-boilerplate</a><p><a href="https://github.com/h5bp/server-configs-nginx" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;h5bp&#x2F;server-configs-nginx</a>
shirro超过 10 年前
Is there any point to optimisations like sendfile anymore when increasingly sites are being served over TLS?
nodesocket超过 10 年前
Our nginx configurations use:<p><pre><code> sendfile on; tcp_nopush on; tcp_nodelay off; </code></pre> With tcp_nodelay off. Is the author suggesting we turn it on?
评论 #9047498 未加载
评论 #9047503 未加载
frankzinger超过 10 年前
sendfile is also not necessarily zero-copy: <a href="https://svnweb.freebsd.org/base?view=revision&amp;revision=255608" rel="nofollow">https:&#x2F;&#x2F;svnweb.freebsd.org&#x2F;base?view=revision&amp;revision=25560...</a> <a href="https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/commit/?id=485ddb4b9741bafb70b22e5c1f9b4f37dc3e85bd" rel="nofollow">https:&#x2F;&#x2F;git.kernel.org&#x2F;cgit&#x2F;linux&#x2F;kernel&#x2F;git&#x2F;stable&#x2F;linux-st...</a> (sendfile calls splice; the splice manpage also states that it might copy instead of move pages.)<p>That said, it equates to a single kernel-to-kernel copy instead of a kernel-to-userspace plus a userspace-to-kernel copy as in the read&#x2F;write case.
raptium超过 10 年前
Nagle Angle and Naggle ?