leev / ngx_http_geoip2_module

Nginx GeoIP2 module
BSD 2-Clause "Simplified" License
983 stars 186 forks source link

Is it possible to lookup to different keys same time #128

Closed fatihusta closed 8 months ago

fatihusta commented 9 months ago

First of all thank you for this project.

I found an issue that some times country information not available under the country key. Country information also stores under the registered_country.

Is it possible to lookup registered_country if not available under the country. details

By the way I'm not sure registered_country always available in database. That's why I'm asking this question.

Queries


mmdblookup -f GeoLite2-Country.mmdb -i 1.1.1.1

  {
    "registered_country":
      {
        "geoname_id":
          2077456 <uint32>
        "iso_code":
          "AU" <utf8_string>
        "names":
          {
            "de":
              "Australien" <utf8_string>
            "en":
              "Australia" <utf8_string>
            "es":
              "Australia" <utf8_string>
            "fr":
              "Australie" <utf8_string>
            "ja":
              "オーストラリア" <utf8_string>
            "pt-BR":
              "Austrália" <utf8_string>
            "ru":
              "Австралия" <utf8_string>
            "zh-CN":
              "澳大利亚" <utf8_string>
          }
      }
  }

mmdblookup -f GeoLite2-Country.mmdb -i 8.8.8.8

  {
    "continent":
      {
        "code":
          "NA" <utf8_string>
        "geoname_id":
          6255149 <uint32>
        "names":
          {
            "de":
              "Nordamerika" <utf8_string>
            "en":
              "North America" <utf8_string>
            "es":
              "Norteamérica" <utf8_string>
            "fr":
              "Amérique du Nord" <utf8_string>
            "ja":
              "北アメリカ" <utf8_string>
            "pt-BR":
              "América do Norte" <utf8_string>
            "ru":
              "Северная Америка" <utf8_string>
            "zh-CN":
              "北美洲" <utf8_string>
          }
      }
    "country":
      {
        "geoname_id":
          6252001 <uint32>
        "iso_code":
          "US" <utf8_string>
        "names":
          {
            "de":
              "Vereinigte Staaten" <utf8_string>
            "en":
              "United States" <utf8_string>
            "es":
              "Estados Unidos" <utf8_string>
            "fr":
              "États Unis" <utf8_string>
            "ja":
              "アメリカ" <utf8_string>
            "pt-BR":
              "EUA" <utf8_string>
            "ru":
              "США" <utf8_string>
            "zh-CN":
              "美国" <utf8_string>
          }
      }
    "registered_country":
      {
        "geoname_id":
          6252001 <uint32>
        "iso_code":
          "US" <utf8_string>
        "names":
          {
            "de":
              "Vereinigte Staaten" <utf8_string>
            "en":
              "United States" <utf8_string>
            "es":
              "Estados Unidos" <utf8_string>
            "fr":
              "États Unis" <utf8_string>
            "ja":
              "アメリカ" <utf8_string>
            "pt-BR":
              "EUA" <utf8_string>
            "ru":
              "США" <utf8_string>
            "zh-CN":
              "美国" <utf8_string>
          }
      }
  }
leev commented 8 months ago

To achieve this, you could do something like assigning both of those values to separate variables and then making the decision to use

$geoip2_data_country country names en
$geoip2_data_registered_country registered_country names en

Then use something like map to choose the right one (not sure if this is correct, but you can lookup map here: https://nginx.org/en/docs/http/ngx_http_map_module.html)

map "$geoip2_data_country" $country {
  default $geoip2_data_registered_country;
  ~. $geoip2_data_country;
}

You would now have $country available which would be $geoip2_data_country if it wasn't empty, otherwise $geoip2_data_registered_country would be used.

fatihusta commented 8 months ago

@leev Thank you for the answer. It worked.