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

NGINX Calculate reproduceable Etag for static files

· 52CE 我爱测

I was considering moving my static files to multiple origin servers, however I noticed NGINX computes different Etags for identically deployed files on the different servers, which will then mess with downstream caching layers th…

问题原文

原帖链接 · Fire Lancer · CC BY-SA 3.0

I was considering moving my static files to multiple origin servers, however I noticed NGINX computes different Etags for identically deployed files on the different servers, which will then mess with downstream caching layers thinking files keep changing which have not.

What I wanted to happen was for the Etag to be dependent on the served file itself, e.g. using a MD5 or other hash of the content (and naturally for NGINX to locally cache that for performance).

Is this possible with the provided static file serving, or is it expected that I solve this in another way? e.g. write my own "file server" app/script that does compute and cache the hash, or ensure all the filesystem meta data (whatever it uses) is always identical somehow?

Using Apache or similar instead is an option.

社区高分解答

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

as stated in Algorithm behind nginx etag generation, nginx uses the last modified time and the content length of files to generate static file etags.

So if you get different etags for the same static files on different backend servers, chances are that your files' timestamps do not match exactly (e.g. because you checked them out with git at different times).

I don't see any nginx setting to configure how nginx computes the etag, so your only chance with nginx is to ensure that file timestamps are exactly the same, e.g. by using rsync or manually setting timestamps after checking the files out (for a script that sets timestamps to the last timestamp a file has been committed, see https://stackoverflow.com/questions/1964470/whats-the-equivalent-of-use-commit-times-for-git/13284229#13284229)

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

资料来源与适用范围

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

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

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