Nginx Caching Based On Page Size

Published on Author gryzli

The problem …

If you are using Nginx as a caching reverse proxy, at certain point of time you may want to cache only pages which fit specific size ranges.

This is usefull when you don’t want your cache storage to be filled with big files (moveis, iso’s or something like that), and you don’t have good mechanism to qualify and exclude these pages by using custom Location {} directives.

One good solution to this problem is to use conditional caching based on the page size you are caching.

How to cache based on page size ?

One way to do the conditional caching is by using map {} to check the size of the page received by your upstream and define certain limits (by using regex), for which you set option that controls whether the request to be cached or not.

Here is some simple configuration, which disables caching for pages bigger than 5,99Megabytes.

 

1) Define your map {} inside http {}

The first thing is to define your map clause, inside http clause, where you actually make the size comparison with regex:

$upstream_http_content_length is Nginx internal variable, which contains the size of the page received by our upstream server.

Whether the upstream_http_content_length is bigger or smaller than our defined regex values, we are going to set custom variable which I called: “$request_too_large” to either 0 or 1 .

Later we will use the variable $request_too_large to decide whether this request/response is cacheable or not.

 

2) Decide to cache or not based on $request_too_large

Now we should define inside our caching Location {} , that we don’t want to cache requests, which have $request_too_large set to 1.

This could be done by using “proxy_no_cache”.

 

Finally you should restart your Nginx and you can test out if everything is working as expected.