Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have openresty set up on a server (nginx bundle which includes the lua module) and I'm trying to create a script which has specific odds of setting a cookie on a user's browser. My code looks like this:

    location =/index.php {
        set $random_num 0;

        rewrite_by_lua '

                marth.randomseed(1);
                nvx.var.random_num = math.random(0,3);

        ';

        add_header Set-Cookie "random_num=$random_num; path=/; domain=...com;";
    }

I know my random seed function isn't actually very random, but I figured I'd deal with that later. At the moment I'm just trying to get nginx to set a random number, but doing so yields this error:

2012/07/11 11:27:20 [error] 5492#0: *44 lua handler aborted: runtime error: [string "rewrite_by_lua"]:3: attempt to ind
ex global 'marth' (a nil value)
stack traceback:

Can anyone tell me what I'm doing wrong, and if there's any other way to get a random number in nginx?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Based on your error log entry, it appears you have a simple typo.

            marth.randomseed(1);

This should read:

            math.randomseed(1);
share|improve this answer
    
Wow, and this is what I get for not sleeping –  Mediocre Gopher Jul 11 '12 at 17:43
2  
Sleep takes a lot of time, but it takes less time than debugging. I recommend sleep. –  Michael Hampton Jul 11 '12 at 17:54
1  
sleep 28800 is a good idea. –  Janne Pikkarainen Jul 11 '12 at 18:21

Your Answer

 
discard

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.