Search code examples
nginxcurlluaopenresty

Openresty log_by_lua_block non blocking http-request


In one of nginx locations I have log_by_lua_block, where I need to send several ngx.var variables to remote server over http. If I use something like Lua-cURL it block nginx worker until the request is completed. What is the correct way to send non blocking http-request from log_by_lua_block?

Something like:

location / {
   proxy_pass http://host;

   log_by_lua_block {  
          someAsyncCurlRequest(ngx.var)
   }    
}

Solution

  • I did so:

    location /proxy/ { 
        rewrite /proxy/(.*) /$1  break; 
        allow 127.0.0.1; 
        deny all; 
        proxy_pass $1; 
    }
    
    log_by_lua_block {  
        local cjson = require "cjson.safe"
        local postData = {}
        postData["userIP"] = $remote_addr
        res = ngx.location.capture_multi{{"/proxy/http://urlPath",{ method = ngx.HTTP_POST, body = cjson.encode(postData)}},}
    }