Hong Manh's BlogHong Manh's Blog Sharing Technology Knowledge

Improve Nginx cache performance with tmpfs

  • Nginx
  • 1 August, 2023
  • (0)

If you are using ngx_pagespeed, fastcgi_cache or proxy_cache with Nginx, you can easily improve your sites performance by mounting Nginx cache in RAM with tmpfs.

 

Point to check before mounting Nginx cache in RAM

RAM usage

Just make sure there is enough free RAM on your server before mounting a directory with tmpfs, you can use the command free -h to check RAM usage.

Nginx cache path

If there is enough RAM available on your server, the next step is to find Nginx cache path. For fastcgi_cache and proxy_cache, you can use the following command to identify Nginx cache path :

grep "cache_path" -r /etc/nginx

Usually fastcgi_cache_path is /var/run/nginx-cache and proxy_cache_path is /var/cache/nginx.

For ngx_pagespeed, you can use the following command to find Nginx cache path :

grep "FileCachePath" -r /etc/nginx

Mounting Nginx cache in RAM

To mount the directory /var/cache/nginx in RAM with tmpfs, you can use the command :

sudo mount -t tmpfs -o size=256M tmpfs /var/cache/nginx

You can check the result with df -h :

tmpfs
tmpfs

Then to make this configuration permanent, you can add the following line into /etc/fstab.

tmpfs /var/cache/nginx tmpfs defaults,size=256M 0 0
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Press ESC to close

0
Would love your thoughts, please comment.x
()
x