I decided to make my old dream come true and write about some nginx modules, which are used quite rarely. Today we will talk about random_index_module.
Suppose you want the user to receive slightly different content in a random way each time they visit the same page. Well, for example, your software has a bunch of awards from several software publications that you really want to demonstrate to the user, but at the same time, the place in the design of the page is allocated only for one picture. Or do you want to create a mini-banner system within one site with a random distribution of banners. :) Applications can be many.
The easiest option is to make several static page options and include the corresponding directive in the config.
')
location /index.html {
random_index on;
alias /www/root/random_pages/;
}
Thus, each time someone requests /index.html from you, nginx will access the / www / root / random_pages directory and give one of the files that it finds there, randomly.
However, there are a number of problems - if there are several “flashing” blocks, the number of page options may increase very quickly.
The solution is, if you remember about SSI. We describe a separate location for each set of our random blocks, for example:
location /banners/ {
random_index on;
alias /www/root/banners/;
}
location /awards/ {
random_index on;
alias /www/root/awards/;
}
And in the only (this time) page of the index.html we write:
<div id="banner">
<!--# include virtual="/banners/" -->
</div>
...
<div id="my_preciousss_awards">
<!--# include virtual="/awards/" -->
</div>
And all you have to do is to put in directories / www / root / banners / and / www / root / awards / in several (or at least one) file with the corresponding html content.
Of course, this scheme can be further improved by adding error handling (if you still forgot to put at least one file in the above folders), but this can be left "for homework." :)