How to Improve Nginx Cache Performance with tmpfs on Ubuntu

This brief tutorial shows students and new users how to improve Nginx performance by mounting ngx_pagespeed, fastcgi_cache, or proxy_cache in RAM or memory with tmpfs.

Nowadays, everyone wants their websites to run faster. If you’re using Nginx with either of the caching modules above, mounting any of those cache pages in memory via tmpfs is smart.

By default, Nginx will store its cache data on a disk. Even if you’re using SSD disks, caches in RAM will still perform better than on disk.

If you have enough RAM on your server, one of the best optimization methods is mounting cache pages in RAM.

The steps below will get you started easily.

To get started, follow the steps below:

Find System Memory Usage

To find out if your system has enough RAM, one way is to run the commands below:

free -h

That should provide you with a snapshot of your system’s memory.

              total        used        free      shared  buff/cache   available
Mem:            15G        2.8G        9.5G        471M        3.3G         12G
Swap:            0B          0B          0B

You’ll see the total memory available, how much is used, and free.

Create a tmpfs RAM Space

Now that you know the amount of memory available, continue below to create a directory that will be mounted in RAM.

Since by default Nginx uses /var/cache/nginx directory, it’s reasonable to create a directory there called ramcache.

Run the commands below to create a directory called ramcache in the /var/cache/nginx directory.

sudo mkdir -p /var/cache/nginx/ramcache

After creating the directory, use the tmpfs command to mount it in RAM

For this tutorial, we will use 2GB RAM storage. To create 2GB RAM storage, run the command below:

sudo mount -t tmpfs -o size=2G tmpfs /var/cache/nginx/ramcache

That will create RAM storage, but it’s not permanent.

You can safely unmount it using the commands below:

sudo umount /var/cache/nginx/ramcache

If you want the RAM storage to be permanent and always mount when the server reboots, run the commands below to open the /etc/fstab file

sudo nano /etc/fstab

Then copy the line below and append it to the bottom of the file.

sudo tmpfs /var/cache/nginx/ramcache tmpfs defaults,size=2G 0 0

Save the file and exit.

You have created mounted storage in RAM that can store Nginx caches.

Setup Nginx to Use the Storage

Now that the storage space is created, open the Nginx configuration and use the storage space created above.

Now, I don’t know how your Nginx cache is set up, by mine is like the one below:

fastcgi_cache_path /var/cache/nginx/ramcache/fastcgi_temp/ levels=1:2 keys_zone=cachezone:100m max_size=2g inactive=60m use_temp_path=off;
       fastcgi_cache_key $scheme$request_method$host$request_uri;
       fastcgi_cache_revalidate on;
       fastcgi_cache_background_update on;
       fastcgi_cache_use_stale error timeout invalid_header updating http_500;
       fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

Save the file and exit.

Restart Nginx

sudo systemctl reload nginx

Nginx will automatically use the mounted storage for the cache when you do that.

If you’re using PageSpeed Module, you’ll need to specify its cache location.

pagespeed EnableCachePurge on;
pagespeed PurgeMethod PURGE;
pagespeed ImplicitCacheTtlMs 31536000000;
pagespeed FileCachePath "/var/cache/nginx/ramcache/ngx_pagespeed/";
pagespeed CreateSharedMemoryMetadataCache "/var/cache/nginx/ramcache/ngx_pagespeed/" 1000000;
pagespeed DefaultSharedMemoryCacheKB 5000000;

That should do it!

Conclusion:

This post showed you how to set up the Nginx cache to use mounted RAM storage. If you find any error above, please use the comment form below to report.

You may also like the post below: