3

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);
2
  • What is your final goal?
    – quanta
    Nov 7, 2011 at 10:14
  • @quanta: Final goal is fastcgi_cache_key "$request_method|$host|$request_uri|$random_value";
    – RKI
    Nov 7, 2011 at 10:15

2 Answers 2

1

using macports:

afte reading those https://trac.macports.org/ticket/19342 https://stackoverflow.com/questions/7812596/installing-nginx-via-macports-with-ngx-echo-module-available

(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: https://github.com/agentzh/echo-nginx-module/downloads 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
}

then...

# 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
9
# curl localhost/lorieri
8
# curl localhost/lorieri
8
# curl localhost/lorieri
9
# curl localhost/lorieri
6

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

0
        http {
             perl_set  $rnd 'sub { return int(rand(10 - 5)) + 5; }';
             ...
        }

Using perl module.

And what is the goal of such caching?

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .