配置基於客戶端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地址進行哈希計算,確保使用同一個服務器響應請求;