所謂四層就是基于IP+端口的負(fù)載均衡,通過虛擬IP+端口接收請(qǐng)求,然后再分配到真實(shí)的服務(wù)器;七層通過虛擬的URL或主機(jī)名接收請(qǐng)求,然后再分配到真實(shí)的服務(wù)器七層就是基于URL等應(yīng)用層信息的負(fù)載均衡。
七層負(fù)載:
[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;
}
}
四層負(fù)載:
[root@linux-node1 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
stream { #類似于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;
}
}