testcookie-nginx-module icon indicating copy to clipboard operation
testcookie-nginx-module copied to clipboard

Accessing testcookie_ok variable from log_by_lua_block

Open tokumeo opened this issue 5 years ago • 6 comments

I have a use case for setting another variable depending on the value of testcookie_ok variable. I'm trying to achieve this with lua-nginx-module by using log_by_lua_block directive. Unfortunately, when reading testcookie_ok from lua using ngx.var API from log_by_lua_block, the value of testcookie_ok is always nil. But when adding testcookie_ok to access log, the value is either "0" or "1" depending on the results of the validation. Could you please provide some insight on why testcookie_ok might be nil when reading it from log_by_lua_block?

   testcookie_name YouShallNotPass;
   testcookie_session $remote_addr;
   testcookie on;

    log_by_lua_block {
        ngx.log(ngx.DEBUG, "testcookie_ok=", ngx.var.testcookie_ok)
    }

Here are the logs:

# no cookie
2018/12/19 14:17:13 [debug] 19639#19639: *19409 [lua] log_by_lua(default.conf:33):3: testcookie_ok=nil
192.168.122.1 - - [19/Dec/2018:14:17:13 +0300] "GET / HTTP/1.1" 307 171 "-" "curl/7.47.0" "-" testcookie_ok=0

# correct cookie
2018/12/19 14:17:53 [debug] 19639#19639: *19490 [lua] log_by_lua(default.conf:33):3: testcookie_ok=nil
192.168.122.1 - - [19/Dec/2018:14:17:53 +0300] "GET / HTTP/1.1" 200 0 "-" "curl/7.47.0" "-" testcookie_ok=1

I've tried compiling nginx with different order of testcookie and lua-nginx modules, but the situation doesn't change.

tokumeo avatar Dec 19 '18 11:12 tokumeo

Have you tried to change modules ordering during nginx compilation?

kyprizel avatar Dec 19 '18 11:12 kyprizel

Yes, I tried both --add-module=testcookie-nginx-module --add-module=lua-nginx-module and --add-module=lua-nginx-module --add-module=testcookie-nginx-module

tokumeo avatar Dec 19 '18 11:12 tokumeo

@tokumeo have you tried to use pre-built packages of nginx with Lua from openresty.org?

hroost avatar Dec 19 '18 12:12 hroost

@tokumeo have you tried to use pre-built packages of nginx with Lua from openresty.org?

No, I haven't tried that

tokumeo avatar Dec 19 '18 13:12 tokumeo

I've managed to work around this problem using map directive for now, but I'm still curious why I can't get testcookie_ok variable using log_by_lua_block

tokumeo avatar Dec 19 '18 13:12 tokumeo

@tokumeo Can you share example?

Mecanik avatar Jan 11 '19 17:01 Mecanik