Search code examples
nginxg-wan

Replace url in html files on load balance basing on geolocation?


I have a HTTP web server providing static html pages.

Within the page, it loads images & css from a fixed domain like:

<img src="http://assets.mysite.com/1.jpg" />

Actually there are several different domains serving the same files. For example.

  • assets-us.mysite.com
  • assets-eu.mysite.com
  • assets-asia.mysite.com

I wanna the load balance to replace the domain "assets.mysite.com" to others according to the visitor's geolocation.

For example, when I access the same url from Europe, the html I get is:

<img src="http://assets-eu.mysite.com/1.jpg" />

When I access the same url from Japan, the html I get is

<img src="http://assets-asia.mysite.com/1.jpg" />

I prefer to NGINX(or G-WAN). Is it possible with only some configuration or script setup for the load balance to achieve this? how is the performance affected by this replacement?


Solution

  • If your goal is to perform as well as possible then you should do geo-ip load-balancing at the DNS request - users are redirected prior to querying the Web server. CDNs work this way.

    But if you can't do that and want to manage the load-balancing from the Web server then the best way to scale is to use an AS (the networks used by ISPs) lookup table to find in which regions users are located.

    Doing this as opposed to searching IP addresses, will immensely reduce the database size, and therefore speed-up operations. IP databases offer more details but are much larger.

    For G-WAN, you would write a connection handler or a content-type handler if you want to implement a different logic for different MIME-types (the latter might also ease development as you won't have to parse the request to find the resource type).

    If the database is stored locally (preferably in RAM), G-WAN C/C++/C# scripts, if properly implemented, won't increase the latency in a noticeable manner.