NGINX + Load Balancing + GeoIP

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;
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:,178453,178453#msg-178453

I’not master but just trying to help you :slight_smile:
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 uk; 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 us; us; 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
in this database
include /usr/local/geoip/us_ip.conf; // if us ip address comming
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 weight=4;

// you can see i’m put also option weight that configuration will send
80% of the request to
// and another 20% to

// now here put server for access from uk country
upstream uk.webserver {

// And nginx load balancing have more option
// Like Option: weight, max_fails, fail_timeout, down, backup more go to
// Example i will using option max_fails and backup for ip address
comming from us

upstream us.webserver {
server max_fails=3;
server max_fails=3;
server backup;

// that mean will send 50% of the requests for to and other 50%
// to if your server1 & both
fails 3times requests
// will be send requests to as backup server.

server {
#access_log on;
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 :slight_smile:
hope this can help you to make your server loadbalace :slight_smile:



Posted at Nginx Forum:,178453,182168#msg-182168

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs