h5bp/server-configs

[nginx] Optional file descriptor cache for performance boost

aitte opened this issue · 2 comments

Nginx can be told to cache open file handles, "not found" errors, metadata about files and their permissions, etc.

The upside of this is that Nginx can immediately begin sending data when a popular file is requested, and will also immediately know to send a 404 if a file is missing on disk, and so on.

The downside is that the file information is indeed cached and handles are open, meaning that it takes Nginx a little while to react to actual changes to files/their state on disk (in the example below, inactive file descriptors will expire after 20 seconds in the cache, and valid (recently used) descriptors will be re-validated every 30 seconds).

It is an amazing feature for performance but should not be enabled by default.

Here is a reference:
http://wiki.nginx.org/HttpCoreModule#open_file_cache

And here is a good default setup:

# Uncomment this if you want to cache file descriptors and metadata, which greatly speeds up file access.
# However, it means that the server won't react immediately to changes on disk, which may be undesirable.
# Production servers with stable file collections will definitely want to enable the cache.
open_file_cache          max=1000 inactive=20s;
open_file_cache_valid    30s;
open_file_cache_min_uses 2;
open_file_cache_errors   on;

It could be stored in the main http{} block, or as a file to be included in server{} blocks. The latter is the best.

Here is my own "cache-file-descriptors.conf" include-file for my server blocks:

# This tells Nginx to cache open file handles, "not found" errors, metadata about files and their permissions, etc.
#
# The upside of this is that Nginx can immediately begin sending data when a popular file is requested, 
# and will also know to immediately send a 404 if a file is missing on disk, and so on.
#
# However, it also means that the server won't react immediately to changes on disk, which may be undesirable.
#
# In the below configuration, inactive files are released from the cache after 20 seconds, whereas 
# active (recently requested) files are re-validated every 30 seconds.
#
# Descriptors will not be cached unless they are used at least 2 times within 20 seconds (the inactive time).
#
# A maximum of the 1000 most recently used file descriptors can be cached at any time.
#
# Production servers with stable file collections will definitely want to enable the cache.
open_file_cache          max=1000 inactive=20s;
open_file_cache_valid    30s;
open_file_cache_min_uses 2;
open_file_cache_errors   on;

This and many other issues were fixed in the latest pull request.