NGINX + Load Balancing + GeoIP

i-comers nginx-forum at nginx.us
Fri Mar 11 05:01:19 MSK 2011


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 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 :)
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




More information about the nginx mailing list