ngx_http_random_index_module

Serve a random file from a directory as its index page

compiled inBSD-2-Clausesource docs

Overview

ngx_http_random_index_module processes requests whose URI ends with a slash and picks a random file from the matching directory to serve as the index file. Each request may therefore receive a different file from the same directory.

The module runs before ngx_http_index_module, so when it is enabled the ordinary index directive is never reached for a directory that contains at least one candidate file. Entries whose names start with a dot are skipped and only regular files are considered, so subdirectories are ignored; if the directory has no eligible file the request falls through to the index and autoindex handlers.

It is an official nginx module that upstream does not build by default; it needs the --with-http_random_index_module configure parameter. A typical use is rotating banner images, quotes or landing pages without any application code.

Key directives

random_indexEnables or disables module processing in the surrounding location, default off; location context only.
runs before indexProcessed ahead of ngx_http_index_module, so a random file wins over any configured index file.
hidden files skippedDirectory entries whose names begin with a dot are never chosen.
regular files onlySubdirectories are ignored; symlinks are resolved and count only when they point at a regular file.
empty directory fallbackWith no eligible file the module declines and the index or autoindex modules handle the request.

Example

nginx.conf
location /banners/ { root /srv/www; # each request to /banners/ gets a random file from that directory random_index on; } location / { root /srv/www; index index.html; }

Availability

Compiled into every n.wtf nginx binary. Install nginx-extras and the directives above work out of the box, no load_module line needed.