2

I want to use GeoIP in nginx to serve different pages based on country. I also want to cache everything.

When a user from the US goes to my homepage, I want them to see the US version of the homepage from cache. When a user from the UK goes to my homepage, I want them to see the UK version of the homepage from cache.

After I install the GeoIP module, can I just put the country code in my cache key? Is this the best way to do it for performance, because now the server will be looking up country for each IP. I don't know if that's non-trivial.

I was thinking of getting the browser's userLanguage setting and sending via AJAX, and then caching that. Only a few characters need to be geo-located.

Best way to determine user's locale within browser

Community
  • 1
  • 1
stampede76
  • 1,521
  • 2
  • 20
  • 36

1 Answers1

4

After following a standard tutorial to install the GeoIP module, I just put the country code in my cache key, and it works:

$scheme$request_method$host$request_uri$geoip_country_code

I read that nginx loads the GeoIP database in memory, so performance seems to stay the same.

I found others issues that affect performance more than GeoIP lookups:

  1. 404 errors were hitting the CGI
  2. Cachekey did not include query string. There were way more different query string variations than countries.
  3. Clearing the cache. Before cache was cleared by deleting all files. Now I'm looking at getting new pages with wget, and then copying them over to the cache.
stampede76
  • 1,521
  • 2
  • 20
  • 36
  • If you're using Cloudflare, you can skip all the GeoIP module setup entirely and use the `$http_cf_ipcountry` variable already available. For nginx newbies like me, this goes in your nginx config like so: `proxy_cache_key "$scheme$request_method$host$request_uri$http_cf_ipcountry";` Or you might swap proxy_cache_key for `fastcgi_cache_key` – Ted Avery May 23 '20 at 12:23