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 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: That should provide you with a snapshot of your system’s memory. 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. 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 2GB RAM storage, run the command below: That will create RAM storage, but it’s not permanent. You can safely unmount it using the commands below: 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 Then copy the line below and append it to the bottom of the file. Save the file and exit. You have created mounted storage in RAM which can be used to 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 setup, by mine is like the one below: Save the file and exit. Restart Nginx When you do that, Nginx will automatically use the mounted storage for the cache. If you’re using PageSpeed Module, you’ll also need to specify its cache location. 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: