Nginx’s Open file cache

When it comes to serving static content, Nginx is quite a beast!

You can further improve static content handling by enabling open_file_cache in nginx.

Please note that open_file_cache will cache metadata about files only. Not actual content of files. So performance gain by this kind of cache may not be noticeable.

Since this is part of Nginx’s core HTTP module, you no need to worry about setup/installation.

open_file_cache

Just open /etc/nginx/nginx.conf

Add following lines in HTTP block:

open_file_cache          max=10000 inactive=5m;
open_file_cache_valid    2m;
open_file_cache_min_uses 1;
open_file_cache_errors   on;

Explanation

If you have way too many files, change max from 10000 to more appropriate value.

If files don’t change much often, or accesses less frequently, you can change inactive duration from 5m to something else. inactive andopen_file_cache_min_uses works together.

Above sample tells nginx to cache a file information as long as minimum 2 requests are made during 5m window.

open_file_cache_valid tell nginx to check if information it is holding is valid every 2 minutes.

open_file_cache_errors tell nginx to cache errors like 404 (file not found). If you are using nginx as load-balancer, leave this off.

For more details, check nginx docs.

Restart

Don’t forget to restart nginx.

nginx -t && service nginx restart

Leave a Reply