大网数据广告活动
服务器运维问答

What is the consequence of modifying the server file during downloading on Nginx?

· 52CE 我爱测

It seems that, on Nginx server, modifying the content of a file being downloaded could result in a broken and inconsistent file at the client end. Here's my experiment: I built a Nginx server in a local network, and on that, ther…

问题原文

原帖链接 · Connor Li · CC BY-SA 3.0

It seems that, on Nginx server, modifying the content of a file being downloaded could result in a broken and inconsistent file at the client end. Here's my experiment:

I built a Nginx server in a local network, and on that, there is a 1M file filling with 'a'. I download this file on my computer using curl (and the speed is limited slow enough). During the period of downloading, I overwrite this file on the server with another file with the same size but filled with 'b'. I did this step using Linux command 'cp' and no error occurs. Finally, when I check the downloaded result, it turns out to be a mixed file with 'a's at the first part and 'b's at the second part, like this way: "aaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"

I'm wondering if it is the expected consequence. And if so, is there a way to tell the Nginx to reject any IO operations when there is still active connections, or simply return an error code and abort the connection after the file has changed? Since updating files may be a very common operation on the server, I imagine there should be some workarounds.

My testing environment:

Server OS: Linux 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 
2014 x86_64 x86_64 x86_64 GNU/Linux

Server file system: ext4

Nginx version: nginx/1.4.6 (Ubuntu)

Client OS: Windows 10

Client tool: curl 7.51.0 (x86_64-w64-mingw32) libcurl/7.51.0 OpenSSL/1.0.2j 
zlib/1.2.8 libssh2/1.7.0 nghttp2/1.15.0 librtmp/2.3

已采纳解答

原帖链接 · HBruijn · CC BY-SA 3.0

Reading a file that is being overwritten will have unpredictable results.

You will get much more predictable results by writing your modifications to a temporary location (on the same file system) and rename (mv) the temporary file to replace the current version. Renaming files is atomic and instantaneous.

If your web server was (still) reading and transmitting the old version when you rename a file, it will continue to do so from that old version until that download is complete but any new requests for that file will get the new version.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。