服务器 > 网络 > https

nginx负载均衡配置方式

16人参与 2025-06-05 https

nginx实现负载均衡的方式

每个请求按时间顺序逐一分配到不同的后端服务器,后端服务器宕机时,能被自动删除且请求不会受影响

指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的情况,权重越高被访问的概率就越大

每个请求被访问ip的hash结果分配,这样每个访客访客固定访问一个后端服务器

动态根据后端服务器处理请求的响应时间来进行负载分配,响应时间短的优先分配,时间长的 分配的请求会减少,nginx服务默认不支持这个算法,需要安装upstream_fair模块

根据访问的upl计算出的hash结果来分配请求,每个请求会指向固定的服务器,常用于nginx作为静态资源服务器的场景,可以提高缓存效率,nginx服务默认不支持这个算法,需要安装nginx的hash软件包

环境配置

需要三台虚拟机,都要配置好nginx

机器名服务器ip用途
nginx (主)192.168.95.137负载均衡服务器
服务器1192.168.95.138后端服务器
服务器2192.168.95.139后端服务器

1.设置防火墙 三台虚拟机都要设置

firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld.service
getenforce #查看selinux状态

轮询模式负载均衡

打开nginx(负责负载均衡)主虚拟机

编辑配置文件:

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak #备份一个配置文件,防止配置错误可重新配置

vim nginx.conf

在http{}模块里添加以下内容

upstream webserver {
server 192.168.95.138:80; #服务器1
server 192.168.95.139:80; #服务器2
}
server{
listen 80;
server_name 192.168.95.137;
location / {
index index.html index.htm;
proxy_pass http://webserver; #【webserver】和upstream 【webserver】名字一致
}
}

检查语法并重启

/usr/local/nginx/sbin/nginx -t #检查语法
/usr/local/nginx/sbin/nginx -s reload #重启

配置服务器1

cd /usr/local/nginx/html/
cp index.html index.html.bak
vim index.html #清空里面的所有配置

添加下面的语句: 

<h>welcome to server1<h>

保存退出 

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

浏览器访问:http://ip

配置服务器2

cd /usr/local/nginx/html/
cp index.html index.html.bak
vim index.html #清空里面的所有配置

添加下面的语句: 

<h>welcome to server2<h>

保存退出 

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

浏览器访问:http://ip

测试负载均衡:

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server1 <h>
[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>
[root@localhost conf]# curl 192.168.95.137
<h>welcome to server1 <h>
[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

然后浏览器进行访问

权重模式负载均衡

打开nginx(负责负载均衡)主虚拟机

编辑配置文件:

在http{}模块里添加以下内容

upstream webserver {
server 192.168.95.138:80 weight=3; #在原来的基础上添加weight=自定义权重值
server 192.168.95.139:80 weight=7;
}
server{
listen 80;
server_name 192.168.95.137;
location / {
index index.html index.htm;
proxy_pass http://webserver;
}
}

保存退出

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

测试负载均衡

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server1 <h>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server1 <h>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server1 <h>

[root@localhost conf]# curl 192.168.95.137
<h>welcome to server2<h/>

从上面结果可以看出,一共测试访问10次用户请求,分流至server2服务器上的有7次,分流至server1服务器上的有3次,表明权重配置生效

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。 

(0)

您想发表意见!!点此发布评论

推荐阅读

如何搭建并配置HTTPD文件服务及访问权限控制

06-05

Tomcat端口仅允许本地访问设置的两种方法

05-30

Tomcat端口仅允许本地访问设置的两种实现方法

05-30

在Ubuntu中设置IP地址的几种方式

05-30

Nginx报错“Too many open files”的问题解决

05-30

Nginx设置https和http同时使用同一个端口访问

05-30

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论