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

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 '

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


        add_header Set-Cookie "random_num=$random_num; path=/;;";

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
up vote 4 down vote accepted

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


This should read:

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

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.