---
title: 'Microcaching nginx+apache and configuration nuances | Zomro'
description: 'This article will teach you how to configure micro-caching on your website.'
image: 'https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/seo%40server-panel.net/2024/09/30/dalle-2024-09-30-100314-a-3d-wireframe-structure-in-vibrant-shades-of-pink-and-purple-with-the-text-nginxapache-in-a-modern-futuristic-font-the-text-should-appear-integ_q50.jpg'
---

![opened image]()

  * [Zomro](https://zomro.com/)
  * [Useful articles](https://zomro.com/blog/articles/)
  * Microcaching nginx+apache and configuration nuances 



# Microcaching nginx+apache and configuration nuances

30-09-2024

In this article, we will discuss micro-caching: why it is needed, how to configure it, and what pitfalls may arise along the way.

![Micro-caching nginx+apache and configuration nuances - 1](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/seo%40server-panel.net/2024/09/30/dalle-2024-09-30-100314-a-3d-wireframe-structure-in-vibrant-shades-of-pink-and-purple-with-the-text-nginxapache-in-a-modern-futuristic-font-the-text-should-appear-integ_q50.jpg)

## 

## **What is micro-caching?**

Micro-caching is a type of cache that fully creates page files and allows serving a ready-made page instead of making a request to the backend server.

The main downside of such caching is, of course, that pages are updated very rarely. But what are the advantages of such caching?

Let's take a situation with a weak server and a fairly fast website, but it receives a large number of requests from bots. They will load both the backend and the database, which will lead to a drop in website performance, and it will load more slowly. Here, micro-caching comes to the rescue: we simply serve already prepared pages, without loading either the backend or the database server. Micro-caching also allows pages to load much faster because there is no need to wait for a response from the backend with each request.

Such cache is not suitable for dynamic websites and admin panel pages, but if you have a page that you update, for example, once a day, then this is an excellent option.

Now we can move on to configuring micro-caching.

## **Configuring micro-caching**

For this, we need a directory where our cache will be stored.

In this article, a part of RAM will be used as disk space, as it is more efficient than storing such files on disk.

First, let's create a directory where our cache will be stored, in our case `/home/cache`:
    
    
     
    
    
    mkdir /home/cache

We will mount 1 GB of RAM as storage for files using the command:
    
    
     
    
    
    mount -t tmpfs -o size=1G tmpfs  /home/cache

And we will add the mounting of this space at each server startup (you need to add this line to the `/etc/fstab` file):
    
    
     
    
    
    tmpfs /home/cache tmpfs rw,size=1G 0 0

We also set secure permissions for the directory:
    
    
     
    
    
    chown www-data:www-data -R /home/cache
    
    
    chmod 700 /home/cache

![Micro-caching nginx+apache and configuration nuances - 2](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/dmytro.gerasymchuk%40zomro.org/2024/09/30/screenshot_180_q50.jpg)

Done, we have prepared a place for the cache. The advantage of this solution is that when the server is restarted, the entire cache is cleared.

Now we will address potential pitfalls that may hinder working with the cache, and due to which it may not work even if cache files are being created. The first thing to do is to set the PHP parameter **`session.cache_limiter`** to **`public`** , otherwise the website header will indicate `Pragma: no-cache`, which will prevent us from using and creating the cache correctly.

![Micro-caching nginx+apache and configuration nuances - 3](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/dmytro.gerasymchuk%40zomro.org/2024/09/30/screenshot_184_q50.jpg)

The second pitfall is that you need to add a line to your website's `.htaccess` file. I recommend setting `max-age` the same as the rest of the cache, so that the cache does not become stale:
    
    
     
    
    
    Header set Cache-Control "max-age=300, public"

![Micro-caching nginx+apache and configuration nuances - 4](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/dmytro.gerasymchuk%40zomro.org/2024/09/30/screenshot_181_q50.jpg)

Now we can be sure that almost nothing will prevent us from using the cache. The most important thing I will leave for last, namely the configuration of Nginx.

In the file `/etc/nginx/nginx.conf`, you need to add the following lines to the `http` section:
    
    
     
    
    
    proxy_cache_path /home/cache levels=1:2 keys_zone=global_cache:100m max_size=1g inactive=5m;
    proxy_cache_valid any 5m;
    proxy_cache_valid 200 5m;
    proxy_cache_valid 404 15s;
    proxy_cache_use_stale error timeout invalid_header updating http_500 http_503;
    proxy_cache_lock on;

![Micro-caching nginx+apache and configuration nuances - 5](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/dmytro.gerasymchuk%40zomro.org/2024/09/30/screenshot_182_q50.jpg)

In the configuration file of the site itself, in the `location /` block, you need to add the following lines: **``**
    
    
    location / {
    proxy_cache global_cache;
    add_header X-Cache-Status $upstream_cache_status;
    proxy_cache_key $host$request_uri;
    proxy_ignore_headers "Set-Cookie";
    proxy_pass http://backend;
    }

![Micro-caching nginx+apache and configuration nuances - 6](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/dmytro.gerasymchuk%40zomro.org/2024/09/30/screenshot_183_q50.jpg)

For ISPManager, these lines need to be added to the `location @fallback` block, as all requests to the backend go through this block.

Now I will explain what each line we added in the configurations is responsible for:

  * `proxy_cache_path /home/cache levels=1:2 keys_zone=global_cache:100m max_size=1g inactive=5m;`

    * **proxy_cache_path** — the path where the cache will be stored.
    * **levels=1:2** — you can use no more than 3 levels of nesting (for example, 1:2:3). The numbers correspond to the number of characters in the names of the cache directories, which is necessary for fast operation on slow disks. If you use only `levels=1`, on slow disks the cache will be of little use, as a large number of files will be created. I recommend using the ready-made configuration.
    * **keys_zone=global_cache:100m** — the name of the zone that our sites will refer to. If the cache is configured for the `global_cache` zone, it will look for files specifically in the `/home/cache` directory. `100m` — the size of the name zone for the cache.
    * **max_size=1g** — the total size of the cache.
    * **inactive=5m** — the time during which the cache is considered active. If the cache is not accessed during this time, the system considers it inactive and may delete it.
  * `proxy_cache_valid any 5m;` — the time during which the cache will be stored. `any` means that all HTTP codes, except those specified separately, will be stored for 5 minutes.

  * `proxy_cache_use_stale error timeout invalid_header updating http_500 http_503;` — allows using stale cache in case of the errors specified in the line.

  * `proxy_cache_lock on;` — prevents the creation of identical caches from multiple requests simultaneously.

  * `proxy_cache global_cache;` — specifies which cache zone the site will use.

  * `proxy_cache_key $host$request_uri;` — cache status check. If the header shows `X-Cache-Status: HIT`, then everything is configured correctly. If `MISS`, then you need to look for reasons.

  * `proxy_ignore_headers "Set-Cookie";` — allows creating a request while ignoring cookies. Without this, the cache will not show `HIT`, as each request will be unique.

  * `proxy_pass http://backend;` — here you need to specify the IP and port of the backend server (for example, `localhost:8080`, if Apache2 is used as the backend server).




You can check the cache status with the command:
    
    
     
    
    
    curl -I http://Your_site

​​​​​​​![Micro-caching nginx+apache and configuration nuances - 7](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/dmytro.gerasymchuk%40zomro.org/2024/09/30/screenshot_185_q50.jpg)

In the response, there will be a header `X-Cache-Status: HIT` (or `MISS`, `BYPASS`). Anything that is not `HIT` is an error.

Done. We have configured micro-caching on the site.

*The site articlesite.com was created specifically for the article.

We also suggest considering other useful articles: 

  * [How to install the FileGator file manager on Centos 7. (Apache-Nginx)](https://zomro.com/rus/blog/faq/380-how-to-install-the-filegator-file-manager-on-centos-7-apache-nginx)
  * [From zero to hero: building a LEMP server on Rocky Linux 9](https://zomro.com/rus/blog/faq/499-from-zero-to-hero-building-a-lemp-server-on-rocky-linux-9)
  * [Helpful tips and hacks for ISPManager users](https://zomro.com/rus/blog/articles/324-helpful-tips-and-hacks-for-ispmanager-users)



Similar articles:

[What Is FTP and Why Would You Need an FTP Server?](https://zomro.com/blog/articles/175-chto-takoe-ftp-i-zachem-vam-nuzhen-ftp-server) [A detailed guide to creating a responsive and user-friendly website: from idea to launch](https://zomro.com/blog/articles/596-guide-to-creating-responsive-user-friendly-website) [What is the purpose of a Samba server?](https://zomro.com/blog/articles/528-what-is-a-samba-server-for) [API - Retrieving information about available rates and their parameters](https://zomro.com/blog/articles/464-api-obtaining-information-about-available-tariffs-and-their-parameters) [How to Deploy a Website or Web Application: A Practical Guide from Zomro](https://zomro.com/blog/articles/733-how-to-deploy-a-website-or-web-application)

[ GPU server from €174.80/mon ](/dedicated-servers/type=gpu)
