Two of Three Encodings Served the Old Page
On 17 November I fixed some text in one post on this site, rebuilt its compressed copies and deployed just those files. The files on the server were correct. nginx still sent the old version to some requests: a client that asked for Brotli got the new page, and clients that asked for gzip or for no compression got the old one.
The most likely cause is a cache inside nginx, open_file_cache, which my config turns on for speed. It keeps file descriptors open between requests. A deploy that replaces files with tar leaves those descriptors pointing at files that no longer have a name, and nginx keeps reading them until it decides to check the path again.
I reproduced the mechanism locally and the output is below. The access log, which I only checked afterwards, shows that the stale responses ended when I reloaded nginx, about 42 seconds after the deploy, so everything I saw fits inside the cache’s 60-second window.
The Deploy and the Symptom
The site is static HTML served by nginx on an Ubuntu EC2 instance. The setup is in Deploying a Static Site to EC2 with Nginx. Every public text file has two pre-compressed siblings next to it, page.html.br and page.html.gz. nginx picks one with brotli_static and gzip_static, based on the request’s Accept-Encoding header, so it never compresses anything on the fly.
The change was small. I edited posts/web-security-testing.html, regenerated its .br and .gz files, and put those three files in a tarball together with sitemap.xml and its two siblings. On the server I extracted the tarball over the webroot:
# deploy.tgz holds the post, its .br and .gz, and sitemap.xml with its .br and .gz
tar -xzf deploy.tgz -C /var/www/codertronix.comThen I checked the post from outside with curl, once per encoding. With Accept-Encoding: br I got the new page. With identity (no compression) and with gzip I got the old one. The sitemap was mixed up in a different way: the uncompressed sitemap was old, and the Brotli and gzip versions were new.
I checked again about 25 seconds later. The identity and gzip responses for the post were still old.
A split like that rules out the usual suspects. curl keeps no cache, and there is no CDN in front of this site; the domain resolves straight to the server. A broken upload does not explain it either, because the new Brotli file and the old-looking gzip file came out of the same tarball.
Checking the Disk Against the Response
The first question was whether the server really held the new files. I looked at all three copies of the post on disk, and all three contained the new text. So the problem sat somewhere between the disk and the response.
If you want to make the same comparison, decompress each sibling and search for a string you know you changed. The brotli command comes from Ubuntu’s brotli package, which my server does not have installed, so install it or run that line on your own machine:
cd /var/www/codertronix.com/posts
grep -c 'text you changed' web-security-testing.html
zcat web-security-testing.html.gz | grep -c 'text you changed'
brotli -dc web-security-testing.html.br | grep -c 'text you changed'Each file’s inode number is also worth a look, because it matters later. Here is what the three files look like on the server today:
cd /var/www/codertronix.com
stat -c '%i %s bytes %n' posts/web-security-testing.html posts/web-security-testing.html.gz posts/web-security-testing.html.br
They are three separate inodes, which means three separate files as far as the kernel is concerned, at 58,105, 16,578 and 13,377 bytes.
From outside, the response headers tell you which file nginx used. Content-Length is the size of the file it sent, and Last-Modified is that file’s modification time:
url=https://codertronix.com/posts/web-security-testing.html
for enc in identity gzip br; do
printf '%-8s ' "$enc"
curl -s -o /dev/null -D - -H "Accept-Encoding: $enc" "$url" \
| tr -d '\r' | grep -i -E '^(content-encoding|content-length|last-modified)' | paste -sd' '
done
Today the responses carry two different modification times: 17:47:59 for the plain file and 17:48:06 for both compressed ones, because the siblings were regenerated a few seconds after the HTML. That is normal, and it shows that each encoding is served from its own file with its own metadata. nginx takes the size and the date from the same cache entry that holds the file descriptor, so a stale response also arrives with the old file’s size and date. Comparing them against the files on disk is a quick way to spot it.
What open_file_cache Keeps
My nginx.conf has this block in the http context:
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;To see what the running config contains, ask nginx rather than reading files, because nginx -T prints the full config including every included file. It only reads, so it is safe on a live server:
sudo nginx -T 2>/dev/null | grep open_file_cache
The nginx docs describe open_file_cache as a cache that can store open file descriptors with their sizes and modification times, whether directories exist, and file lookup errors such as “file not found” (the errors only when open_file_cache_errors is on). Without it, nginx calls open() and fstat() for every static file it serves, and close() afterwards. With it, a busy file stays open and nginx skips those calls. The four settings:
max=10000: at most 10,000 entries. When the cache is full, the least recently used entries are removed.inactive=60s: an entry nobody has requested for 60 seconds is removed.open_file_cache_min_uses 2: the docs call this the minimum number of accesses during theinactiveperiod that a file descriptor needs to stay open in the cache. In the source, each entry counts its lookups. When a request finishes and the count is 2 or more, the descriptor stays open. Below 2, nginx closes the descriptor and keeps only the entry.open_file_cache_valid 60s: the docs say only “Sets a time after which open_file_cache elements should be validated.” This is the setting that matters for deploys. For 60 seconds after an entry was last checked, nginx uses the cached descriptor, size and modification time without looking at the disk at all.
The docs don’t say what “validated” means, so I read src/core/ngx_open_file_cache.c in the nginx 1.28.0 source (the server runs 1.28.3). Validation is a stat of the path and a comparison of its inode number with the one stored in the entry. If the inode is the same, nginx keeps the descriptor and refreshes the size and modification time. If it differs, nginx closes the old descriptor and opens the path again.
Two more details from the source matter here. First, the cache is keyed by the full file path, so page.html, page.html.gz and page.html.br are three independent entries. gzip_static looks up the .gz path, and the Brotli static module looks up the .br path, both through the same cache with the same settings. Second, the cache lives in each worker process’s memory, and workers don’t share it. My server runs two workers, so one file can have two entries in two different states.
I keep the cache anyway. On a static site almost every request is a file read, and keeping hot files open saves several system calls per request. I haven’t measured what that saves on my server, so I won’t put a number on it. It is a reasonable default for a static site, as long as the deploy knows the cache exists.
tar Replaces Files Instead of Editing Them
On Linux, a file name is a directory entry that points to an inode, and the inode holds the data. “Overwriting a file” can mean two different things. You can write new bytes into the same inode, or you can create a new inode and point the name at it.
GNU tar does the second by default. When it extracts over an existing file, it removes the old file and creates a new one. I checked this with the same tar version the server has, GNU tar 1.35, on my Windows machine, by comparing the file ID before and after extraction:
#!/usr/bin/env bash
# tar-inode.sh: does extracting over an existing file keep its inode?
set -u
echo new > src/page.html && tar -czf deploy.tgz -C src page.html
for mode in "" "--overwrite"; do
echo old > www/page.html
before=$(stat -c %i www/page.html)
tar $mode -xzf deploy.tgz -C www
printf '%-22s %s -> %s (%s)\n' "tar ${mode:-(default)} -xzf" "$before" "$(stat -c %i www/page.html)" "$(cat www/page.html)"
done
Plain tar -xzf produced a new file ID. --overwrite kept the same one, which means it wrote into the existing file.
rsync behaves the same way by default. Its man page says that without --inplace it creates a new copy of the file and moves it into place when the copy is complete, and that --inplace writes the updated data directly to the destination file instead. Used the usual way, tar and rsync both give the path a new inode.
Now put that together with the cache. Before the deploy, a worker has page.html open: its descriptor points at inode A. tar removes the name page.html and creates inode B with the new content under the same name. The unlink(2) man page explains what happens to A: if the name was the last link to a file but processes still have it open, the file stays in existence until the last descriptor referring to it is closed. nginx’s descriptor still works, and it still reads the old bytes. Until the entry’s 60 seconds are up, nginx has no reason to look at the path, so it keeps serving inode A.

