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=/; 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?

  • just add that "nvx.var.random_num" should actually be ngx – lippea Aug 22 '17 at 6:02

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


This should read:

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

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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