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

怎么使用nginx进行负载均衡

Jun 03, 2023 am 08:19 AM
nginx

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

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

怎么使用nginx进行负载均衡

常见软件的支持

怎么使用nginx进行负载均衡

常见的负载均衡算法

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

怎么使用nginx进行负载均衡

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

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

怎么使用nginx进行负载均衡

事前准备

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

1

2

3

4

5

6

7

8

9

10

11

[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

1

2

3

4

5

[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中

1

2

3

4

5

6

7

8

9

10

11

12

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,使用如下方式即可

1

2

3

4

5

[root@kong ~]# docker exec -it nginx-lb sh

# apt-get update

...省略

# apt-get install vim

...省略

登录后复制

修改前

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

# 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;

  #}

}

#

登录后复制

修改后

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

# 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容器

1

2

3

[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

1

2

3

4

5

6

7

8

9

10

# 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容器

1

2

3

[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

热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

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

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24
nginx在windows中怎么配置 nginx在windows中怎么配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

docker怎么启动容器 docker怎么启动容器 Apr 15, 2025 pm 12:27 PM

Docker 容器启动步骤:拉取容器镜像:运行 "docker pull [镜像名称]"。创建容器:使用 "docker create [选项] [镜像名称] [命令和参数]"。启动容器:执行 "docker start [容器名称或 ID]"。检查容器状态:通过 "docker ps" 验证容器是否正在运行。

docker容器名称怎么查 docker容器名称怎么查 Apr 15, 2025 pm 12:21 PM

可以通过以下步骤查询 Docker 容器名称:列出所有容器(docker ps)。筛选容器列表(使用 grep 命令)。获取容器名称(位于 "NAMES" 列中)。

怎么查看nginx是否启动 怎么查看nginx是否启动 Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

docker怎么创建容器 docker怎么创建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中创建容器: 1. 拉取镜像: docker pull [镜像名] 2. 创建容器: docker run [选项] [镜像名] [命令] 3. 启动容器: docker start [容器名]

nginx怎么查版本 nginx怎么查版本 Apr 14, 2025 am 11:57 AM

可以查询 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打开 Nginx 错误页,查看页面的标题。

nginx怎么配置云服务器域名 nginx怎么配置云服务器域名 Apr 14, 2025 pm 12:18 PM

在云服务器上配置 Nginx 域名的方法:创建 A 记录,指向云服务器的公共 IP 地址。在 Nginx 配置文件中添加虚拟主机块,指定侦听端口、域名和网站根目录。重启 Nginx 以应用更改。访问域名测试配置。其他注意事项:安装 SSL 证书启用 HTTPS、确保防火墙允许 80 端口流量、等待 DNS 解析生效。

nginx服务器挂了怎么办 nginx服务器挂了怎么办 Apr 14, 2025 am 11:42 AM

当 Nginx 服务器宕机时,可执行以下故障排除步骤:检查 nginx 进程是否正在运行。查看错误日志以获取错误消息。检查 nginx 配置语法正确性。确保 nginx 具有访问文件所需的权限。检查文件描述符打开限制。确认 nginx 正在侦听正确的端口。添加防火墙规则以允许nginx流量。检查反向代理设置,包括后端服务器可用性。如需进一步帮助,请联系技术支持。

See all articles