How to pass GeoIP Country ISO as header to upstream

1. Output of caddy version:

v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=

2. How I run Caddy:

a. System environment:

Ubuntu 22.04 LTS

b. Command:

c. Service/unit/compose file:

NA

d. My complete Caddy config:

{
  "admin": {
    "enforce_origin": true,
    "listen": "",
    "origins": [
      "REDACTED"
    ]
  },
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "handle": [
                {
                  "account_id": 0,
                  "api_key": "",
                  "db_path": "/etc/caddy/GeoLite2-Country.mmdb",
                  "download_frequency": 0,
                  "handler": "geoip",
                  "override_country_code": "",
                  "reload_frequency": 0,
                  "trust_header": "X-Real-IP"
                }
              ]
            },
            {
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "body": "Hello from {geoip.country_code}",
                          "handler": "static_response"
                        }
                      ]
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "REDACTED"
                  ]
                }
              ],
              "terminal": true
            }
          ]
        },
        "srv1": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "handle": [
                {
                  "handler": "reverse_proxy",
                  "headers": {
                    "request": {
                      "set": {
                        "Country": [
                          "{geoip.country_code}"
                        ]
                      }
                    },
                    "response": {
                      "set": {
                        "Country": [
                          "{geoip.country_code}"
                        ]
                      }
                    }
                  },
                  "transport": {
                    "protocol": "http",
                    "tls": {}
                  },
                  "upstreams": [
                    {
                      "dial": "REDACTED"
                    }
                  ]
                }
              ]
            }
          ],
          "tls_connection_policies": [
            {}
          ]
        }
      }
    },
    "tls": {
      "automation": {
        "on_demand": {
          "ask": "REDACTED"
        },
        "policies": [
          {
            "issuers": [],
            "on_demand": true
          }
        ]
      }
    }
  }
}

3. The problem I’m having:

I’ve set up the GeoIP plugin (GitHub - shift72/caddy-geo-ip: Implements a Geo IP Lookup using the Maxmind IP Database) via the caddy download page and it works correctly.

I’m unable to add the country code to the header, it outputs the placeholder {geoip.country_code} literally.

4. Error messages and/or full log output:

NA

5. What I already tried:

I’ve read the docs extensively and part of the codebase and have gone through multiple posts about this

6. Links to relevant resources:

Where are you adding it to the header?

1 Like

@matt, thanks for the follow-up. I’ve updated my Caddy Config

You’re talking about the server on :443, right? There’s no geoip handler configured so the placeholder never gets set.

1 Like

I just confirmed this :cry: Kept me up for a full night.

Thank you for all you do @matt

1 Like

Thanks for the help @matt,

It worked great for a moment and then it suddenly stopped loading the maxmind database and displayed “–” instead of “US”. I’ve added the log below:

Aug 23 23:08:27 tapkit caddy[30373]: {"level":"info","ts":1661296107.0255842,"logger":"http.handlers.geoip","msg":"no geo database available"}
Aug 23 23:08:27 tapkit caddy[30373]: {"level":"info","ts":1661296107.0262136,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc0000f4310"}
Aug 23 23:08:27 tapkit caddy[30373]: {"level":"info","ts":1661296107.0266235,"logger":"tls.cache.maintenance","msg":"stopped background certificate maintenance","cache":"0xc00014b880"}
Aug 23 23:08:27 tapkit caddy[30373]: {"level":"info","ts":1661296107.0269692,"msg":"autosaved config (load with --resume flag)","file":"/var/lib/caddy/.config/caddy/autosave.json"}
Aug 23 23:08:27 tapkit caddy[30373]: {"level":"info","ts":1661296107.0276082,"logger":"admin.api","msg":"load complete"}
Aug 23 23:08:27 tapkit caddy[30373]: {"level":"info","ts":1661296107.0300202,"logger":"admin","msg":"stopped previous server","address":"tcp/0.0.0.0:2025"}
Aug 23 23:08:30 tapkit caddy[30373]: {"level":"warn","ts":1661296110.2886603,"logger":"http.handlers.geoip","msg":"no database loaded, skipping geoip lookup"}
Aug 23 23:08:30 tapkit caddy[30373]: {"level":"warn","ts":1661296110.70963,"logger":"http.handlers.geoip","msg":"no database loaded, skipping geoip lookup"}
Aug 23 23:08:50 tapkit caddy[30373]: {"level":"warn","ts":1661296130.8657374,"logger":"http.handlers.geoip","msg":"no database loaded, skipping geoip lookup"}
Aug 23 23:08:51 tapkit caddy[30373]: {"level":"warn","ts":1661296131.2896855,"logger":"http.handlers.geoip","msg":"no database loaded, skipping geoip lookup"}
1 Like

I would ask the author of the plugin about that, I’m not sure. (File an issue in the geoip repo)

Thanks @matt, I can see the issue has already been raised 3 days ago and it’s pending. Is there any alternative you can recommend

3 days isn’t much time in the open source world. To speed things up, consider contributing a patch or forking it with a patch if the owner doesn’t have time to review it. Some maintainers will also prioritize things with an invoice or a sponsorship.

Totally understand. I’m currently learning Go so I can build custom plugins for Caddy and help maintain existing plugins.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.