在nginx中负载均衡主要实现用户访问请求进行调度分配和后端服务器的压力分担。
准备环境:
hostname IP 角色
web 外网IP:192.168.8.30 web服务器
内网IP:172.16.1.30
web02 外网IP:192.168.8.21 web服务器
内网IP:172.16.1.21
nginx-LB 外网IP:192.168.8.11 nginx负载均衡
内网IP:172.16.1.11
在nginx-lb服务器的/etc/nginx/conf.c/l4lb.conf文件添加一下内容:
stream {
upstream web02 {
server 172.16.1.21:22;
}
upstream web {
server 172.16.1.30:22;
}
server {
listen 5555;
proxy_pass web02;
}
server {
listen 6666;
proxy_pass web;
}
}
然后在/etc/nginx/nginx.conf的http模块上添加:
include /etc/nginx/conf.c/*.conf;
实验效果:


四层的负载均衡主要是根据IP和端口号进行转发。这里用ssh协议进行演示,也可以用80端口然后进行访问效果也是一样的,就不再演示了。
负载均衡配置模块说明:




实例:

web、web02的配置文件:

分别在web、web02上mkdir -p /jxj/www创建网页目录。
web服务器的index.html文件内容为

web02服务器的index.html文件内容为:

使用curl命令查看实验效果:

到此,实验就成功了。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3162206/ ,如需转载,请注明出处,否则将追究法律责任。