搜索
首页运维Nginx怎么使用nginx进行负载均衡

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

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

怎么使用nginx进行负载均衡

常见软件的支持

怎么使用nginx进行负载均衡

常见的负载均衡算法

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

怎么使用nginx进行负载均衡

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

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

怎么使用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:7001
hello, service :user service 1: 7001
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7002
hello, 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-lb
9d53c7e9a45e    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.conf
server {
  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.conf
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;
  #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-lb
nginx-lb
[root@kong ~]#

确认结果

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

[root@kong ~]# curl
hello, service :user service 1: 7001
[root@kong ~]# curl
hello, service :user service 1: 7002
[root@kong ~]# curl
hello, service :user service 1: 7001
[root@kong ~]# curl
hello, service :user service 1: 7002
[root@kong ~]#

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

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

怎么使用nginx进行负载均衡

修改default.conf

按照如下修改default.conf

# cp default.conf default.conf.org
# vi default.conf
# diff default.conf default.conf.org
2,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-lb
nginx-lb
[root@kong ~]#

确认结果

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

[root@kong ~]# curl
hello, service :user service 1: 7001
[root@kong ~]# curl
hello, service :user service 1: 7002
[root@kong ~]# curl
hello, service :user service 1: 7002
[root@kong ~]#

以上是怎么使用nginx进行负载均衡的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
NGINX的优点:速度,效率和控制NGINX的优点:速度,效率和控制May 12, 2025 am 12:13 AM

NGINX受欢迎的原因是其在速度、效率和控制方面的优势。1)速度:采用异步、非阻塞处理,支持高并发连接,静态文件服务能力强。2)效率:内存使用低,负载均衡功能强大。3)控制:通过灵活的配置文件管理行为,模块化设计便于扩展。

NGINX与Apache:社区,支持和资源NGINX与Apache:社区,支持和资源May 11, 2025 am 12:19 AM

NGINX和Apache在社区、支持和资源方面的差异如下:1.NGINX的社区虽然规模较小,但活跃度和专业性高,官方支持通过NGINXPlus提供高级功能和专业服务。2.Apache拥有庞大且活跃的社区,官方支持主要通过丰富的文档和社区资源提供。

NGINX单元:应用程序服务器简介NGINX单元:应用程序服务器简介May 10, 2025 am 12:17 AM

NGINXUnit是一个开源的应用服务器,支持多种编程语言和框架,如Python、PHP、Java、Go等。1.它支持动态配置,可以在不重启服务器的情况下调整应用配置。2.NGINXUnit支持多语言应用,简化了多语言环境的管理。3.通过配置文件,可以轻松部署和管理应用,如运行Python和PHP应用。4.它还支持高级配置,如路由和负载均衡,帮助管理和扩展应用。

使用NGINX:优化网站性能和可靠性使用NGINX:优化网站性能和可靠性May 09, 2025 am 12:19 AM

NGINX可通过以下方式提升网站性能和可靠性:1.作为Web服务器处理静态内容;2.作为反向代理服务器转发请求;3.作为负载均衡器分配请求;4.作为缓存服务器减轻后端压力。通过配置优化如启用Gzip压缩和调整连接池,NGINX能显着提高网站性能。

NGINX的目的:服务Web内容等NGINX的目的:服务Web内容等May 08, 2025 am 12:07 AM

nginxserveswebcontentandactsasareverseproxy,loadBalancer和more.1)效率高效的servesstaticContentLikeHtmlandImages.2)itfunctionsasareverseproxybalancer,and andginxenhanceperforfforfforfforfforfforffrenfcaching.4)

NGINX单元:简化应用程序部署NGINX单元:简化应用程序部署May 07, 2025 am 12:08 AM

NGINXUnit通过动态配置和多语言支持简化应用部署。1)动态配置无需重启服务器即可修改。2)支持多种编程语言,如Python、PHP、Java。3)采用异步非阻塞I/O模型,提升高并发处理性能。

NGINX的影响:Web服务器及其他NGINX的影响:Web服务器及其他May 06, 2025 am 12:05 AM

NGINX起初解决C10K问题,现已发展为处理负载均衡、反向代理和API网关的全能选手。1)它以事件驱动和非阻塞架构闻名,适合高并发。2)NGINX可作为HTTP和反向代理服务器,支持IMAP/POP3。3)其工作原理基于事件驱动和异步I/O模型,提升了性能。4)基本用法包括配置虚拟主机和负载均衡,高级用法涉及复杂负载均衡和缓存策略。5)常见错误包括配置语法错误和权限问题,调试技巧包括使用nginx-t命令和stub_status模块。6)性能优化建议包括调整worker参数、使用gzip压缩和

NGINX故障排除:诊断和解决常见错误NGINX故障排除:诊断和解决常见错误May 05, 2025 am 12:09 AM

Nginx常见错误的诊断与解决方法包括:1.查看日志文件,2.调整配置文件,3.优化性能。通过分析日志、调整超时设置和优化缓存及负载均衡,可以有效解决404、502、504等错误,提高网站稳定性和性能。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中