This is also why my disk checks all looked fine. cat, zcat and grep open the path, and the path leads to the new inode. Only nginx was reading through an old descriptor.
Why Brotli Was Fresh and gzip Was Not
Once you know that each path has its own entry, the mixed results stop looking random. Whether an entry served stale content after the deploy depends on two things, and both are tracked per path and per worker:
- Whether the entry held an open descriptor. With
min_uses 2, a path requested only once has its descriptor closed when the request ends. The next request opens the path again and gets the new inode. Only paths requested at least twice keep a descriptor that can go stale. - How much of its 60 seconds was left. An entry is trusted for 60 seconds after its last check, counted from that check and not from the deploy. An entry checked 55 seconds before the deploy can be stale for about 5 seconds. One checked just before can be stale for close to a minute.
For the post, the .br entry was fresh on my first check and the other two were not. For the sitemap, the plain entry was stale and both compressed ones were fresh. My best explanation is that the stale paths were the ones whose entries held a descriptor and still had time left on their 60 seconds, and the fresh paths either had no descriptor or were already due for a check. The access log has each request’s time and body size, which shows which version it got, but not which worker answered or what state each cache entry was in, so I can’t prove which entry was in which state. The explanation fits the facts, but I haven't proven it.

The two workers add one more variable. Each curl request lands on one worker or the other. A path can be stale in one worker and fresh in the other, so repeating the same check can give different answers. That is a reason not to trust a single curl.
What the Access Log Showed About Timing
The source code puts a limit on how long this can last. Each entry is checked again no more than 60 seconds after its previous check, and that check sees the new inode and reopens the file. Every entry was checked at some point before the deploy, so all of them should have been serving the new files within 60 seconds of tar finishing. That is what happened in my local reproduction below, where the stale entry turned fresh at the 60-second mark.
When I first wrote this, I thought the stale responses had lasted longer than that, because I remembered the second round of checks as a minute or two after the first. The access log says otherwise. The deploy landed at about 17:43:26 UTC. The first checks at 17:43:27 got the old page for identity (59,006 bytes) and gzip (16,848 bytes) and the new one for Brotli (13,394 bytes). The second round at 17:43:52 got the same mix. I reloaded nginx at 17:44:08, and the requests after that got the new page in all three encodings (58,106 bytes for identity and 16,580 for gzip). The stale responses lasted at most 42 seconds, inside the 60-second window, so nothing I saw needs a second explanation.
A reload did fix all three encodings at once, which fits the cache explanation. According to nginx’s control docs, a reload starts new worker processes and asks the old ones to shut down gracefully. The new workers start with empty caches, so their first request for each path opens whatever is on disk.
Reproducing It Locally
I wanted to see this happen on a machine I control. The plan was to build nginx from source in WSL, but WSL stopped responding during the build, so I used the official nginx 1.28.0 Windows build from nginx.org. It uses the same open_file_cache code as nginx on Linux. On Windows, the “inode” it compares is the NTFS file ID, and it opens files with share-delete access, so tar can replace a file that nginx has open, just as on Linux. The Windows build has no Brotli module, so I tested the plain and gzip paths.
Setup
The config uses the same four open_file_cache lines as the server, plus gzip_static, on 127.0.0.1:18080. I started it from Git Bash with nginx.exe -p pointing at a scratch folder, and ran every script below from that folder:
worker_processes 1;
error_log logs/error.log notice;
pid logs/nginx.pid;
events { worker_connections 64; }
http {
include mime.types;
access_log off;
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
gzip_static on;
server {
listen 127.0.0.1:18080;
root html;
}
}Replacing the files with tar
This script requests the plain file twice, so its descriptor stays open, and the gzip file once, so its descriptor is closed again. Then it extracts a tarball holding new versions of both files and polls every five seconds until the plain response changes:
#!/usr/bin/env bash
# repro-tar.sh: replace cached files the way tar does, then watch what nginx serves
set -u
url=http://127.0.0.1:18080/page.html
get() { curl -s -H "Accept-Encoding: $1" "$url" | { [ "$1" = gzip ] && gzip -dc || cat; }; }
echo '<p>OLD version</p>' > html/page.html && gzip -kf html/page.html
get identity >/dev/null; get identity >/dev/null # 2 uses: descriptor stays cached
get gzip >/dev/null # 1 use: descriptor closed again
tar -xzf deploy.tgz -C html # new page.html + page.html.gz
start=$(date +%s)
while :; do
t=$(( $(date +%s) - start ))
a=$(get identity); b=$(get gzip)
printf '+%02ds identity: %s gzip: %s\n' "$t" "$a" "$b"
[ "$a" = '<p>NEW version</p>' ] && break
sleep 5
done
The gzip path, used once, served the new file immediately. The plain path, used twice, served the old file for 60 seconds and then switched. An earlier run that polled every two seconds switched at +60s as well.

