openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.3k stars 2.03k forks source link

ngx.var.request_uri is sometimes nil #1648

Open ktalebian opened 4 years ago

ktalebian commented 4 years ago

We sometimes get an unhandled exception thrown to our nginx/error.log when we try to read ngx.var.request_uri and manipulate it, for example:

2020/02/13 13:06:48 [error] 11893#0: *195814 [lua] logger.lua:166: warning(): failed to post to rollbar because ./rollbar.lua:125: attempt to concatenate field 'request_uri' (a nil value) while logging request, client: 123.456.12.3, server: myapp.com request: "{}"

This is corresponding to local url = ngx.var.scheme .. '://' .. ngx.var.host .. ngx.var.request_uri.

I'm not sure how ngx.var.request_uri can ever be nil? Under what condition does this happen?

wanghuizzz commented 4 years ago

Please provide the version info of Openresty/Nginx, and whether any other Nginx modules are used?

tokers commented 4 years ago

@ktalebian AFAIK ngx.var.request_uri if the client sent a malformed HTTP request line which aborted the HTTP protocol parsing. Meanwhile, if you have a log_by_lua* directive inside HTTP block (or server block) directly, the Lua code specified by this directive still has a chance to run, and when you fetch $request_uri in your Lua code, you will get nil.