当前位置: 首页 > news >正文

webshell实践,在nginx上实现负载均衡

1、配置多台虚拟机,用作服务器

在不同的虚拟机上安装httpd服务

我采用了三台虚拟机进行服务器设置:192.168.240.11、192.168.240.12、192.168.240.13

[root@node0-8 /]# yum install httpd -y  #使用yum安装httpd服务#开启httpd服务
[root@node0-8 /]# systemctl start httpd
[root@node0-8 /]# systemctl status httpd
● httpd.service - The Apache HTTP ServerLoaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)Drop-In: /usr/lib/systemd/system/httpd.service.d└─php-fpm.confActive: active (running) since Mon 2022-12-08 23:38:30 CST; 8 months 4 days ago

在服务器上创建标识文件

[root@node0-8 /]# vim /etc/httpd/conf/httpd.conf  #查看httpd的配置文件所指向的网站主页文件路径
</Directory># Further relax access to the default document root:
<Directory "/var/www/html">#在/var/www/html路径下创建index.html文件,文件内容标识不同的服务器端
#在虚拟机11上创建
[root@server79 /]# vim /var/www/html/index.html
这是主机11
#在虚拟机12上创建
[root@node0-8 /]# vim /var/www/html/index.html
这是主机12
#在虚拟机13上创建
[root@node1-79 /]# vim /var/www/html/index.html
这是主机13

使用Windows主机浏览器分别访问服务器


[root@node1-79 /]# systemctl stop firewalld  #关闭服务器主机的防火墙
[root@node1-79 /]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)

 

  

 

在新的虚拟机上配置nginx实现负载均衡

首先要安装nginx,随后再配置负载均衡

[root@localhost ~]# nginx -V  #查看nginx所有配置文件的路径#在添加所要代理的服务器前先测试与服务器之间是否信息互联
[root@localhost /]# ping 192.168.32.137
PING 192.168.32.137 (192.168.32.137) 56(84) bytes of data.
64 bytes from 192.168.32.137: icmp_seq=1 ttl=64 time=0.553 ms#修改nginx的配置文件配置服务器代理
#在nginx的nginx.conf文件中添加以下内容
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#在http模块中插入upstream servers {                          #upstream XXX(自定义命名)server 192.168.240.11;              #server 后面的地址为被代理的服务器的地址server 192.168.240.12;server 192.168.240.13;}
#在同一http模块下的server中的location修改为以下内容
location / {                              #/后面自定义命名路径名proxy_pass http://servers;      #这里的//后面的XXX必须与上端的upstream后面的相同}#配置修改完毕后重加载nginx配置
[root@localhost sbin]# ./nginx -s reload#关闭防火墙
[root@localhost sbin]# systemctl stop firewalld
[root@localhost sbin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)Active: inactive (dead) since Tue 2023-08-12 15:08:34 CST; 7 days agoDocs: man:firewalld(1)

 在浏览器上测试

 

 修改代理服务器的权重

 

#在原本的代理地址后面加上权重
[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.confupstream servers {server 192.168.32.137 weight=1;server 192.168.32.138 weight=5;server 192.168.32.147 weight=2;}[root@localhost sbin]# ./nginx -s reload  #重新加载nginx的配置

http://www.lryc.cn/news/130801.html

相关文章:

  • LVS+Keepalived集群
  • Java的网络编程
  • kafka配置远程连接
  • css实现渐变色border
  • 管理 IBM Spectrum LSF
  • 117页数字化转型与产业互联网发展趋势及机会分析报告PPT
  • 【JavaWeb】实训的长篇笔记(上)
  • 如何使用Docker安装AWVS?
  • vue命名规范
  • 第05天 SpringBoot自动配置原理
  • AlphaZero能否从围棋和国际象棋飞跃到量子计算?
  • 进程切换
  • ES踩坑记录之UNASSIGNED分片无法恢复
  • ubuntu更换国内apt源
  • OpenCV-Python中的图像处理-视频分析
  • STM32 CubeMX (第四步Freertos内存管理和CPU使用率)
  • 题解 | #1012.Equalize the Array# 2023杭电暑期多校10
  • UE4/5C++多线程插件制作(二十一、使用)
  • 【C#】关于?的用法
  • linux——mysql的高可用MHA
  • 【学习日记】【FreeRTOS】空闲任务与阻塞延时
  • 衣服材质等整理(时常更新)
  • 电子商务环境下旅游价值链
  • spring源码分析bean的生命周期(下)
  • 完美解决Github提交PR后报错:File is not gofumpt-ed (gofumpt)
  • pytorch3d成功安装
  • 【vue3】同个页面引入多个图表组件实现自适应的方法
  • 一文了解汽车芯片的分类及用途介绍
  • Linux0.11内核源码解析-truncate.c
  • LED驱动型IC芯片的原理介绍