当前位置: 首页> 健康> 知识 > Linux——nginx 负载均衡

Linux——nginx 负载均衡

时间:2025/7/17 15:35:18来源:https://blog.csdn.net/Xinan_____/article/details/141627225 浏览次数:0次

常规的web服务器一般提供对于静态资源的访问,比如说:图片、web样式

网站提供的大部分交互功能都需要web编程语言的支持,而web服务对于程序的调用,不管编译型语言还是解释型语言,web服务同将对于应用程序的调用递交给通用网关接口(CGI)。CGI 服务完成对于程序的调用和运行,并将运行结构通过CGI接口返回给web服务,由web服务生成响应报文。

此时在web服务的领域内,引入了LAMP等较为知名的web架构,使web页面提供应用服务成为一种可能,

随着web网站的发展,基本的web服务器容易出现性能瓶颈、响应缓慢等问题,为了解决这些问题,人们尝试通过一些技术手段解决这个问题,这一类技术都属于负载均衡技术,常用于进行负载均衡的服务有:HAProxy、Nginx、lvs等。除此之外,还可以结合dns负载均衡等技术,即一个域名映射多个IP地址等方式。

一、nginx 负载均衡实验

nginx 负载均衡实验过程整理如下:

[root@bogon ~]# systemctl disable --now httpd			// 确保80端口未被httpd 占用
[root@bogon ~]# systemctl start nginx.service 			// 启动nginx 服务
[root@bogon ~]# ps -elf | grep nginx 	
1 S root        5825       1  0  80   0 -  2875 sigsus 14:47 ?        00:00:00 nginx: master process /usr/sbin/nginx
5 S nginx       5826    5825  0  80   0 -  3964 ep_pol 14:47 ?        00:00:00 nginx: worker process
5 S nginx       5827    5825  0  80   0 -  3964 ep_pol 14:47 ?        00:00:00 nginx: worker process
0 S root        5833    2362  0  80   0 - 55417 pipe_r 14:47 pts/0    00:00:00 grep --color=auto nginx
// 恢复成默认的主配置文件,如果之前没有备份,可以使用在/etc/nginx/nginx.default 来恢复,略有不同,影响不大
[root@bogon ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf_$(date +%s)		//备份当前配置文件
[root@bogon ~]# cp /etc/nginx/nginx.conf_1724661958 /etc/nginx/nginx.conf  // 恢复默认配置0
cp: overwrite '/etc/nginx/nginx.conf'? y
[root@bogon ~]# vim /etc/nginx/nginx.conf			// 暂时无需修改,额外的配置放在conf.d 目录下
[root@bogon ~]# ls /etc/nginx/conf.d/
[root@bogon ~]# vim /etc/nginx/conf.d/real_web.conf   // 这是一个新文件,全部内容如下:
server {listen  81;server_name     web1;root    /usr/share/nginx/real1;index index.html;
}
server {listen  82;server_name     web2;root    /usr/share/nginx/real2;index index.html;
}
server {listen  83;server_name     web3;root    /usr/share/nginx/real3;index index.html;
}[root@bogon ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@bogon ~]# systemctl reload nginx.service 
[root@bogon ~]# ss -anput | grep nginx 
tcp   LISTEN 0      511                   0.0.0.0:81            0.0.0.0:*    users:(("nginx",pid=6045,fd=16),("nginx",pid=6044,fd=16),("nginx",pid=5825,fd=16))
tcp   LISTEN 0      511                   0.0.0.0:80            0.0.0.0:*    users:(("nginx",pid=6045,fd=8),("nginx",pid=6044,fd=8),("nginx",pid=5825,fd=8))   
tcp   LISTEN 0      511                   0.0.0.0:83            0.0.0.0:*    users:(("nginx",pid=6045,fd=18),("nginx",pid=6044,fd=18),("nginx",pid=5825,fd=18))
tcp   LISTEN 0      511                   0.0.0.0:82            0.0.0.0:*    users:(("nginx",pid=6045,fd=17),("nginx",pid=6044,fd=17),("nginx",pid=5825,fd=17))
tcp   LISTEN 0      511                      [::]:80               [::]:*    users:(("nginx",pid=6045,fd=9),("nginx",pid=6044,fd=9),("nginx",pid=5825,fd=9))   
[root@bogon ~]# mkdir /usr/share/nginx/real{1,2,3}
[root@bogon ~]# ls /usr/share/nginx/
html  modules  real1  real2  real3
// 定义81-83 端口的index文件
[root@bogon ~]# echo "81 port real webserver " > /usr/share/nginx/real1/index.html
[root@bogon ~]# echo "82222 port real webserver " > /usr/share/nginx/real2/index.html
[root@bogon ~]# echo "33333 port real webserver " > /usr/share/nginx/real3/index.html
[root@bogon ~]# cat /usr/share/nginx/real1/index.html 
81 port real webserver 
[root@bogon ~]# cat /usr/share/nginx/real2/index.html 
82222 port real webserver 
[root@bogon ~]# cat /usr/share/nginx/real3/index.html 
33333 port real webserver 
// 访问测试
[root@bogon ~]# curl http://127.0.0.1:81
81 port real webserver 
[root@bogon ~]# curl http://127.0.0.1:82
82222 port real webserver 
[root@bogon ~]# curl http://127.0.0.1:83
33333 port real webserver // 定义后端工作池
[root@bogon ~]# vim /etc/nginx/conf.d/work_server.conf		// 新文件
[root@bogon ~]# cat  /etc/nginx/conf.d/work_server.conf
upstream realwebs {server 	127.0.0.1:81;server	127.0.0.1:82;server	127.0.0.1:83;
}
[root@bogon ~]# vim /etc/nginx/nginx.conf

[root@bogon ~]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@bogon ~]# systemctl reload nginx 
// 注意这里使用默认的80端口访问,也就是通过80端口访问到了81 82 83 端口的响应内容
[root@bogon ~]# curl http://127.0.0.1
81 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
82222 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
33333 port real webserver 
// 上面展示的负载均衡算法为轮询,在轮询时可以通过权重,让性能更好的节点多处理用户访问
[root@bogon ~]# cat  /etc/nginx/conf.d/work_server.conf
upstream realwebs {server 	127.0.0.1:81  weight=3;   // 设置81端口的权重为3,那么81-83 各个端口实际处理用户访问的比例大概为 3:1:1  未修改的情况下,默认所有节点的权重为1server	127.0.0.1:82;server	127.0.0.1:83;
}[root@bogon ~]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@bogon ~]# systemctl reload nginx.service 
[root@bogon ~]# curl http://127.0.0.1
81 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
82222 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
81 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
33333 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
81 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
81 port real webserver 
[root@bogon ~]# curl http://127.0.0.1
82222 port real webserver 
、
除此之外还可以设置nginx负载均衡的算法包括:
1、最少连接数  least_conn;
2、ip 哈希  	ip_hash;
只需要将对应的算法名称单独写入upstream配置块中即可,例如:[root@bogon ~]# cat /etc/nginx/conf.d/work_server.conf
upstream realwebs {least_conn;server 	127.0.0.1:81 weight=3;server	127.0.0.1:82;server	127.0.0.1:83;
}然而上图示例的架构中,在负载均衡也是就标记为前端节点的位置容易发生类似于

二、在ansible编写实验

关键字:Linux——nginx 负载均衡

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: