use the http_perl_module to development a simple url hash module

Igor Sysoev is at rambler-co.ru
Tue Dec 25 13:26:17 MSK 2007


On Tue, Dec 25, 2007 at 02:14:44PM +0800, ?????? wrote:

>         perl_set $backend '
>                 sub {
>                         use Digest::MD5 (md5_hex);
>                         my $r = shift;
>                         my $uri = $r->uri;
>                         @arr = qw(192.168.1.2 192.168.1.3 192.168.1.4
> 192.168.1.5);
>                         $arr_num = $#arr + 1;
>                         $url = substr(md5_hex($uri), -8);
>                         $backend = $arr[hex($url) % $arr_num];
>                         return $backend;
>                 }
>         ';
> 
> location / {
>         proxy_pass http://$backend$request_uri;
> }

The better:

       upstream  b1 {
            server   192.168.1.1;
            server   192.168.1.2  backup;
            server   192.168.1.3  backup;
            server   192.168.1.4  backup;
       }

       upstream  b2 {
            server   192.168.1.1  backup;
            server   192.168.1.2;
            server   192.168.1.3  backup;
            server   192.168.1.4  backup;
       }

       perl_set $backend {
            ...
            @arr = qw(b1 b2 b3 b4);


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list