SSI module : Date wise caching in Nginx

Hi everyone, today we are going to see how to implement date wise caching in Nginx using SSI module. There happen to be time when we want to refresh the cache as soon as new date comes. That is, we need such caching when we have our content as static but only for a specific date and it changes with new date.

For the date-wise caching, it doesn’t need actually a lot of work. You just need to change the fastcgi_cache_key and append date as well. For the date, we can use $date_local variable in nginx. But the problem comes as $date_local contains time as well, it is in non readable format, just numbers, (we can’t rewrite it).

SSI module comes for the rescue here. SSI module is a great implementation in nginx which processes SSI(Server Side Include) commands in the response to be sent. We can format the $date_local variable using this, to a readble form and even modify it for just the date.

Then we can use $date_local variable and append it with cache_key. Please follow below to see some codes of how to achieve it.

server {

ssi on;

location ~ \.php$ {

fastcgi_cache_key $your_key|$date_local;

}

}

Now, in somewhere in your view(response to be sent) html tag include below ssi command,

less than html greater than

less than!–# config timefmt=”%A, %d-%b-%Y” –greater than

less than/html greater than

Now, above command will be executed by Nginx before it is sent to client. It changes the $date_local default format to the desired format. And so your cache_key will be generated date-wise. As soon as, new date comes a new cache will be generated, and for the same date, the response from the cache will be served.

So now with the help of SSI module of Nginx, date wise caching can be easily done.
Read my article here for how to install nginx from source, which is needed to install third party module(SSI module) for Nginx.

Leave a Reply

Your email address will not be published. Required fields are marked *