配置基于客户端ip_hash的负载均衡
$ vim $NGINX_HOME/conf/nginx.conf
worker_processes auto;
events {
use epoll;
worker_connections 65535;
}
http {
upstream aidan.org{
ip_hash;
server 127.0.0.1:8881;
server 127.0.0.1:8882;
server 127.0.0.1:8883;
}
server {
listen 80;
server_name aidan.org;
location / {
proxy_pass http://aidan.org;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
$ nginx -s reload
测试一下ip_has的效果
[root@localhost ~]# curl aidan.org
hello nginx,I'm 8883 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org
hello nginx,I'm 8883 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org
hello nginx,I'm 8883 Server.nginx host is aidan.org,your realIp is 127.0.0.1
发现都是同一个服务在提供服务
小结
ip哈希负载均衡使用ip_hash
指定定义;
nginx使用客户端的IP地址进行哈希计算,确保使用同一个服务器响应请求;