Welcome! Log In Create A New Profile

Advanced

Reading POST Body before the server block

Posted by mrw 
mrw
Reading POST Body before the server block
September 24, 2021 08:13AM
Like the title says, I'm curious if it would be possible to somehow read the POST body before the server block (either with a module or with some nginx setting).

I'm using the ngx_http_geoip2_module and sometimes the IPs that I need to parse are being passed to me in a POST body and I'm unable to access the POST body in the http block (which is the only place where I can make use of the geoip2 module). I also use the njs module and I tried to do a workaround by making a subrequest with GET variables to see if I can get it to work that way. The result was that even though nginx seems to be able to register the url parameters in the http block with this method the module still doesn't register the new variables and it doesn't change its output. This ultimately forces me to make a proxy_pass request to the same server with GET url parameters in order to parse the data. For performance reasons I would prefer not to use the proxy_pass approach and I would much more prefer it if I could somehow force the body to be read early on and to parse the parameters from the POST body via njs in the http block.

I'm using Nginx 1.21.3 and njs 0.6.2.

If anyone has any solutions or tips to help with this I'd appreciate it.
mrw
Re: Reading POST Body before the server block
September 24, 2021 05:00PM
Just in case someone else in a similar situation, I managed to solve the problem with js_var (njs module directive: http://nginx.org/en/docs/http/ngx_http_js_module.html#js_var ).

From the looks of it nginx (or the geoip2 module) seems to work in a way that it only assigns values to variables at the time they're called. With that in mind, in njs code I parsed the POST body and I took the ip value from it and assigned it to the js_var variable I created before. By doing that before calling any of the geoip2 variables I was able to start getting the correct values from them.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 68
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready