I had the same problem and I was searching for a long time without find anything. Luckily I've found a solution, and is to use the same compilation options of the installed version.
For example, I've installed openresty from the repository. To compile the module I've downloaded the module and placed it on same openresty src folder, then I've run this commands:
# Export the LUAJIT variables to avoid errors
/tmp/openresty-184.108.40.206/bundle/nginx-1.19.3# export LUAJIT_LIB="/usr/local/openresty/luajit/lib/"
/tmp/openresty-220.127.116.11/bundle/nginx-1.19.3# export LUAJIT_INC="../LuaJIT-2.1-20201027/src/"
# Getting compile options.
COMPILEOPTIONS=$(openresty -V 2>&1|grep -i "arguments"|cut -d ":" -f2-)
# Configure the compilation using the same options and add the geoIP2 module
eval ./configure $COMPILEOPTIONS --add-dynamic-module=../../modules/ngx_http_geoip2_module
# Compile the modules
# Copy the modules to desired folder and enjoy!
cp objs/ngx_http_geoip2_module.so /usr/local/openresty/nginx/modules/
The only tradeoff is that you need to install openresty on the machine that you plan to build the modules, but at least is working. I've compiled four modules (GeoIP2, PageSpeed, ModSecurity and Brotli) and at least three of them are working (I've not tested the PageSpeed module yet, but for sure that also is working).
I'll create an entry on my blog, because surely is usefull for someone else.