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 a smart move.
By default, Nginx will store its cache data on 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.
If you’re a student or a new user, you will find out that the easiest place to start learning Linux is on Ubuntu Linux OS.
It’s a great Linux operating system for beginners.
Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other device.
While learning Ubuntu, you will find that Linux isn’t so different than Windows and other operating systems in so many ways, especially when it comes to using the system to get work done.
To get stated, follow the steps below:
Step 1: Find System Memory Usage
To find out if your system have enough RAM, one way is to run the commands below:
That should provide you with a snapshot of your systems’ 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 used and free.
Step 2: Create a tmpfs RAM Space
Now that you know the amount of memory available, continue below to create a directory which 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’re going to be using 2GB RAM storage. To create a 2GB RAM storage, run the command below:
sudo mount -t tmpfs -o size=2G tmpfs /var/cache/nginx/ramcache
That will create a RAM storage, but it’s not permanent.
You can safe 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 bottom of the file.
sudo tmpfs /var/cache/nginx/ramcache tmpfs defaults,size=2G 0 0
Save the file and exit.
You have created a mounted storage in RAM which can be used to store Nginx caches.
Step 3: Setup Nginx to Use the Storage
Now that the storage space is created, open Nginx configuration and use the storage space created above.
Now, I don’t know how your Nginx cache is setup, 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.
sudo systemctl reload nginx
When you do that, Nginx will automatically use the mounted storage for cache.
If you’re using PageSpeed Module, you’ll also need to specify it 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!
This post showed you how to setup 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: