星外飞客

nginx中做负载均衡的时候可以使用ip_hash和url_hash,其中url_hash为第三方模块。

1、ip_hash

upstream backend {
	ip_hash;
	server 10.0.2.1:80;
	server 10.0.2.2:80;
}

server {
	location / {
		proxy_pass http://backend;
	}
}

根据客户端IP进行负载均衡,针对同一个C类地址段中的客户端选择到后端同一个源服务器,可避免session引起的问题。

原文如下:

This directive causes requests to be distributed between upstreams based on the IP-address of the client.
The key for the hash is the class-C network address of the client. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server.

参考:http://wiki.nginx.org/NginxHttpUpstreamModule

2、url_hash(第三方)

upstream backend {
	server 10.0.2.1:80;
	server 10.0.2.2:80;
	hash $request_uri;
}

server {
	location / {
		proxy_pass http://backend;
	}
}

根据url的hash结果来分配请求,使每个url定向到同一个后端服务器。可进一步提供后端缓存服务器(如squid等)效率。
参考:http://wiki.nginx.org/NginxHttpUpstreamRequestHashModule

版权所有,转载请注明出处。
转载自 <a href="http://www.yanghengfei.com/archives/304/" title="在nginx中使用ip_hash和url_hash负载均衡" rel="bookmark">在nginx中使用ip_hash和url_hash负载均衡 | 星外飞客 </a>

我简单说几句

随机推荐

最新评论

无觅相关文章插件,快速提升流量