<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=KOI8-R" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
I wrote a quick php script to amalgamate the ip ranges into larger
regions than countries. It takes large groups of countries and breaks
them into user defined groups (for meš NA, EU, AS). Doing this drops
the line count from 104K to about 33K and after run through the perl
script the conf file is 1.5MB instead of over 3MB. So that's not a bad
savings. I checked a lot of the regions manually to be sure it was
working so I think it's ok. <br>
<br>
I'll post the code here just in case anyone else can use it. Sorry it's
not perl - I learned it a decade ago but never use it so didn't want to
brush up. This works. I just want to set the correct image server for a
visitor so they get faster photos. <br>
<br>
I guess the best thing would be to do a set of GETs from the client to
each server on demand and then choose the image server with best times
- then it adapts real time. Didn't think of that til now...<br>
<br>
Chris :)<br>
<br>
<tt>&lt;?phpš // Combine regions in GeoIP Database<br>
<br>
$regions = array(<br>
ššš 'NA' =&gt; 'US CA MX PR VI BM BO BS DM AR BZ BR CL PN AD AI AG AW
AT BB BA BG KY CO '.<br>
ššš ššš 'CR CU DM EC SV GQ GP GT HT HN JM NR NI PY PE PL RU RO TT TC ',<br>
ššš 'EU' =&gt; 'EU GB DE FR IT ES SE IR NL BE IE IL CH AL AM BY HR CY
CZ DK EE FI GE GI '.<br>
ššš ššš 'GR GL GG HU IS LB LY LI LT LU MC ME MS NO PT RS SK SI TR UA VA
'.<br>
ššš ššš 'ZA GA EG NA NG ZW BJ GH CG MW UG SC TZ TM KE RW TZ SO SR SY TM
AE UZ AF DZ AO AZ BI '.<br>
ššš ššš 'CV CF TD IQ JE LV MR MQ MU MN SA SL CI NE LS SZ MG SL AO BF MU
TG LY SN SD RE CV GQ '.<br>
ššš ššš 'ZM BW CD TN BJ TG BT BW DJ ER ET JO KZ KW KG LB OM QA ',<br>
ššš 'AS' =&gt; 'JP IN AU NZ TH CN HK MY PK KR HK SG BD ID TW PH LK VN
AP AS AQ TO KH '.<br>
ššš ššš 'CK FJ GN LA MO MM NP NC PG PN WS ST'<br>
ššš );<br>
$other = 'NA';<br>
<br>
$geo = fopen('GeoIPCountryWhois.csv', 'r');<br>
$r = $w = 0;<br>
$last = fgetcsv($geo);<br>
$last_region = region($last);<br>
<br>
while($line = fgetcsv($geo))<br>
ššš {<br>
ššš $r++;<br>
ššš if(($region = region($line)) != $last_region)<br>
ššš ššš {<br>
ššš ššš print '"'.join('","', array($last[0], $last[1], $last[2],
$last[3], $last_region, '-'))."\"\n";ššš <br>
ššš ššš $last = $line;<br>
ššš ššš $last_region = $region;<br>
ššš ššš $w++;<br>
ššš ššš }<br>
ššš else<br>
ššš ššš {<br>
ššš ššš $last[1] = $line[1];<br>
ššš ššš $last[3] = $line[3];<br>
ššš ššš }<br>
ššš }<br>
fclose($geo);<br>
//print "$r =&gt; $w\n";<br>
<br>
function region($vars)<br>
{<br>
ššš global $regions, $other;<br>
ššš <br>
ššš $found = false;<br>
ššš foreach($regions as $r =&gt; $codes)<br>
ššš ššš if(strpos($codes, $vars[4]) !== false)<br>
ššš ššš ššš { <br>
ššš ššš ššš $found = $r; <br>
ššš ššš ššš break; <br>
ššš ššš ššš }<br>
ššš return $found ? $found : $other;<br>
}<br>
<br>
?&gt;</tt><br>
<br>
Igor Sysoev wrote:
<blockquote cite="mid:20080816113443.GM87655@rambler-co.ru" type="cite">
  <pre wrap="">On Sat, Aug 16, 2008 at 05:27:47PM +0700, Chris Savery wrote:

  </pre>
  <blockquote type="cite">
    <blockquote type="cite">
      <pre wrap="">All nginx variables are evaluated on demand only, therefore geo variables
are looked up only if they are really used in a request.
      </pre>
    </blockquote>
    <pre wrap="">Ok. Excellent, so if I only include the fastcgi param line for one 
location, say for index.php then it would only evaluate under that 
condition to pass thru to php, like this:

fastcgi_param  COUNTRY      $geo;

Which is easy then...
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Yes. Actually even if you set fastcgi_param on http level, it will eventually
be inherited on all localtions level (unless overridden), but it will execute
only when fastcgi_pass directive will start to work.


  </pre>
</blockquote>
<br>
</body>
</html>