I'm using nginx to server my static content, is there a way that I can set the expires headers for every file that meets a specific rule? For example can I set the expires header for all files that have an extension of '.css'?
I'm using nginx to server my static content, is there a way that I can set the expires headers for every file that meets a specific rule? For example can I set the expires header for all files that have an extension of '.css'?
I prefer to do a more complete cache header, in addition to some more file extensions. The '?' prefix is a 'non-capturing' mark, nginx won't create a $1. It helps to reduce unnecessary load.
The
location
directiveThe
expires
directiveI don't have enough reputation to comment on why the accepted answer would cause the files to no longer show up, but I figured it out and would like to help out!
Short version:
Make sure you have a root directory specified for your location block on images if you do not have a global one set!
Long version below:
First of all, my method of implementing this solution was really similar to this answer, where you write the rule (as in the accepted answer):
into a file img-cache.conf
and then include that file into your
server {...}
directive.My example of somesite.com in my sites-available folder:
That way you can add the image caching location block to multiple sites you might be running.
Second of all, I have a situation where my /var/www/ contains two folders that I allow as public_html - secure and training, so I have to make specific location blocks in my site's server directive singling out these folders.
As such, I do not have a global root directory set.
So when you make your image location blocks, you may not be providing them with a root directory from which to look for the images in!
My solution was then to:
You can also set the expires to maximum. Here is the directive I use for css and js.
All the aforementioned solutions will deny the possibility to have different aliases for different paths. Also for the sake of having all your different cache expirations in one place you should use nginx map in the following way.
...
Off
disables caching,epoch
(for unix epoch) results in resource always being refetched,max
sets the date to browser max value.The ~image/ matches any image types.
More about nginx maps at http://nginx.org/en/docs/http/ngx_http_map_module.html.
If you have one place that is home to all your static files, something like this will do...
The accepted answer caused nginx to not find any of my static files. Not really sure why, but this is a simple alternative.
Most of the answers on this page are extremely outdated and/or convoluted...
For starters, the
Expires
header is not a good option these days, nor is thePragma
header... ideally, you should disable both of these headers in your Nginx configuration.It's much cleaner to set a single
Cache-Control
header on your static assets location block:Remember that for Nginx, if you use the
expires
directive such asexpires max
this is NOT just setting an HTTPExpires
header, it's also setting aCache-Control
header too, so it can get very messy rather quickly.TLDR just use
Cache-Control
on your static files so that CDNs like Cloudflare can cache them at the edge and/or browsers can cache them for a while.And I recommend NOT using any cache headers for your page/HTML content on dynamic sites like WordPress because it will only lead to conflicts and confusion... if you really want to, you can just use an
etag
for your pages, but I personally don't and think those make more sense when using aggressive output caches like Varnish or Litespeed's LS Cache.The approaches above are what we use in SlickStack and it works fantastic for performance and stability, while guarding against e.g. WordPress plugins (or people) messing with your settings.
Step1: Configuring Cache-Control and Expires Headers:
Add the following above the
server
block:And this line within
server
block.expires $expires;
Source: DigitalOcean