Code change:
'"request":"$request",' '"http_referer":"$http_referer",' '"upstream_addr":"$upstream_addr"}'; <ins> ''; services.nginx.virtualHosts."0.my.test" = { extraConfig = '' access_log syslog:server=unix:/dev/log,facility=user,tag=mytag,severity=info ceeformat; location /favicon.ico { allow all; access_log off; log_not_found off; } </ins> ''; <del> services.nginx.virtualHosts."0.my.test" = { extraConfig = '' access_log syslog:server=unix:/dev/log,facility=user,tag=mytag,severity=info ceeformat; location /favicon.ico { allow all; access_log off; log_not_found off; } ''; }; </del> }; <ins> services.nginx.virtualHosts.localhost = { root = pkgs.runCommand "testdir" {} '' mkdir "$out" echo hello world > "$out/index.html" ''; }; }; in { webserver = commonConfig; newwebserver = { pkgs, lib, ... }: { imports = [ commonConfig ]; services.nginx.virtualHosts.localhost = { root = lib.mkForce (pkgs.runCommand "testdir2" {} '' mkdir "$out" echo hello world > "$out/index.html" ''); }; }; </ins> }; <del> testScript = '' startAll; </del> <ins> testScript = { nodes, ... }: let newServerSystem = nodes.newwebserver.config.system.build.toplevel; switch = "${newServerSystem}/bin/switch-to-configuration test"; in '' my $url = 'http://localhost/index.html'; sub checkEtag { my $etag = $webserver->succeed( 'curl -v '.$url.' 2>&1 | sed -n -e "s/^< [Ee][Tt][Aa][Gg]: *//p"' ); $etag =~ s/r?n$//; my $httpCode = $webserver->succeed( 'curl -w "%{http_code}" -X HEAD -H 'If-None-Match: '.$etag.'' '.$url ); chomp $httpCode; die "HTTP code is not 304" unless $httpCode == 304; return $etag; } </ins> $webserver->waitForUnit("nginx"); $webserver->waitForOpenPort("80"); <ins> subtest "check ETag if serving Nix store paths", sub { my $oldEtag = checkEtag; $webserver->succeed('${switch}'); my $newEtag = checkEtag; die "Old ETag $oldEtag is the same as $newEtag" if $oldEtag eq $newEtag; }; </ins> ''; })

Related discussion:
text: If you serve files from , nginx will set last-modified header to unix timestamp 1. The result is that browser will cache indefinitely. Not sure what we should do about it really, following nginx configuration helps:
Why do you need to turn etag off?
Ah, I see. nginx's etags aren't hashes.
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.
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?
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 directive to translate into an etag.
The etag function is at called from and . Making it dependent on the realpath of the file seems doable.
Explanation how to configure nginx to use the hash from the nix path:
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 header.
Nice! An example with a directory would be more common:
I've to correct what I said. 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.