Ngnix Static File Caching (location)

  • Posted September 18, 2014

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.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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.