Get Free Audit

How to Cache Static Files With NGINX

Jun 27, 201616697 Updated: February 25, 2025 Andrey Litvin
How to Cache Static Files 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

FAQ for NGINX static cache

How does Nginx cache static content?

NGINX static files cache configures the server to identify and serve files with specific extensions (e.g., images, CSS, JavaScript). It uses settings like expires for caching duration, gzip_static for compression, and Cache-Control headers to manage cache behavior.

What are the benefits of caching static files in Nginx?

The NGINX static file cache provides faster content delivery, reduced server load, and lower bandwidth usage. By caching static files, NGINX removes repetitive requests to the backend, speeding up page load times and improving user experience.

Can I cache dynamic content with Nginx, or is it only for static files?

NGINX can cache static and dynamic content, though it's more commonly used for static files. Caching dynamic content typically involves using tools like NGINX's proxy cache or FastCGI caching to store the output of dynamic pages. This can improve performance for dynamic sites by reducing server load and response time for repeated requests.

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 *