Get Free Audit

How to Cache Static Content With NGINX

Jun 27, 2016

16244 Andrey Litvin

How to Cache Static Content 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:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


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.

full_cache_store

Full Cache

Take your online store to the next level with BelVG module

Download here

Check out BelVG’s quality Magento extensions at our official store.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

3 Comments

  1. hi, here is the answer.
    Change
    “location ~ {”
    on 21 line
    “location ~ ^/([A-Za-z0-9]+) {“

  2. your code not work and return error:
    nginx: [emerg] “proxy_pass” cannot have URI part in location given by regular expression, or inside named location, or inside “if” statement, or inside “limit_except” block

Post a new comment

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