目的:降低 后端大文件服务器由于新增和故障调整的影响
通过nginx的perl_module来实现302,根据请求的哈希跳转crc32校验
工作环境: perl版本在>=5.10.0 nginx版本 >==0.8.33 二点必要
rewrite.pm 配置
- package rewrite;
- use nginx;
- use Hash::ConsistentHash;
- use String::CRC32;
- sub handler {
- my $r = shift;
- my $uri = $r->uri;
- my $domain = $r->header_in(host);
- my @crr = qw( 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.0.5 );
- my $chash = Hash::ConsistentHash->new(
- buckets => [@crr],
- hash_func => \&crc32
- );
- my $url = $chash->get_bucket($uri);
- $url = "http://".$url."/".$domain.$uri;
- $r->header_out(Location => $url);
- $r->status(302);
- $r->send_http_header;
- return OK;
- }
- 1;
- __END__
nginx server 配置
- server {
- listen 80 ;
- server_name
- root /tmp/hello;
- location ~ \.exe {
- perl rewrite::handler;
- }
- }
nginx conf配置(加2行)
- perl_modules perl/lib;
- perl_require rewrite.pm;
Hash::ConsistentHash 下载地址
String::CRC32下载地址