当前位置:Gxlcms > PHP教程 > nginx应用:使用nginx进行负载均衡

nginx应用:使用nginx进行负载均衡

时间:2021-07-01 10:21:17 帮助过:6人阅读

这篇文章主要介绍了关于nginx应用:使用nginx进行负载均衡,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

这里写图片描述
nginx一般可以用于七层的负载均衡,这篇文章将介绍一些负载均衡的基本知识以及使用nginx进行负载均衡的简单的例子。

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

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

类别OSI模型层说明
二层负载均衡MAC层根据MAC地址进行响应
三层负载均衡IP层根据IP地址进行响应
四层负载均衡TCP层在IP地址的基础上结合端口号进行响应
七层负载均衡HTTP层在四层的基础上,可继续根据URL/浏览器类别等七层的信息进行进一步的响应

常见软件的支持

软件四层负载均衡七层负载均衡
nginx轻量实现支持http和mail,性能与haproxy相近
haproxy-支持七层负载均衡
LVS支持四层负载均衡,实现较重-
F5硬件实现,成本高-

常见的负载均衡算法

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

负载均衡算法负载均衡算法(E)nginx支持与否说明适用场景
普通轮询Round Robin支持权重相同的轮询适用于外部服务请求和内部服务器都相对均衡的场景
权重轮询Weighted Round Robin支持(weight)可以设定不同权重进行轮询服务器的处理能力不同,或则希望进行流量的控制,比如Canary Release
随机均衡Random-随机分配给服务器外部和内部均非常均衡的场合,或者需要随机的分配的需求较强
权重随机Weighted Random-结合权重随机分配给服务器可结合权重调节随机策略,更好地适应现实中分布状况
响应速度Response Time支持(fair)根据服务器的响应速度进行分配服务器性能和服务器当前运行状况的结合,此种策略能动态的调整状态,避免能者已经不能的情况下仍然被大量分配作业
最少连接Least Connection根据连接的数量进行分配轮询做的是分配任务,由于实际情况中无法控制轮训分配的任务,但是无法确认任务完成的速度,会导致反映真实服务器负荷的连接数产生不同,适合于长时间提供长连接服务的业务,比如网上的客服的WebSocket的实现,或者FTP/SFTP等服务。
DNS响应Flash DNS-根据最快返回的DNS解析结果来继续请求服务,忽略其他DNS返回的IP地址适用于具有全局负载均衡的情况下,比如CDN

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

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

负载均衡算法负载均衡算法(E)nginx支持与否说明适用场景
普通轮询Round Robin支持权重相同的轮询适用于外部服务请求和内部服务器都相对均衡的场景

事前准备

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

  1. [root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7001"ddba0abd24524d270a782c3fab907f6a35c0ce514eec3159357bded09022ee57
  2. [root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7002"95deadd795e19f675891bfcd44e5ea622c95615a95655d1fd346351eca707951
  3. [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

  1. [root@kong ~]# docker run -p 9080:80 --name nginx-lb -d nginx 9d53c7e9a45ef93e7848eb3f4e51c2652a49681e83bda6337c89a3cf2f379c74
  2. [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
  3. [root@kong ~]#

nginx代码段

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

  1. http {
  2. upstream nginx_lb { server 192.168.163.117:7001; server 192.168.163.117:7002;
  3. }server {
  4. listen 80;
  5. server_name www.liumiao.cn 192.168.163.117;
  6. location / {
  7. proxy_pass http://nginx_lb;
  8. }
  9. }

修改default.conf的方法

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

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

修改前

  1. # cat default.confserver {
  2. listen 80;
  3. server_name localhost; #charset koi8-r;
  4. #access_log /var/log/nginx/host.access.log main;
  5. location / {
  6. root /usr/share/nginx/html; index index.html index.htm;
  7. } #error_page 404 /404.html;
  8. # redirect server error pages to the static page /50x.html
  9. #
  10. error_page 500 502 503 504 /50x.html;
  11. location = /50x.html {
  12. root /usr/share/nginx/html;
  13. } # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  14. #
  15. #location ~ \.php$ {
  16. # proxy_pass http://127.0.0.1;
  17. #}
  18. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  19. #
  20. #location ~ \.php$ {
  21. # root html;
  22. # fastcgi_pass 127.0.0.1:9000;
  23. # fastcgi_index index.php;
  24. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  25. # include fastcgi_params;
  26. #}
  27. # deny access to .htaccess files, if Apache's document root
  28. # concurs with nginx's one
  29. #
  30. #location ~ /\.ht {
  31. # deny all;
  32. #}}#

修改后

  1. # cat default.confupstream nginx_lb { server 192.168.163.117:7001; server 192.168.163.117:7002;
  2. }server {
  3. listen 80;
  4. server_name www.liumiao.cn 192.168.163.117; #charset koi8-r;
  5. #access_log /var/log/nginx/host.access.log main;
  6. location / { #root /usr/share/nginx/html;
  7. #index index.html index.htm;
  8. proxy_pass http://nginx_lb;
  9. } #error_page 404 /404.html;
  10. # redirect server error pages to the static page /50x.html
  11. #
  12. error_page 500 502 503 504 /50x.html;
  13. location = /50x.html {
  14. root /usr/share/nginx/html;
  15. } # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  16. #
  17. #location ~ \.php$ {
  18. # proxy_pass http://127.0.0.1;
  19. #}
  20. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  21. #
  22. #location ~ \.php$ {
  23. # root html;
  24. # fastcgi_pass 127.0.0.1:9000;
  25. # fastcgi_index index.php;
  26. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  27. # include fastcgi_params;
  28. #}
  29. # deny access to .htaccess files, if Apache's document root
  30. # concurs with nginx's one
  31. #
  32. #location ~ /\.ht {
  33. # deny all;
  34. #}}#

重启nginx容器

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

确认结果

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

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

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

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

负载均衡算法负载均衡算法(E)nginx支持与否说明适用场景
权重轮询Weighted Round Robin支持(weight)可以设定不同权重进行轮询服务器的处理能力不同,或则希望进行流量的控制,比如Canary Release

修改default.conf

按照如下修改default.conf

  1. # cp default.conf default.conf.org
  2. # vi default.conf
  3. # diff default.conf default.conf.org
  4. 2,3c2,3
  5. < server 192.168.163.117:7001 weight=100;< server 192.168.163.117:7002 weight=200;
  6. ---> server 192.168.163.117:7001;
  7. > server 192.168.163.117:7002;
  8. #

重启nginx容器

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

确认结果

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

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

相关推荐:

nginx管理配置优化

Nginx反向代理websocket配置实例

以上就是nginx应用:使用nginx进行负载均衡的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行