HanouaJ
HanouaJ

Reputation: 381

How to enable ETag headers on Nginx for static files?

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?

Upvotes: 38

Views: 100641

Answers (2)

Martin Konecny
Martin Konecny

Reputation: 59571

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.

Upvotes: 31

Tan Hong Tat
Tan Hong Tat

Reputation: 6864

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

Upvotes: 43

Related Questions