Skip to main content
page last edited on 28 February 2016

How to set expiration date for static files being served directly by Nginx

Version: 5.4 and early

In case all static files are served directly by Nginx on your site, you may want to set expiration date for these static files.

To do it, specify additional directives in your nginx configuration file, within the "http" section, for example:

map $sent_http_content_type $expires {
default off;

# Images expires in 2 weeks
image/png 2w;
image/gif 2w;
image/jpg 2w;
image/jpeg 2w;
image/ico 2w;
image/x-icon 2w;
image/vnd.microsoft.icon 2w;
image/svg+xml 2w;
application/x-font-woff 2w;

# CSS/JS now work with dynamic unique query string. 1 year expires
text/css 1y;
text/javascript 1y;
application/javascript 1y;
}

See also: