我们当时在使用 NGINX 0.6.x时发现,虽然nginx支持url hash功能,但是当后端real server down后,不能自动hash到其他的real server非常不爽。
感谢吴岷修改了源码
22c22 < --- > ngx_str_t s; 106,110c106,110 < ngx_str_t val; < < if (ngx_http_script_run(r, &val, us->lengths, 0, us->values) == NULL) { < return NGX_ERROR; < } --- > ngx_str_t val; > val.data = r->uri.data; > if (r->args.data) val.len = r->uri.len+1+r->args.len; > else val.len = r->uri.len; > ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "myuri: "%V"", &val); 132c132 < /* iphp->hash = 89; */ --- > //iphp->hash = 89; 136c136 < --- > iphp->s = val; 145a146 > u_char mybuff[1024*100]; 150c151 < --- > u_char bFirst = 1; 172a174 > 189a192 > ngx_log_error(NGX_LOG_ERR, pc->log, 0, "hash to peer:%s",inet_ntoa(((struct sockaddr_in*)peer->sockaddr)->sin_addr)); 194a198 > ngx_log_error(NGX_LOG_ERR, pc->log, 0,"fail! hash to peer:%s",inet_ntoa(((struct sockaddr_in*)peer->sockaddr)->sin_addr)); 198d201 < 208a212,227 > if ( iphp->s.len >= sizeof(mybuff) ) > {ngx_log_error(NGX_LOG_ERR, pc->log, 0, "too long uri, size of mybuff is %d", sizeof(mybuff)); > hash ++;continue; > } > else > { > if ( bFirst ) > { > bFirst = 0; > ngx_memset(mybuff,'a', sizeof(mybuff)); > ngx_memcpy(mybuff,iphp->s.data, iphp->s.len); > iphp->s.data = mybuff; > } > } > iphp->s.len++; > 210,211c229,230 < hash+=(ngx_uint_t) now; < --- > hash = ngx_http_upstream_hash_crc32(iphp->s.data, iphp->s.len); > ngx_log_error(NGX_LOG_ERR, pc->log, 0, "rehash ");
目前只在0.6.x测试通过,0.7和0.8尚未测试过。