ngx_http_geoip2_module
ngx_http_geoip2_module copied to clipboard
Performance of DB lookup and options to reduce it
There are use-cases where the geo-ip lookup is needed for some of the virtual-hosts (nginx 'servers') and not for others. Futher, there can be cases where the lookup is needed only for certain 'locations' within a server block and not to others. Also, different server/location blocks need different geo-ip lookups (different variables). However, the 'geoip2' directive can only be defined in the context of 'http' or 'stream' blocks. This means that: a) The lookup happens for requests to every server/location irrespective of whether it requires geo-ip or not, which is performance wise not optimum. b) The variables needed in different blocks needs to be unioned and defined in the geoip2 directive. Not every variable may be needed in every request.
Have the above points been considered before? If so, what was the analysis and findings.
Would it help to allow defining the geoip2 directive (and consequently invoking the db lookup) at the server/location context?
cc: @leev
I think you'll find the behavior you're wanting is how it does work, but it has been sometime since I've looked at this.
My understanding is nginx variables are lazy loaded and only resolved when accessed, so if a request doesn't access one of the variables, the call won't be made to resolve it.
The variables are cached in nginx by setting the no_cacheable
flag on it. This means they are only resolved once per request and reused if needed.
https://github.com/leev/ngx_http_geoip2_module/blob/master/ngx_http_geoip2_module.c#L282-L284
https://github.com/nginx/nginx/blob/master/src/http/ngx_http_variables.c#L653
If you want to confirm, I would suggest sticking some ngx_log_error calls inside ngx_http_geoip2_variable and testing requests where you need geo data and where you don't as well as requests that use the same variable multiple times.