Ngnix Static File Caching (location)

September 18, 2014 4k views

Hi all! I follow this tutorial to optimize ngnix . In Static File caching section can you read:

It's possible to set expire headers for files that don't change and are served regularly. This directive can be added to the actual Nginx server block.

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
Add and remove any of the file types in the array above to match the types of files your Nginx servers.

Where should be placed this?

If i put it in http block the nginx.conf fail! And i haven't server block in my nginx.conf.

1 Answer

From the nginx documentation on the location directive:

Context: server, location

That means that a location directive can only be placed inside a server block or another location block (which is why adding it to the http block didn't work). Your server blocks are most likely stored in /etc/nginx/sites-enabled, so you will need to add

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}

to the file in /etc/nginx/sites-enabled that contains you site's configuration.

Have another answer? Share your knowledge.