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:

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
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 weight=4;
server default.server2.com;
}

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

// now here put server for access from uk country
upstream uk.webserver {
server server1.uk-server.com;
server server2.uk-server.com;
server server3.uk-server.com;
}

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

upstream us.webserver {
server server1.us-server.com max_fails=3;
server server2.us-server.com max_fails=3;
server server3.us-server.com backup;
}

// that mean will send 50% of the requests for your-domain.com to
server1.us-server.com and other 50%
// to server2.us-server.com if your server1 & server2.us-server.com both
fails 3times requests
// will be send requests to 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 :slight_smile:
hope this can help you to make your server loadbalace :slight_smile:

Regards,

GhoHan

Posted at Nginx Forum: