You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
nginx serving nix store paths sets last-modified header to unix+1 timestamp #25485
Thanks for finding this. I've been having issues with broken caching on my nginx/nixos server. Even when the files on the server were updated (files in the nix store), the browser was told that no change had happened.
If I'm understanding this correctly, since nginx's etags are based on filesystem modification time; you can't have proper cache invalidation (ie. it will always send 304 not modified). Or is there a workaround?
+1 this is a problem. And turning off etag seems like overkill. Is there some way to 1) set the date on a file so that Nginx can pick it up, or 2) do something else awesome?
Is the zero-timestamp rule set in stone in Nix, or is there some way to override it if you really want a file in /nix/store to have a particular timestamp?
It's not perfect, as it will change on each Nix evaluation instead of being content addressed. Another way would be to turn the store path passed to nginx root directive to translate into an etag.
In my testing, this sends the correct etag, but does not send the 304 not
modified, when the etag matches.
Also, the header gets stripped out when compression is enabled.
It's working perfectly for me. I've gzip compression enabled, the etag header is sent and the browser gets a 304 when querying the If-None-Match header.
I've to correct what I said. @yorickvP your statement seems to be correct. What happens in my case is that Cloudflare is actually handling the caching of the ETag correctly, not nginx.
So this approach is completely useless without external caching. I'll investigate further and see if I can come up with a solution.
This is really great! @yegortimoshenko, @domenkozar, or anyone, do you have a moment to write an explanation of this new feature and its practical ramifications for anyone configuring an nginx server? Since it isn't typical nginx behavior, I think the nixos manual should explain it somewhere so it doesn't catch people by surprise.
Activity
chris-martin commentedon Jan 14, 2018
Why do you need to turn etag off?
chris-martin commentedon Jan 14, 2018
Ah, I see. nginx's etags aren't hashes. https://serverfault.com/questions/690341/algorithm-behind-nginx-etag-generation/690374#690374
chris-martin commentedon Jan 14, 2018
I wonder if we could find a way to pull the hash out of the nix store path and put it into an etag header.
samdroid-apps commentedon Feb 1, 2018
Thanks for finding this. I've been having issues with broken caching on my nginx/nixos server. Even when the files on the server were updated (files in the nix store), the browser was told that no change had happened.
If I'm understanding this correctly, since nginx's etags are based on filesystem modification time; you can't have proper cache invalidation (ie. it will always send 304 not modified). Or is there a workaround?
greglearns commentedon Apr 2, 2018
+1 this is a problem. And turning off etag seems like overkill. Is there some way to 1) set the date on a file so that Nginx can pick it up, or 2) do something else awesome?
chris-martin commentedon Apr 2, 2018
Is the zero-timestamp rule set in stone in Nix, or is there some way to override it if you really want a file in /nix/store to have a particular timestamp?
domenkozar commentedon May 27, 2018
I think we could get away with:
It's not perfect, as it will change on each Nix evaluation instead of being content addressed. Another way would be to turn the store path passed to nginx
rootdirective to translate into an etag.yorickvP commentedon May 27, 2018
The etag function is at ngx_http_core_module.c#L1582
called from ngx_http_gzip_static_module and ngx_http_static_module.
Making it dependent on the realpath of the file seems doable.
Akii commentedon Jul 29, 2018
Explanation how to configure nginx to use the hash from the nix path: https://nixos.wiki/wiki/Nginx#Correct_Caching_when_Serving_Static_Files_from_.2Fnix.2Fstore
yorickvP commentedon Jul 29, 2018
Akii commentedon Jul 29, 2018
It's working perfectly for me. I've gzip compression enabled, the etag header is sent and the browser gets a 304 when querying the
If-None-Matchheader.domenkozar commentedon Jul 29, 2018
Nice! An example with a directory would be more common:
Akii commentedon Aug 2, 2018
I've to correct what I said. @yorickvP your statement seems to be correct. What happens in my case is that Cloudflare is actually handling the caching of the ETag correctly, not nginx.
So this approach is completely useless without external caching. I'll investigate further and see if I can come up with a solution.
lukateras commentedon Sep 28, 2018
Here is a patch: https://gitlab.com/yegortimoshenko/patches/blob/cc67b35dc7cf68a18ba98043b54ed3d10374c486/nginx/nix-etag-1.15.4.patch
If real path (after dereferencing symlinks) of
rootis in Nix store, it will use path's hash as ETag. For example:I'll take some time to double check everything and only then send a PR. However, feel free to test:
nginx: apply ETag patch (see NixOS/nixpkgs#25485)
nginx: apply ETag patch (see NixOS/nixpkgs#25485)
nginx: if root is in Nix store, use path's hash as ETag
chris-martin commentedon Apr 30, 2019
This is really great! @yegortimoshenko, @domenkozar, or anyone, do you have a moment to write an explanation of this new feature and its practical ramifications for anyone configuring an nginx server? Since it isn't typical nginx behavior, I think the nixos manual should explain it somewhere so it doesn't catch people by surprise.
domenkozar commentedon Apr 30, 2019
I think https://twitter.com/chris__martin/status/1123050969271554048 is a good explanation, just needs to be put into the manual.
chris-martin commentedon May 1, 2019
Cool, submitted #60578.
doc: Explain the nginx ETag patch