nginx是著名的非专职全七层负载均衡器,在用惯了四层LVS后,终于碰上了麻烦:LVS后端的4台RS磁盘都较小(20G),跑不到一天就塞满了东西;而根据预估,实际上一天时间该节点也就只有20G的文件增长。很显然,因为lvs转发的轮询算法,导致RS重复缓存了相同的文件。
针对这个情况,可以有两个办法(我只想到两个,欢迎大家补充):
那么,就开始试试nginx的url_hash负载均衡吧:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.01.tar.gz
tar zxvf pcre-8.01.tar.gz
wget http://wiki.nginx.org/images/7/78/Nginx_upstream_hash-0.3.tar.gz
tar zxvf Nginx_upstream_hash-0.3.tar.gz
wget http://sysoev.ru/nginx/nginx-0.7.65.tar.gz
tar zxvf nginx-0.7.65.tar.gz
vi nginx-0.7.65/src/http/ngx_http_upstream.h ```c struct ngx_http_upstream_srv_conf_s { ngx_http_upstream_peer_t peer; void **srv_conf;
ngx_array_t servers; / ngx_http_upstream_server_t */
+ngx_array_t *values; +ngx_array_t *lengths; +ngx_uint_t retries;
ngx_uint_t flags; ngx_str_t host; u_char *file_name; ngx_uint_t line; in_port_t port; in_port_t default_port; };
为了安全,可以修改一下nginx的version信息:vi nginx-0.7.65/src/core/nginx.h
```c
#define NGINX_VERSION "2.6.STABLE21"
#define NGINX_VER "squid/" NGINX_VERSION
vi nginx-0.7.65/src/http/ngx_http_header_filter_module.c
static char ngx_http_server_string[] = "Server: squid/2.6.STABLE21" CRLF;
vi nginx-0.7.65/src/http/ngx_http_special_response.c
static u_char ngx_http_error_tail[] =
"<hr><center>squid/2.6.STABLE21</center>" CRLF
"</body>" CRLF
"</html>" CRLF
cd pcre-8.01
./configure --prefix=/usr
make && make install
cd nginx-0.7.65
./configure --prefix=/home/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --without-http_rewrite_module --add-module=/tmp/nginx_upstream_hash-0.3
vi auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"
make && make install
这样就安装完成了。
2、配置文件
upstream images6.static.com {
server 11.11.11.11:80;
server 11.11.21.12:80;
server 11.11.21.13:80;
server 11.11.21.14:80;
hash $request_uri;
}
server {
listen 80;
server_name images6.static.com;
access_log /dev/null main;
location / {
proxy_pass http://images6.static.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
以上配置的问题:
解决办法:
最后的根本性问题:
对nginx下的RS集群进行增减操作,是否会对hash表产生影响?nginx_upstream_hash目录中的CHANGES有如下三条:
Changes with upstream_hash 0.3 06 Aug 2008
*) Bugfix: infinite loop when retrying after a 404 and the "not_found" flag of *_next_upstream was set.
*) Change: no more "hash_method" directive. Hash method is always CRC-32.
*) Change: failover strategy is compatible with PECL Memcache.
nginx的wiki上,关于hash_again的doc这么写到:
Number of times to rehash the value and choose a different server if the backend connection fails. Increase this number to provide high availability.
关于PECL Memcache,请参考下列链接:
http://www.surfchen.org/archives/348 http://tech.idv2.com/2008/07/24/memcached-004/
尤其是第二个链接,其中关于rehash的解释,很好的解释了为什么大家都不推荐使用hash_again标签。 由此可知,upstream_hash模块,使用的是余数计算standard+CRC32方式,10+2的存活率是17%,3+1的存活率是23%! 而存活率最高的是consistent+CRC32方式,存活率是n/(n+m)*100%,10+2是83%,3+1是75%。
nginx的wiki中,还有另一个3rd模块upstream_consistent_hash,下回可以试试; 网上还有针对upstream_hash模块的补丁http://www.sanotes.net/wp-content/uploads/2009/06/nginx_upstream_hash.pdf,好模块就是有人研究呀~~