open_file_cache_valid ran out. A reload ended it at once.The same deploy with a reload
This time both paths are requested twice, so both go stale after tar, and the script reloads nginx straight away. $NGX holds the nginx.exe -p ... command for the scratch folder; on a server it would be sudo nginx:
#!/usr/bin/env bash
# repro-reload.sh: same replacement, but reload nginx straight after it
set -u
url=http://127.0.0.1:18080/page.html
get() { curl -s -H "Accept-Encoding: $1" "$url" | { [ "$1" = gzip ] && gzip -dc || cat; }; }
echo '<p>OLD version</p>' > html/page.html && gzip -kf html/page.html
for i in 1 2; do get identity >/dev/null; get gzip >/dev/null; done # both cached
tar -xzf deploy.tgz -C html
echo "after tar: identity: $(get identity) gzip: $(get gzip)"
$NGX -t 2>&1 | tail -1 && $NGX -s reload && sleep 1
echo "after reload: identity: $(get identity) gzip: $(get gzip)"
Both encodings were stale after tar, and both were fresh one second after the reload.
Why editing in place is worse
If the new inode is the problem, why not write into the existing file with tar --overwrite or rsync --inplace? I tested what nginx does then. This script caches a file and then rewrites it in place with a longer version. The shell’s > truncates the file and writes into the same inode:
#!/usr/bin/env bash
# repro-inplace.sh: rewrite the cached file in place (same inode), make it longer
set -u
url=http://127.0.0.1:18080/page.html
echo '<p>OLD version</p>' > html/page.html
curl -s -o /dev/null "$url"; curl -s -o /dev/null "$url" # cache it
echo "file id before: $(stat -c %i html/page.html)"
echo '<p>NEW version, edited in place</p>' > html/page.html # > truncates and rewrites
echo "file id after: $(stat -c %i html/page.html)"
echo "size on disk: $(wc -c < html/page.html) bytes"
curl -s -D headers.txt -o body.txt "$url"
echo "served: $(grep -i '^content-length' headers.txt | tr -d '\r')"
echo "body: $(cat body.txt)"
The file ID didn’t change, and the file on disk was 36 bytes. nginx sent Content-Length: 19 and the first 19 bytes of the new content. The descriptor reads the new bytes because it is the same inode, but the size still comes from the cache. A page that grows gets cut off, so you get a broken page instead of an old one. I didn’t test a file that shrinks. There is also the window while the file is being written, when a reader can see half of it, and rsync’s man page warns against using --inplace on files that others are reading. I would rather serve an old page for a minute than a cut-off one, so I don’t write in place.
A new post can 404 for a minute
open_file_cache_errors on also caches “file not found”. That matters when you publish a new URL rather than change an old one:
#!/usr/bin/env bash
# repro-404.sh: open_file_cache_errors on also caches "file not found"
set -u
url=http://127.0.0.1:18080/new-post.html
rm -f html/new-post.html
curl -s -o /dev/null -w 'before deploy: %{http_code}\n' "$url"
curl -s -o /dev/null -w 'before deploy: %{http_code}\n' "$url"
echo '<p>brand new post</p>' > html/new-post.html
start=$(date +%s)
while :; do
code=$(curl -s -o /dev/null -w '%{http_code}' "$url")
printf '+%02ds %s\n' $(( $(date +%s) - start )) "$code"
[ "$code" = 200 ] && break
sleep 10
done
After two 404s, the new file returned 404 for another 60 seconds even though it existed on disk. I also tried it with a single request before the file existed, and that one request was enough: the 404 lasted another 60 seconds. In the source, a cached error is served until its 60 seconds are up, whatever the use count. So if anything requests a URL before it exists, including you checking whether the deploy has landed yet, the new page can 404 for up to a minute after it goes live.
Fixes and Their Trade-offs
Reload after every deploy
This is what I do now. The deploy ends with:
sudo nginx -t && sudo systemctl reload nginxA reload replaces every worker, so every cache entry in every worker is gone, and nothing is dropped: old workers finish the requests they are already serving before they exit. The costs are small. The deploy needs sudo on the server. The config has to be valid, which is what nginx -t checks first; the control docs also say that if applying a new config fails, the master rolls back and keeps working with the old one. The first requests after a reload open their files again, because the caches start empty.
A shorter open_file_cache_valid
open_file_cache_valid 5s;This shrinks the worst case from a minute to about five seconds, at the cost of more stat calls on busy files. It doesn’t remove the window. A check run right after the deploy can still get the old page, and so can a real visitor. It is a sensible change if you can’t reload as part of the deploy.
Turn the cache off for HTML
location ~* \.html$ {
open_file_cache off;
# ...existing headers...
}open_file_cache is allowed in location context, so you can switch it off just for the files that change on deploys and keep it for everything else. The cost is an open, a stat and a close for every HTML request, which I haven’t measured. On this site it would not be enough on its own: sitemap.xml and feed.xml change on deploys too, and they have their own location blocks, so they would need the same line. So would location /, because extensionless URLs like /posts/nginx-configuration are served from there, not from the .html block.
Cache-busting file names don’t help pages
The CSS and JS on this site already have content hashes in their names, like style.<hash>.css. A changed stylesheet is a new path, so it gets a new cache entry, and nothing requests the old name after the HTML stops pointing at it, so hashed CSS and JS can’t go stale this way. Pages can’t do that. A post’s URL, sitemap.xml and feed.xml have to stay the same, because they are the addresses people and crawlers use. Every HTML deploy replaces a file at a path nginx may already have open.

