Enabling open_file_cache in Nginx reduces repeated filesystem lookups for frequently requested static files, which can lower disk activity and keep response times steadier when a site serves the same CSS, JavaScript, images, or downloads over and over.
Each worker can keep file descriptors, file metadata, directory existence checks, and optional lookup errors in memory instead of resolving them from disk on every request. The companion directives let you control how many entries are kept, how long rarely used entries stay cached, how often metadata is revalidated, and whether missing-file lookups should be cached too.
Use open_file_cache where Nginx serves files from local storage, such as a static asset location or a try_files path. The max value is per worker, open_file_cache_valid defaults to 60s when unset, and caching lookup errors can delay visibility of newly deployed files until the next revalidation window; examples below use the nginx systemd service with a non-systemd reload fallback.
Steps to enable open file cache in Nginx:
- Inspect the loaded configuration so you know which file and server block are active for the static content you want to optimize.
$ sudo nginx -T nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful ##### snipped ##### # configuration file /etc/nginx/nginx.conf: user www-data; worker_processes auto; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; ##### snipped #####
On Debian and Ubuntu, site files are commonly loaded from /etc/nginx/sites-available/ through /etc/nginx/sites-enabled/. On RHEL-family systems, server blocks are commonly stored under /etc/nginx/conf.d/.
- Edit the server or location block that serves files from disk and add the cache directives.
server { listen 80; server_name example.com; root /var/www/example.com/public; location /static/ { open_file_cache max=5000 inactive=60s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; } }open_file_cache, open_file_cache_valid, open_file_cache_min_uses, and open_file_cache_errors can be set in the http, server, or location context. The closest definition wins.
Keep the existing root or alias settings for the static path and add the cache directives in the same block that serves the files you want to optimize.
open_file_cache_errors on; caches negative lookups such as missing files. That reduces repeated disk checks, but a newly deployed file may still return a cached miss until the open_file_cache_valid interval expires.
- Test the configuration syntax before reloading Nginx.
$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
- Reload the service so the new directives are used by the running workers.
$ sudo systemctl reload nginx
When systemd is not managing the service, use sudo nginx -s reload to ask the master process to reload the configuration in place.
- Confirm the service is still running after the reload.
$ sudo systemctl is-active nginx active
If the service does not report active, review sudo journalctl --unit=nginx --no-pager --lines=20 or the Nginx error log before retrying the reload.
- Confirm the effective configuration now contains the cache directives.
$ sudo nginx -T 2>&1 | grep -n 'open_file_cache' 24: open_file_cache max=5000 inactive=60s; 25: open_file_cache_valid 30s; 26: open_file_cache_min_uses 2; 27: open_file_cache_errors on;
The -T switch performs the same syntax test as -t and also dumps the loaded configuration to standard output, which is useful when the active site file is assembled through multiple include directives.
- Request a representative static file and confirm Nginx still serves it after the reload.
$ curl -I -sS http://127.0.0.1/static/test.txt HTTP/1.1 200 OK Server: nginx/1.24.0 (Ubuntu) Date: Thu, 09 Apr 2026 12:49:48 GMT Content-Type: text/plain Content-Length: 12 Last-Modified: Thu, 09 Apr 2026 12:49:47 GMT Connection: keep-alive ETag: "69d7a06b-c" Accept-Ranges: bytes
This confirms that the static path still works after the configuration change. Replace the sample URL with a real CSS, JavaScript, image, or download path served by the location you updated.
Related: How to improve Nginx performance
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
