Nginx負載均衡策略 - ip_hash

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章