Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a http request be sent with the nginx location directive?

Maybe this is trivial, but I haven't found anything meaningful or I didn't know where to look...

(How) is it possible to to send a curl / whatever command as soon as a certain path is requested?

Something along these lines, but that would actually work:

location / {
curl --data 'v=1&t=pageview&tid=UA-XXXXXXXX-X&cid=123&dp=hit'  https://google-analytics.com/collect
}
like image 483
lucian Avatar asked Dec 06 '22 10:12

lucian


1 Answers

(as pointed out in the comments), ngx_http_lua_module can do it!

location / {
          access_by_lua_block  {
            os.execute("/usr/bin/curl --data 'v=1&t=pageview&tid=UA-XXXXXXXX-X&cid=123&dp=hit'  https://google-analytics.com/collect >/dev/null 2>/dev/null") 
        }
}

note that the execution halts the pageload until curl has finished. to run curl in the background and continue the pageload immediately, add a space and an & to the end so it looks like

>/dev/null 2>/dev/null &")
like image 113
hanshenrik Avatar answered Dec 09 '22 16:12

hanshenrik