leev / ngx_http_geoip2_module

Nginx GeoIP2 module
BSD 2-Clause "Simplified" License
960 stars 182 forks source link

Read client IP from custom variable #5

Closed donbeave closed 10 years ago

donbeave commented 10 years ago

Hello!

Is it possible to specify from which variable read client IP?

I think to integrate this module with PowerDNS through remote backend, so it will be awesome if I can use $http_x_remotebackend_real_remote variable.

leev commented 10 years ago

Hi @donbeave

I think you should be able to achieve this by using ngx_http_realip_module (http://nginx.org/en/docs/http/ngx_http_realip_module.html).

Let me know if that suits your needs.

Cheers, Lee

donbeave commented 10 years ago

Hi @leev

It not worked for me.

In nginx config I tried two variants:

1)

  real_ip_header X-RemoteBackend-real-remote;

  geoip2 /usr/local/share/GeoIP2-Country/GeoIP2-Country.mmdb {
    $geoip2_data_country_code default=RU country iso_code;
  }

2)

  set_real_ip_from 119.161.140.138;

  geoip2 /usr/local/share/GeoIP2-Country/GeoIP2-Country.mmdb {
    $geoip2_data_country_code default=RU country iso_code;
  }

I check the values by this log format:

log_format ns '[$time_local] $http_x_remotebackend_real_remote $geoip2_data_country_code';

And in the result I see:

[08/Jul/2014:22:40:05 +0800] 203.80.210.55/32 RU
[08/Jul/2014:22:42:20 +0800] 203.80.210.55/32 RU
leev commented 10 years ago

I haven't used the realip module before, but did you try with both the real_ip_header and set_real_ip_from:

  real_ip_header X-RemoteBackend-real-remote;
  set_real_ip_from 119.161.140.138;

  geoip2 /usr/local/share/GeoIP2-Country/GeoIP2-Country.mmdb {
    $geoip2_data_country_code default=RU country iso_code;
  }
donbeave commented 10 years ago

Tried. But nothing changed...

leev commented 10 years ago

There currently isn't any other way to do it. I can look at adding something like 'geoip2_ip $variable'.

donbeave commented 10 years ago

Cool! Thank you. It's gonna be great!

leev commented 10 years ago

I think the problem might be that you have the /32 on the end of the IP. Can you send through just the IP?

I have just tested using the realip module and sending through that header and it has worked.

donbeave commented 10 years ago

Not worked, too. Can you share you config file?

leev commented 10 years ago
    set_real_ip_from   10.0.0.0/8;
    real_ip_header     X-RemoteBackend-real-remote;

    geoip2 /usr/share/GeoIP/GeoIP2-Country.mmdb {
        $geoip2_data_country_code default=GB country iso_code;
        $geoip2_data_country_name default=UnitedKingdom country names en;
    }

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" - '
                      '$geoip2_data_country_code $http_x_remotebackend_real_remote';

Sending the header: X-RemoteBackend-real-remote:8.8.8.8

Without realip: 10.192.4.10 - - [09/Jul/2014:13:19:30 +0000] "GET / HTTP/1.1" - GB 8.8.8.8 With realip: 8.8.8.8 - - [09/Jul/2014:13:20:04 +0000] "GET / HTTP/1.1" - US 8.8.8.8

leev commented 8 years ago

As an update, you can now do this:

$geoip2_data_country_code default=GB source=$variable country iso_code;

donbeave commented 8 years ago

@leev Thanks! Useful feature.