My nginx.conf upstream testdb { postgres_server 10.0.9.43:9000 dbname=testdb user=postgres password=postgres; postgres_keepalive max=100 mode=multi overflow=ignore; } location / { set $x ""; content_by_lua ' local a = ngx.var.http_cookie local b = ngx.var.http_my_custom_auth print(a) print(b) if ngx.var.http_my_custom_auth ~= nil then local res = ngx.location.capture("/postgresquery", { args = {x = b } } ) print(res.status) print(res.header) print(res.body) -- I would like to do more actions based on the result. However, for now, we simply move ahead return ngx.exec("@final") elseif ngx.var.http_cookie ~= nil then return ngx.exec("@final") else ngx.status = 444 return end '; } location @final { # # This is an internal location. Cannot be accessed from the outside # internal; proxy_pass http://10.0.1.42; } location /postgresquery { internal; postgres_pass testdb; postgres_output text; postgres_escape $http_my_custom_auth; postgres_query "select id, name from testtable where key = $x"; } My error Log: 2014/06/24 13:01:36 [debug] 6073#0: epoll add event: fd:7 op:1 ev:00002001 2014/06/24 13:01:38 [debug] 6073#0: post event 099D1620 2014/06/24 13:01:38 [debug] 6073#0: delete posted event 099D1620 2014/06/24 13:01:38 [debug] 6073#0: accept on 0.0.0.0:80, ready: 0 2014/06/24 13:01:38 [debug] 6073#0: posix_memalign: 099A8D40:256 @16 2014/06/24 13:01:38 [debug] 6073#0: *1 accept: 127.0.0.1:57731 fd:11 2014/06/24 13:01:38 [debug] 6073#0: *1 event timer add: 11: 60000:3435819925 2014/06/24 13:01:38 [debug] 6073#0: *1 reusable connection: 1 2014/06/24 13:01:38 [debug] 6073#0: *1 epoll add event: fd:11 op:1 ev:80002001 2014/06/24 13:01:38 [debug] 6073#0: *1 post event 099D1688 2014/06/24 13:01:38 [debug] 6073#0: *1 delete posted event 099D1688 2014/06/24 13:01:38 [debug] 6073#0: *1 http wait request handler 2014/06/24 13:01:38 [debug] 6073#0: *1 malloc: 0999FA50:1024 2014/06/24 13:01:38 [debug] 6073#0: *1 recv: fd:11 98 of 1024 2014/06/24 13:01:38 [debug] 6073#0: *1 reusable connection: 0 2014/06/24 13:01:38 [debug] 6073#0: *1 posix_memalign: 099A56D0:4096 @16 2014/06/24 13:01:38 [debug] 6073#0: *1 http process request line 2014/06/24 13:01:38 [debug] 6073#0: *1 http request line: "HEAD / HTTP/1.1" 2014/06/24 13:01:38 [debug] 6073#0: *1 http uri: "/" 2014/06/24 13:01:38 [debug] 6073#0: *1 http args: "" 2014/06/24 13:01:38 [debug] 6073#0: *1 http exten: "" 2014/06/24 13:01:38 [debug] 6073#0: *1 http process request header line 2014/06/24 13:01:38 [debug] 6073#0: *1 http header: "User-Agent: curl/7.32.0" 2014/06/24 13:01:38 [debug] 6073#0: *1 http header: "Host: 127.0.0.1" 2014/06/24 13:01:38 [debug] 6073#0: *1 http header: "Accept: */*" 2014/06/24 13:01:38 [debug] 6073#0: *1 http header: "my-custom-auth: Vamshi" 2014/06/24 13:01:38 [debug] 6073#0: *1 http header done 2014/06/24 13:01:38 [debug] 6073#0: *1 event timer del: 11: 3435819925 2014/06/24 13:01:38 [debug] 6073#0: *1 rewrite phase: 0 2014/06/24 13:01:38 [debug] 6073#0: *1 test location: "/" 2014/06/24 13:01:38 [debug] 6073#0: *1 using configuration "/" 2014/06/24 13:01:38 [debug] 6073#0: *1 http cl:-1 max:1048576 2014/06/24 13:01:38 [debug] 6073#0: *1 rewrite phase: 2 2014/06/24 13:01:38 [debug] 6073#0: *1 http script value: "" 2014/06/24 13:01:38 [debug] 6073#0: *1 http script set $x 2014/06/24 13:01:38 [debug] 6073#0: *1 post rewrite phase: 3 2014/06/24 13:01:38 [debug] 6073#0: *1 generic phase: 4 2014/06/24 13:01:38 [debug] 6073#0: *1 generic phase: 5 2014/06/24 13:01:38 [debug] 6073#0: *1 access phase: 6 2014/06/24 13:01:38 [debug] 6073#0: *1 access phase: 7 2014/06/24 13:01:38 [debug] 6073#0: *1 post access phase: 8 2014/06/24 13:01:38 [debug] 6073#0: *1 lua content handler, uri:"/" c:1 2014/06/24 13:01:38 [debug] 6073#0: *1 lua reset ctx 2014/06/24 13:01:38 [debug] 6073#0: *1 lua creating new thread 2014/06/24 13:01:38 [debug] 6073#0: *1 http cleanup add: 099A5FAC 2014/06/24 13:01:38 [debug] 6073#0: *1 lua run thread, top:0 c:1 2014/06/24 13:01:38 [debug] 6074#0: epoll add event: fd:7 op:1 ev:00002001 Curl output: [vamshi@localhost ~]$ curl -I -H "my-custom-auth: Vamshi" http://127.0.0.1 curl: (52) Empty reply from server Postgres Table (on a different machine): 1 Row ID : 1 Name : Vamshi Krishna Ramaka Key: Vamshi I know that code 444 is being returned, because lua seems to find the cookie and my header as empty. Can you tell me why Lus thinks they are empty ? The current config is not production quality since this is my first attempt at nginx as well as Lua. I have almost removed everything else is trying to catch this bug, and this is where I have landed. So do excuse me if there are any glaringly obvious errors. -Vamshi Posted at Nginx Forum: http://forum.nginx.org/read.php?2,251127,251127#msg-251127
on 2014-06-24 10:25

on 2014-06-24 14:29

I found the issue(s). I am not sure why the error log did not have any logs after the lua thread got launched. Here is the updated /etc/nginx/nginx.conf location / { set $dbKey ""; content_by_lua ' local a = ngx.var.http_cookie local b = ngx.var.http_my_custom_auth print(a) print(b) if ngx.var.http_my_custom_auth ~= nil then local res = ngx.location.capture("/postgresquery", { args = {dbKey = b } } ) print(res.status) print(QueryResult) if res.status == 200 then return ngx.exec("@final") else ngx.status = 403 return end elseif ngx.var.http_cookie ~= nil then return ngx.exec("@final") else ngx.status = 444 return end '; } location /postgresquery { internal; postgres_pass testdb; postgres_escape $escaped_dbKey $arg_dbKey; postgres_query "select id from testtable where key = $escaped_dbKey;"; postgres_output value; } However, as you can see, I had to delete print(res.headers) and print(res.body), since lua was complaining that it expects a single value, but it got a table -Vamshi Posted at Nginx Forum: http://forum.nginx.org/read.php?2,251127,251129#msg-251129