A Post-Deploy Check for Every Encoding
Checking status codes is not enough here, because a stale response is a perfectly good 200. The check has to compare the encodings with each other. This script fetches each path once per encoding, lets curl decode the body (--compressed), and hashes the result. If all three hashes match, the encodings agree:
#!/usr/bin/env bash
# check-encodings.sh: fetch each URL once per encoding and compare the decoded bodies.
set -uo pipefail
base=${BASE:-https://codertronix.com}
status=0
for path in "$@"; do
sums=()
for enc in identity gzip br; do
hdrs=$(curl -sS --compressed -H "Accept-Encoding: $enc" -D - -o body.tmp "$base$path")
ce=$(printf '%s' "$hdrs" | tr -d '\r' | awk -F': ' 'tolower($1)=="content-encoding"{print $2}')
lm=$(printf '%s' "$hdrs" | tr -d '\r' | awk -F': ' 'tolower($1)=="last-modified"{print $2}')
sum=$(sha256sum < body.tmp | cut -c1-12)
sums+=("$sum")
printf '%-34s %-8s %-5s %s %s\n' "$path" "$enc" "${ce:--}" "$sum" "$lm"
done
if [ "${sums[0]}" = "${sums[1]}" ] && [ "${sums[1]}" = "${sums[2]}" ]; then
echo "OK $path: all encodings decode to the same bytes"
else
echo "STALE $path: encodings differ, reload nginx"; status=1
fi
done
rm -f body.tmp
exit $status
Run it with the paths the deploy touched. It exits non-zero when the encodings disagree, so a deploy script can stop on it. curl needs Brotli support to decode the br response; curl -V lists brotli under Features if it has it.
One limit: the script proves the three encodings agree, not that they are new. If every entry is stale, it passes. After a reload that can’t happen, but if you skip the reload, compare the identity hash with sha256sum of your local copy of the file, or grep the response for a string from your change. It is also worth running twice, since each run may land on a different worker.
Checklist
- Know whether the cache is on:
sudo nginx -T | grep open_file_cache. - tar and rsync replace files with new inodes. A cached descriptor keeps serving the old file for up to
open_file_cache_valid. - Each encoding is a separate file and a separate cache entry.
min_usesand timing decide which ones go stale, so check all of them. - Don’t switch to in-place writes to fix it. The cached size goes wrong and pages get cut off.
- End every deploy with
sudo nginx -t && sudo systemctl reload nginx. - Compare the decoded bodies of every encoding from outside, after the reload.
- For new URLs, don’t request them before they exist, or expect a cached 404 for up to a minute.
For the rest of the nginx setup on this site, see the nginx configuration guide and the EC2 deploy write-up. The post whose edit started all this is the web security testing guide.



