# NGINX + Load Balancing + GeoIP

**URL:** <https://www.ruby-forum.com/t/nginx-load-balancing-geoip/203718>\
**Category:** NGINX\
**Created:** [February 25, 2011, 7:28pm UTC](https://www.ruby-forum.com/t/nginx-load-balancing-geoip/203718 "2011-02-25T19:28:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 25, 2011, 7:28pm UTC](https://www.ruby-forum.com/t/nginx-load-balancing-geoip/203718/1 "2011-02-25T19:28:42Z")

</div>

Hi everyone,

Has anyone got ngix + load balancing + geoip working nicely?

I have a nginx server load balancing two webservers and would like to  
add in GeoIP to the mix.

Config :

# My config file.

worker\_processes 1;

events {  
worker\_connections 1024;  
}

http {  
### SET the path to the .dat file used for determining the visitor’s  
country from the IP-address ###  
geoip\_country /var/lib/GeoIP/GeoIP.dat;

```
### SET FASTCGI Variables ###
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream webcluster {
server websvr01:80;
server websvr02:80;

}

```

server {  
location / {  
proxy\_pass [http://webcluster](http://webcluster);  
proxy\_set\_header Host $host;  
}

```
    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }

}

```

}

================================================

I would like to set it up that those in the UK goto certain webservers,  
those in USA goto another set of servers and so on.

Appreciate any help or feedback.

Take care and have a great weekend.

Posted at Nginx Forum:

> **[NGINX + Load Balancing + GeoIP](https://forum.nginx.org/read.php?2,178453,178453#msg-178453)**
>
> Hi everyone, Has anyone got ngix + load balancing + geoip working nicely? I have a nginx server load balancing two webservers and would like to add in GeoIP to the mix. Config : My config file. ===============================

---

<div class="post-metadata">

**Author:** ![tainted](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/45deac/32.png) [@tainted](https://www.ruby-forum.com/u/tainted)\
**Post date:** [March 11, 2011, 3:04am UTC](https://www.ruby-forum.com/t/nginx-load-balancing-geoip/203718/2 "2011-03-11T03:04:15Z")

</div>

I’not master but just trying to help you 🙂  
for rist you must to know All of UK IP Address range before.

This just example only:  
i make file uk\_ip.conf and save on /usr/local/geoip/uk\_ip.conf  
on file uk\_ip.conf put all uk ip address  
192.168.1.0/24 uk;  
172.168.0.0/24 uk;  
and next… uk ip address

One more i want also to add us country ip address  
i make file us\_ip.conf and save on /usr/local/geoip/us\_ip.conf  
1.2.3.0/24 us;  
2.2.3.0/24 us;  
3.2.3.0/24 us;  
and next… us ip address

And this file of nginx.conf you can see:  
This just example only i say you must use your own skill to  
configuration your own server

#nginx Master configuration  
user nobody;  
error\_log logs/error.log;  
#Number of worker you need  
worker\_processes 1;

# How many connections a worker can handle maximum.

events {  
worker\_connections 50000;  
}  
http {  
include mime.types;  
default\_type application/octet-stream;  
sendfile on;  
server\_names\_hash\_max\_size 10000;  
server\_names\_hash\_bucket\_size 1024;  
tcp\_nopush on;  
tcp\_nodelay on;  
keepalive\_timeout 10;  
gzip on;  
gzip\_min\_length 1100;  
gzip\_buffers 4 32k;  
gzip\_types text/plain application/x-javascript text/xml text/css;  
ignore\_invalid\_headers on;  
client\_header\_timeout 3m;  
client\_body\_timeout 3m;  
send\_timeout 3m;

geo $geo {  
default default; // for default server  
include /usr/local/geoip/uk\_ip.conf; // if uk ip address comming  
found  
in this database  
include /usr/local/geoip/us\_ip.conf; // if us ip address comming  
found  
in this database  
}

// Now you make first default server if that ip address comming no one  
in your ip address database country  
upstream default.webserver {  
server [default.server1.com](http://default.server1.com) weight=4;  
server [default.server2.com](http://default.server2.com);  
}

// you can see i’m put also option weight that configuration will send  
80% of the request to [default.server1.com](http://default.server1.com)  
// and another 20% to [default.server2.com](http://default.server2.com)

// now here put server for access from uk country  
upstream uk.webserver {  
server [server1.uk-server.com](http://server1.uk-server.com);  
server [server2.uk-server.com](http://server2.uk-server.com);  
server [server3.uk-server.com](http://server3.uk-server.com);  
}

// And nginx load balancing have more option  
// Like Option: weight, max\_fails, fail\_timeout, down, backup more go to

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

// Example i will using option max\_fails and backup for ip address  
comming from us

upstream us.webserver {  
server [server1.us-server.com](http://server1.us-server.com) max\_fails=3;  
server [server2.us-server.com](http://server2.us-server.com) max\_fails=3;  
server [server3.us-server.com](http://server3.us-server.com) backup;  
}

// that mean will send 50% of the requests for [your-domain.com](http://your-domain.com) to  
[server1.us-server.com](http://server1.us-server.com) and other 50%  
// to [server2.us-server.com](http://server2.us-server.com) if your server1 & [server2.us-server.com](http://server2.us-server.com) both  
fails 3times requests  
// will be send requests to [server3.us-server.com](http://server3.us-server.com) as backup server.

server {  
#access\_log on;  
server\_name your\_domain\_name.com;  
listen 80;

location / {  
client\_max\_body\_size 10m;  
client\_body\_buffer\_size 128k;  
proxy\_send\_timeout 60;  
proxy\_read\_timeout 60;  
proxy\_buffer\_size 16k;  
proxy\_buffers 4 32k;  
proxy\_busy\_buffers\_size 32k;  
proxy\_temp\_file\_write\_size 64k;  
proxy\_connect\_timeout 30s;

```
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://$geo.webserver$request_uri;

    }

```

}

i’m use that configuration for my website but only little different  
methode 🙂  
hope this can help you to make your server loadbalace 🙂

Regards,

GhoHan

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,178453,182168#msg-182168](http://forum.nginx.org/read.php?2,178453,182168#msg-182168)
