nginx的四层负载均衡和七层负载均衡的区别
所谓四层就是基于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; } }