36

I'm using Nginx as a webserver and want to implement a browser caching method so that the users keep copies of static, unchanged files locally and download only changed files. One of the propositions was to use the file's timestamp to figure out the changed files and refresh them only, but this is not possible in my case since after every new deploy, a new version of the whole web application is created, and all the files' timestamps change.

I researched a little about the ETag header, which seemed like a pretty good solution, but I found out that Etags are not officially supported by Nginx yet.

Is there any way of implementing the Etags on Nginx or alternative solutions?

Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69
HanouaJ
  • 361
  • 1
  • 3
  • 4
  • 1
    If you disable `gzip` using `gzip off;` line you can see `etag` header on your response. – efkan May 26 '15 at 10:28
  • 3
    Keep in mind that nginx uses the file's last modification date and the file size to generate the etag. – Christoph Wimberger Oct 14 '16 at 08:10
  • 3
    If you see a complaint from a security scanner (Nikto etc) about etags leaking inodes, it's probably a false positive if you're running nginx because [nginx does not include inodes in etags](http://serverfault.com/questions/690341/algorithm-behind-nginx-etag-generation). It can be disabled in apache, though apache does leak inodes by default (see the `FileETag` directive). – Synchro Apr 05 '17 at 15:32
  • Related: https://stackoverflow.com/questions/55305687/how-to-address-weak-etags-conversion-by-nginx-on-gzip-compression – Jesse Nickles Sep 05 '22 at 15:58

2 Answers2

41

Upgrade your Nginx.

Syntax:     etag on | off;
Default:    etag on;

Context:    http, server, location

This directive appeared in version 1.3.3.

Enables or disables automatic generation of the “ETag” response header field for static resources.

Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#etag

Tan Hong Tat
  • 6,671
  • 2
  • 27
  • 25
  • 11
    Thank you, I have tried it and the Etag header are added to the HTTP header, but still the cache validation relies on the timestamps of the files even that I removed Last Modified header. I want the files to be checked if they really have a change in the content and not just by verifying the timestamps of the files – HanouaJ Jul 04 '14 at 13:52
  • Upgrade to what? – Altimus Prime Mar 01 '21 at 02:14
  • 1
    @AltimusPrime This question was from several years ago before ETags were included in Nginx by default (which they are now after v1.3.3). – Jesse Nickles Sep 05 '22 at 15:24
30

All recent versions of Nginx (newer than 1.3.3) will automatically set these.

For example:

location /img {
    root /path/to/public;
}

and the response headers Etag + Last-Modified headers will be returned.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • 4
    This is the answer I was looking for. It would nice if your answer also included a link to the Nginx docs / release notes regarding this functionality. Thanks. – Mark Edington Apr 02 '18 at 17:50
  • Do you know which file types Nginx will auto-generate ETags for? If so, please consider answering my question here, thanks! https://serverfault.com/questions/1110002/which-static-file-types-does-nginx-generate-etags-for-by-default – Jesse Nickles Sep 05 '22 at 15:21