Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to set random value in the specified range in variable without 3rd party modules in nginx's config?

Something like

set    $random_value         rand(1,4);
share|improve this question
What is your final goal? – quanta Nov 7 '11 at 10:14
@quanta: Final goal is fastcgi_cache_key "$request_method|$host|$request_uri|$random_value"; – RKI Nov 7 '11 at 10:15

using macports:

afte reading those

(has to be perl 5.8 or 5.10)

# port -f install perl5 +perl5_8
# port clean nginx
# port install nginx +perl5

I cant post more links, find the /HttpEchoModule#Installation on the nginx site

cd /opt/local/src/

download the tgz here: tar -zxvf /Users/lorieri/Downloads/agentzh-echo-nginx-module-v0.37rc7-1-gb0e0a23.tar.gz

port edit nginx

add those lines, between any other variant

variant echo description {Add echo } {
    configure.args-append --add-module=/opt/local/src/agentzh-echo-nginx-module-b0e0a23


# port install -v nginx +perl5 +echo

edit /opt/local/etc/nginx and mention in the previous (below) comment, in the http context then edit same file to add test location

    location /lorieri {
            default_type text/plain;
            echo $rnd;

run nginx

# nginx

test it with curl, wget, your browser...

# curl localhost/lorieri
# curl localhost/lorieri
# curl localhost/lorieri
# curl localhost/lorieri
# curl localhost/lorieri

why I did that ? to set on nginx a cookie user_uid for a year and a random cookie for the current session

share|improve this answer
        http {
             perl_set  $rnd 'sub { return int(rand(10 - 5)) + 5; }';

Using perl module.

And what is the goal of such caching?

share|improve this answer
nginx: [emerg] unknown directive "perl_set" – RKI Nov 7 '11 at 10:25
I want to do this without 3rd party modules. Goal of such caching is dynamic random content. – RKI Nov 7 '11 at 10:26
perl_module is standard but optional. Use --with-http_perl_module. – Vadim Nov 7 '11 at 10:34
It seems to me that you can store pages on disk by using backend script and then use random_index module: – Vadim Nov 7 '11 at 10:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.