0
0
Nginxdevops~5 mins

Open file cache in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a web server reads files repeatedly, it can slow down if it opens the same files over and over. Open file cache helps by keeping file information ready in memory, so the server can access files faster without reopening them each time.
When your website serves many static files like images or stylesheets and you want faster loading times.
When your server handles many requests for the same files and you want to reduce disk access.
When you want to improve performance without changing your hardware.
When you notice slow response times due to frequent file opening operations.
When you want to reduce the load on your storage system by caching file metadata.
Config File - nginx.conf
nginx.conf
http {
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
            index index.html;
        }
    }
}

open_file_cache sets the maximum number of cached files and how long inactive entries stay cached.

open_file_cache_valid defines how often the cache is refreshed.

open_file_cache_min_uses sets the minimum number of times a file must be accessed to be cached.

open_file_cache_errors enables caching of file errors like "file not found" to avoid repeated disk checks.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the server to verify it serves files correctly with the new cache settings.
Terminal
curl -I http://localhost/index.html
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 31 Dec 2024 10:00:00 GMT Connection: keep-alive ETag: "5e2a-5c3a5f8b4a000" Accept-Ranges: bytes
Key Concept

If you remember nothing else from this pattern, remember: open file cache keeps file info ready in memory to speed up repeated file access and reduce disk load.

Common Mistakes
Setting open_file_cache max value too low or too high without testing.
Too low means not enough files are cached, so performance gains are minimal; too high wastes memory and can cause instability.
Start with a moderate max value like 1000 and adjust based on server memory and workload.
Not reloading nginx after changing the configuration.
Changes won't take effect until nginx reloads, so caching won't work as expected.
Always run 'sudo nginx -t' to check config, then 'sudo systemctl reload nginx' to apply changes.
Ignoring caching of file errors by leaving open_file_cache_errors off.
Repeatedly checking for missing files wastes resources and slows down the server.
Enable open_file_cache_errors on to cache file errors and reduce unnecessary disk checks.
Summary
Configure open_file_cache in nginx.conf to cache file metadata and improve performance.
Test the configuration syntax with 'nginx -t' before applying changes.
Reload nginx to apply the new cache settings without downtime.
Verify the server serves files correctly after enabling open file cache.