Hi Andrei,
1) What you've got there seems to make sense - init the maxminddb module in init_by_lua_* and then access it in set_by_lua_* later. I use this a lot - 'require' in init_by_lua_* and then just use the module directly in code elsewhere. If 'geo' is nil in the set_by_lua_* section you might have some other problem. You can certainly run set_by_lua_* in server {} blocks - also in location {}, and if you can restrict what you're doing to specific location that makes sense, but if it's for every request, the server block should be fine. I might be missing something obvious, hopefully someone else will jump in if so.
2) You might want to use set_by_lua_block {} in your nginx configs rather than set_by_lua '', in the same way you've doing with init_by_lua_block {} - it's easier to write because you don't get problems with quotes, and (in my editor at least) the syntax highlighting is clearer. Also, using set_by_lua_file (and all the other *_by_file directives) is nice because you can put your Lua code in a separate file, set 'lua_code_cache off' in development, and then it'll load the Lua file for each request, so you won't need to keep reloading nginx to see your changes. (Set 'lua_code_cache on' again for production deployment though.)
3) You can probably do what you're trying to do directly in nginx, using the nginx geoip2 module (https://github.com/leev/ngx_http_geoip2_module), without having to write any Lua code. It's a bit of work building in the module, but it's a bit of work either way. If you check out the README you'll see some examples showing how it gives you nginx-scope vars like $geoip2_data_country_name - you can then either access those in your Lua code (using e.g. 'ngx.var.geoip2_data_country_name'), or directly in nginx (e.g. "add_header X-Country $geoip2_data_country_name;"). If I'm understanding right, then for what you describe, it might be enough just to do it in nginx like that - it's a native module and it should be pretty quick. I use it in production with no problems.
Sorry this reply is after you've done all the work to find out how to do it in lua & openresty! But maybe you'll stay with the lua route anyway, and hopefully your digging around will have given you a flavour of resty development :-)
Best
Igor
> On 9 Nov 2019, at 16:03, Andrei <la...@gmail.com> wrote:
>
> Hello,
>
> So I managed to get this working using the anjia0532/lua-resty-maxminddb opm, libmaxminddb-devel (for libmaxminddb.so), https://dev.maxmind.com/geoip/geoip2/geolite2/ and the following config entries in "/etc/nginx.conf" and "/etc/nginx/vhosts/vhost1.conf":
>
> >> /etc/nginx.conf <<
> http {
> init_by_lua_block {
> geo = require "resty.maxminddb"
> if not geo.initted() then
> geo.init("/etc/nginx/GeoLite2-Country.mmdb")
> end
> }
> include /etc/nginx/vhosts/*.conf;
> }
>
> >> /etc/nginx/vhosts/vhost1.conf <<
> server {
> listen 80;
> server_name vhost1.com;
> set_by_lua $country '
> local res,err = geo.lookup(ngx.var.arg_ip or ngx.var.remote_addr)
> if not res then
> return "-"
> else
> return res["country"]["iso_code"]
> end
> ';
> add_header X-Country $country;
> location ~* {
> return 200;
> }
> }
>
> I'm not sure if the lua variable scope is correct, and efficient considering the server will have hundreds of different server {} blocks, and the overall request rates can jump to the thousands per second. I read over https://github.com/openresty/lua-nginx-module#lua-variable-scope, tried using init_by_lua in the http {} context, but the 'geo' variable was 'nill' in the server { set_by_lua {} } context. I also did the init part directly in the server {} context using a local 'geo' variable, which worked fine, but I'm not sure if that's recommended considering I will have hundreds of vhost files included. Any optimization suggestions at this point would be greatly appreciated.
>
> On Mon, Nov 4, 2019 at 4:44 AM Andrei <la...@gmail.com> wrote:
> Hello,
>
> Does anyone have a working example for setting a X-Country header on OpenResty?
>
> On Wed, Jul 17, 2019 at 2:39 AM Andrei <la...@gmail.com> wrote:
> anyone?
>
> On Sun, Jul 14, 2019 at 4:14 AM Andrei <la...@gmail.com> wrote:
> Hello,
>
> Does anyone have a working example on how to set a X-Country header, which only contains the country code using Lua and maxminddb? I was looking over https://github.com/leafo/luajit-geoip and https://github.com/anjia0532/lua-resty-maxminddb but I don't have a good enough grasp of Lua to do the lookup and add in the header. Any help is greatly appreciated!
>
> --Andrei .