randomized static webpage content with just nginx config

look mom no javascript

you actually only need map and sub_filter (which are stock in the debian distribution of nginx)

the basic idea is: match on the current millisecond, and map that to your random content string. then, put a sentinel in your HTML to get replaced with the random string (here it's RANDOM_TEXT_SUB). finally, use sub_filter to do the replacement

here's how

# in http {

map $msec $rnd_text {
    default "";
    ~0$ "message 0";
    ~1$ "message 1";
    ~2$ "message 2";
    ~3$ "message 3";
    ~4$ "message 4";
    ~5$ "message 5";
    ~6$ "message 6";
    ~7$ "message 7";
    ~8$ "message 8";
    ~9$ "message 9";
}

server {
    # ....

    location /motd.html {
        sub_filter RANDOM_TEXT_SUB $rnd_text;
    }
}

if you have a number of random messages that isn't 10, you can match arbitrarily on ranges of trailing digits using regex (that's what the ~ does, it's a regex match). For example, for 3 messages you can use

~[0-3]$ "first message";
~[4-6]$ "second message";
~[7-9]$ "third message";

this isn't perfectly split, but it's good enough. you can use more trailing digits for more accuracy.

that's basically it. you can see a demo at https://joan.awoo.systems/random.html


you can also use this for different types of basic dynamic content. for example, once i was playing with nginx-rtmp and dash streaming, and wanted to provide a useful fallback for people with javascript disabled (which is, imo, a perfectly reasonably thing to want). so the noscript tag on this page says something along the lines of "you can view this stream in a regular video player at rtmp://...the url..."

now the problem for this was... this is a static html page, how do we insert the url especially when the context is specifically a lack of javascript? well, i used the same thing, with sub_filter inserting a url based on the request path, and it worked :P