Static Content Caching With NGINX

Static Content Caching With NGINX

NGINX is a powerful tool for static content delivery (ex. images, JavaScript, CSS, etc.). It proposes a simple mechanism for caching on a client’s side allowing to reduce server load and increase content delivery speed.

Below an example of server configuration is shown.

Let us examine it.

We specify the port which should be used by our caching proxy (NGINX):

We specify website URL:

Then we configure directory where server should look for the static files:

In order to serve the dynamic content properly we specify the following instructions that allow sending query and IP variables:

The next step will be to define processed extensions of static content files.

We disable logging of static content (increases content delivery speed and reduces the size of log files).

Further on we enable gzip compression:

Most favorable compression size is 5. This can be seen from the table below:

Compression rate Number queries per second Compressed size(original 170kb)
1 370 51,7
2 350 48,9
3 294 45,7
4 242 44,2
5 181 41,3
6 134 39,7
7 115 39,5
8 103 39,4
9 102 39,4

Later we specify cache lifetime:

The next step is to specify caching area:

Directive «private» allows only browser caching, while «public» option tells to any invisible proxies to cache our content as well (more about Cache-Control header).

Then we try to deliver the requested file or if it is not found — pass request to the dynamic processor:

In the end we should specify server address for processing dynamic content:

The NGINX configuration for static content delivery is finished.

1 comment

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *