random_index
The 'random_index' directive enables NGINX to serve files in a random order from a specified directory.
Description
The 'random_index' directive is used within a location block to modify the behavior of directory listings. When the directive is set to 'on', NGINX will randomly select one of the files in the specified directory to be served, rather than listing all available files in a static order. This can be particularly useful in scenarios where the directory contains a large number of files, allowing users to encounter different files during repeated visits, enhancing the browsing experience in certain contexts.
The syntax for the 'random_index' directive is simple and only accepts a flag argument. The directive essentially impacts the internal handler's output when a directory is requested, and is evaluated in the context of the location block. It helps in reducing predictability in file serving, potentially aiding in content delivery strategies where dynamic outcomes are preferred over static file listings. In practice, integrating the 'random_index' directive simply requires adding it into the relevant location block within your server configuration.
One crucial note is that this directive will only have an effect if directory indexing is enabled, usually through the 'autoindex' directive. Therefore, it acts in conjunction with other directives, and ensuring the proper setup in the accompanying configuration is key to achieving the desired functionality.
Config Example
location /files {
autoindex on;
random_index on;
}Ensure 'autoindex' is enabled; 'random_index' won't work otherwise.
Random file selection may lead to unexpected behavior if users refresh frequently.