所謂四層就是基於IP+端口的負載均衡,通過虛擬IP+端口接收請求,然后再分配到真實的服務器;
[root@linux-node1 conf]# vim nginx.conf
worker_processes 1; events { worker_connections 1024; } #類似於7層的http段 upstream ssh_proxy { hash $remote_addr consistent; server 192.168.56.2:22; server 192.168.56.3:22; }
server { listen 2222; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass ssh_proxy; }
}
七層通過虛擬的URL或主機名接收請求,然后再分配到真實的服務器七層就是基於URL等應用層信息的負載均衡。
七層負載:
[root@www ~]# cat /etc/nginx/conf.d/test.conf upstream phpserver { server192.168.2.3; server192.168.2.4; } upstream htmlserver { server192.168.2.1; server192.168.2.2; }
[root@www ~]# vim /etc/nginx/nginx.conf location / { root /usr/share/nginx/html; index index.html index.htm; if ($request_uri ~*\.html$){ proxy_pass http://htmlserver; } if ($request_uri~* \.php$){ proxy_pass http://phpserver; } }