千家信息网

如何使用nginx进行负载均衡

发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,这篇文章主要介绍了如何使用nginx进行负载均衡,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。四层负载均衡 vs 七层负载均衡经常会说
千家信息网最后更新 2025年12月03日如何使用nginx进行负载均衡

这篇文章主要介绍了如何使用nginx进行负载均衡,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

四层负载均衡 vs 七层负载均衡

经常会说七层负载均衡还是四层负载均衡,其实根据ISO的OSI网络模型的所在层的叫法而决定的,nginx因为在使用http协议在应用层进行负载均衡的操作,所以被称为七层负载均衡。而诸如LVS在TCP层进行负载均衡操作的则被称为四层负载均衡。一般来说,有如下层的负载均衡分类:

常见软件的支持

常见的负载均衡算法

负载均衡常见有如下几种算法:

负载均衡演示实例:普通轮询

接下来使用nginx来演示一下如何进行普通轮询:

事前准备

事前在7001/7002两个端口分别启动两个服务,用于显示不同信息,为了演示方便,使用tornado做了一个镜像,通过docker容器启动时传递的参数不同用于显示服务的不同。

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7001"ddba0abd24524d270a782c3fab907f6a35c0ce514eec3159357bded09022ee57[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7002"95deadd795e19f675891bfcd44e5ea622c95615a95655d1fd346351eca707951[root@kong ~]# [root@kong ~]# curl http://192.168.163.117:7001Hello, Service :User Service 1: 7001[root@kong ~]# [root@kong ~]# curl http://192.168.163.117:7002Hello, Service :User Service 1: 7002[root@kong ~]#

启动nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-lb -d nginx 9d53c7e9a45ef93e7848eb3f4e51c2652a49681e83bda6337c89a3cf2f379c74[root@kong ~]# docker ps |grep nginx-lb9d53c7e9a45e    nginx           "nginx -g 'daemon ..."  11 seconds ago   Up 10 seconds    0.0.0.0:9080->80/tcp                         nginx-lb[root@kong ~]#

nginx代码段

准备如下nginx代码段将其添加到nginx的/etc/nginx/conf.d/default.conf中

http {upstream nginx_lb {  server 192.168.163.117:7001;  server 192.168.163.117:7002;}server {  listen    80;  server_name www.liumiao.cn 192.168.163.117;  location / {    proxy_pass http://nginx_lb;  }}

修改default.conf的方法

可以通过在容器中安装vim达到效果,也可以在本地修改然后通过docker cp传入,或者直接sed修改都可。如果在容器中安装vim,使用如下方式即可

[root@kong ~]# docker exec -it nginx-lb sh# apt-get update...省略# apt-get install vim...省略

修改前

# cat default.confserver {  listen    80;  server_name localhost;  #charset koi8-r;  #access_log /var/log/nginx/host.access.log main;  location / {    root  /usr/share/nginx/html;    index index.html index.htm;  }  #error_page 404       /404.html;  # redirect server error pages to the static page /50x.html  #  error_page  500 502 503 504 /50x.html;  location = /50x.html {    root  /usr/share/nginx/html;  }  # proxy the PHP scripts to Apache listening on 127.0.0.1:80  #  #location ~ \.php$ {  #  proxy_pass  http://127.0.0.1;  #}  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  #  #location ~ \.php$ {  #  root      html;  #  fastcgi_pass  127.0.0.1:9000;  #  fastcgi_index index.php;  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  #  include    fastcgi_params;  #}  # deny access to .htaccess files, if Apache's document root  # concurs with nginx's one  #  #location ~ /\.ht {  #  deny all;  #}}#

修改后

# cat default.confupstream nginx_lb {  server 192.168.163.117:7001;  server 192.168.163.117:7002;}server {  listen    80;  server_name www.liumiao.cn 192.168.163.117;  #charset koi8-r;  #access_log /var/log/nginx/host.access.log main;  location / {    #root  /usr/share/nginx/html;    #index index.html index.htm;    proxy_pass http://nginx_lb;  }  #error_page 404       /404.html;  # redirect server error pages to the static page /50x.html  #  error_page  500 502 503 504 /50x.html;  location = /50x.html {    root  /usr/share/nginx/html;  }  # proxy the PHP scripts to Apache listening on 127.0.0.1:80  #  #location ~ \.php$ {  #  proxy_pass  http://127.0.0.1;  #}  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  #  #location ~ \.php$ {  #  root      html;  #  fastcgi_pass  127.0.0.1:9000;  #  fastcgi_index index.php;  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  #  include    fastcgi_params;  #}  # deny access to .htaccess files, if Apache's document root  # concurs with nginx's one  #  #location ~ /\.ht {  #  deny all;  #}}#

重启nginx容器

[root@kong ~]# docker restart nginx-lbnginx-lb[root@kong ~]#

确认结果

可以清晰地看到按照顺序,进行轮询:

[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]#

负载均衡演示实例:权重轮询

而在此基础上,进行权重轮询只需要加上weight即可

修改default.conf

按照如下修改default.conf

# cp default.conf default.conf.org# vi default.conf# diff default.conf default.conf.org2,3c2,3<   server 192.168.163.117:7001 weight=100;<   server 192.168.163.117:7002 weight=200;--->   server 192.168.163.117:7001;>   server 192.168.163.117:7002;#

重启nginx容器

[root@kong ~]# docker restart nginx-lbnginx-lb[root@kong ~]#

确认结果

可以看到轮询结果按照1/3和2/3的比重在进行了:

[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]#

感谢你能够认真阅读完这篇文章,希望小编分享的"如何使用nginx进行负载均衡"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

均衡 容器 篇文章 演示 不同 常见 结果 普通 两个 代码 实例 权重 算法 中安 准备 支持 服务 接下来 一般来说 价值 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 计算机网络技术中职课程 手游幸存者挑战在哪申请服务器 天津联想服务器维修维保费用 云服务器虚拟机可以选的配置 中国木根服务器什么时候上线 贵池区先进软件开发服务解决方案 销售软件开发系统的发票 网络安全威胁观后感 保证网络安全对我们的要求 学编程要学数据库吗 福安市大禹网络技术有限公司 软件开发公司如何接项目 少林闲侠最新服务器 计算机网络技术是中专 北邮移动的连接服务器 数据库插入数据过程中建表 军队网络安全问题及对策 桌面软件开发实训心得 6月1日网络安全法实施 西安彩票网络安全公司 织梦模板怎么打开数据库 网络技术专业方向有哪些 车载网络技术实训报告 计算机网络技术的案例 多次查询数据库效率低 2020软科网络安全 深圳市领域网络技术有限公司 2核2g服务器mc 数据库插入数据过程中建表 服务器急需管理员怎么报名
0