High-traffic static sites often hit filesystem limits before CPU limits, because every request can trigger repeated file lookups and opens for the same assets. Enabling the Nginx open_file_cache keeps hot file metadata and file descriptors in memory, reducing disk I/O and improving request latency under load.
When open_file_cache is enabled, each nginx worker maintains a cache of open file descriptors and cached results from file tests (existence, size, modification time). Requests that map to the same on-disk files can reuse cached entries instead of repeating expensive stat() and open() operations.
Cache settings trade freshness for fewer filesystem calls: open_file_cache_valid controls how often cached metadata is revalidated, and open_file_cache_errors can cache negative lookups like missing files. The max limit applies per worker process and can consume large numbers of file descriptors, so keep OS limits and deployment behavior in mind; service reload examples use systemd via systemctl.
Steps to enable open file cache in Nginx:
- Dump the loaded Nginx configuration to find the active server block file for static content.
$ 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; http { include /etc/nginx/mime.types; include /etc/nginx/conf.d/*.conf; ##### snipped #####The loaded configuration often starts at /etc/nginx/nginx.conf and pulls in additional files via include directives.
- Add open_file_cache directives to the static-files location block in the chosen server configuration file.
location /static/ { root /var/www/example; 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 affects filesystem-based responses (static files and try_files checks), not upstream/proxy caching; directives can be set in http, server, or location contexts and overridden in deeper contexts; disabling uses open_file_cache off; in the same context.
max is per worker process, so total potential open files can approach (worker_processes × max) on a busy node.
Oversizing max can exhaust file descriptors and trigger Too many open files errors; enabling open_file_cache_errors can also cache missing-file results, delaying visibility of newly deployed files until the next open_file_cache_valid revalidation.
- Validate the configuration syntax.
$ 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 nginx service.
$ sudo systemctl reload nginx
On systems without systemd, use sudo nginx -s reload.
- Confirm the nginx service is active.
$ sudo systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2025-12-14 12:34:56 UTC; 5s ago ##### snipped ##### - Verify the effective configuration dump includes the open_file_cache directives.
$ 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 ##### server { listen 80; server_name example.com; location /static/ { root /var/www/example; open_file_cache max=5000 inactive=60s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; } } ##### snipped #####
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.
Comment anonymously. Login not required.
