012.Nginx负载均衡
一 负载均衡概述
1.1 负载均衡介绍
二 Nginx负载均衡
2.1 优点
2.2 主要均衡机制
2.3 负载均衡策略
2.4 负载均衡内置变量
2.5 负载均衡健康检查
2.6 负载均衡状态码
|
状态
|
概述
|
|
max_fails
|
允许请求失败的次数,默认为1。
|
|
max_conns
|
限制最大接受的连接数。
|
|
fail_timeout
|
在经历了max_fails次失败后,暂停服务的时间。
|
|
backup
|
预留的备份机器。
|
|
down
|
表示当前的server暂时不参与负载均衡。
|
三 默认轮询负载均衡
3.1 环境预设
|
主机
|
IP
|
备注
|
|
nginx01
|
172.24.10.21
|
Nginx负载均衡
|
|
nginx02
|
172.24.10.22
|
后端RS 01
|
|
nginx03
|
172.24.10.23
|
后端RS 02
|
|
nginx04
|
172.24.10.24
|
后端RS 03
|
1 [root@nginx02 ~]# mkdir /usr/share/nginx/balanc/
2 [root@nginx02 ~]# echo '<h1>Rs_172.24.10.22</h1>' > /usr/share/nginx/balanc/index.html
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF
2 server {
3 listen 9090;
4 server_name 172.24.10.22;
5 location / {
6 root /usr/share/nginx/balanc;
7 index index.html;
8 access_log /var/log/nginx/rs.access.log main;
9 error_log /var/log/nginx/rs.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
2 [root@nginx02 ~]# nginx -s reload #重载配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/balanc/
2 [root@nginx03 ~]# echo '<h1>Rs_172.24.10.23</h1>' > /usr/share/nginx/balanc/index.html
1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF
2 server {
3 listen 9090;
4 server_name 172.24.10.23;
5 location / {
6 root /usr/share/nginx/balanc/;
7 index index.html;
8 access_log /var/log/nginx/rs.access.log main;
9 error_log /var/log/nginx/rs.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
2 [root@nginx03 ~]# nginx -s reload #重载配置文件
1 [root@nginx04 ~]# mkdir /usr/share/nginx/balanc/
2 [root@nginx04 ~]# echo '<h1>Rs_172.24.10.24</h1>' > /usr/share/nginx/balanc/index.html
1 [root@nginx04 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF
2 server {
3 listen 9090;
4 server_name 172.24.10.24;
5 location / {
6 root /usr/share/nginx/balanc/;
7 index index.html;
8 access_log /var/log/nginx/rs.access.log main;
9 error_log /var/log/nginx/rs.error.log warn;
10 }
11 }
12 EOF
1 [root@nginx04 ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
2 [root@nginx04 ~]# nginx -s reload #重载配置文件
3 [root@client ~]# curl 172.24.10.22:9090
4 <h1>Rs_172.24.10.22</h1>
5 [root@client ~]# curl 172.24.10.23:9090
6 <h1>Rs_172.24.10.23</h1>
7 [root@client ~]# curl 172.24.10.24:9090
8 <h1>Rs_172.24.10.24</h1>
3.2 默认轮询方式配置
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/balance.conf
2 upstream mybalance01 {
3 server 172.24.10.22:9090;
4 server 172.24.10.23:9090;
5 server 172.24.10.24:9090;
6 }
7
8 server {
9 listen 80;
10 server_name balance.linuxds.com;
11 access_log /var/log/nginx/mybalance.access.log main;
12 error_log /var/log/nginx/mybalance.error.log warn;
13 location / {
14 proxy_pass http://mybalance01;
15 index index.html;
16 proxy_redirect off;
17 proxy_set_header Host $host;
18 proxy_set_header X-Real-IP $remote_addr;
19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
20 client_max_body_size 10m; #允许客户端请求的最大单文件字节数
21 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
22 proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
23 proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
24 proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
25 proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
26 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
27 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
28 proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
29 }
30 }
1 [root@balance ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件
2 [root@balance ~]# nginx -s reload #重载配置文件
3.3 确认测试

四 加权轮询负载均衡
4.1 环境预设
4.2 加权轮询方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf
2 upstream mybalance01 {
3 server 172.24.9.11:9090 weight=1 max_fails=1 fail_timeout=2;
4 server 172.24.9.12:9090 weight=8 max_fails=2 fail_timeout=2;
5 server 172.24.9.13:9090 backup; #配置为备份服务器
6 }
7
8 server {
9 listen 80;
10 server_name balance.linuxds.com;
11 access_log /var/log/nginx/mybalance.access.log main;
12 error_log /var/log/nginx/mybalance.error.log warn;
13 location / {
14 proxy_pass http://mybalance01;
15 index index.html;
16 proxy_redirect off;
17 proxy_set_header Host $host;
18 proxy_set_header X-Real-IP $remote_addr;
19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
20 client_max_body_size 10m;
21 client_body_buffer_size 128k;
22 proxy_connect_timeout 300;
23 proxy_send_timeout 300;
24 proxy_read_timeout 300;
25 proxy_buffer_size 4k;
26 proxy_buffers 4 32k;
27 proxy_busy_buffers_size 64k;
28 proxy_temp_file_write_size 64k;
29 }
30 }
4.3 确认测试
五 最小连接负载均衡
5.1 环境预设
5.2 最小连接方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf
2 upstream mybalance01 {
3 least_conn;
4 server 172.24.10.21:9090;
5 server 172.24.10.22:9090;
6 server 172.24.10.23:9090;
7 }
8
9 server {
10 listen 80;
11 server_name balance.linuxds.com;
12 access_log /var/log/nginx/mybalance.access.log main;
13 error_log /var/log/nginx/mybalance.error.log warn;
14 location / {
15 proxy_pass http://mybalance01;
16 index index.html;
17 }
18 }
5.3 确认测试
六 IP hash负载均衡
6.1 环境预设
6.2 ip hash方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf
2 upstream mybalance01 {
3 ip_hash;
4 server 172.24.10.21:9090;
5 server 172.24.10.22:9090;
6 server 172.24.10.23:9090;
7 }
8
9 server {
10 listen 80;
11 server_name balance.linuxds.com;
12 access_log /var/log/nginx/mybalance.access.log main;
13 error_log /var/log/nginx/mybalance.error.log warn;
14 location / {
15 proxy_pass http://mybalance01;
16 index index.html;
17 }
18 }
6.3 确认测试
七 fair负载均衡
7.1 环境预设
7.2 fair方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf
2 upstream mybalance01 {
3 fair;
4 server 172.24.10.21:9090;
5 server 172.24.10.22:9090;
6 server 172.24.10.23:9090;
7 }
8
9 server {
10 listen 80;
11 server_name balance.linuxds.com;
12 access_log /var/log/nginx/mybalance.access.log main;
13 error_log /var/log/nginx/mybalance.error.log warn;
14 location / {
15 proxy_pass http://mybalance01;
16 index index.html;
17 }
18 }
7.3 确认测试
八 url_hash负载均衡
8.1 环境预设
8.2 ip hash方式配置
1 [root@balance ~]# vi /etc/nginx/conf.d/balance.conf
2 upstream mybalance01 {
3 hash $request_uri;
4 hash_method crc32;
5 server 172.24.10.21:9090;
6 server 172.24.10.22:9090;
7 server 172.24.10.23:9090;
8 }
9
10 server {
11 listen 80;
12 server_name balance.linuxds.com;
13 access_log /var/log/nginx/mybalance.access.log main;
14 error_log /var/log/nginx/mybalance.error.log warn;
15 location / {
16 proxy_pass http://mybalance01;
17 index index.html;
18 }
19 }
8.3 确认测试
012.Nginx负载均衡的更多相关文章
- 对比Haproxy和Nginx负载均衡效果
为了对比Hproxy和Nginx负载均衡的效果,分别在测试机上(以下实验都是在单机上测试的,即负载机器和后端机器都在一台机器上)做了这两个负载均衡环境,并各自抓包分析.下面说下这两种负载均衡环境下抓包 ...
- nginx负载均衡集群
nginx负载均衡集群 0.前言:nginx 负载均衡,属于网络7层模型中的应用层,说白了就是一个代理,要用 upstrem 模块实现,代理则用proxy模块 1.可以针对域名做转发,lvs只能针对 ...
- 手把手教你玩转nginx负载均衡(二)----安装虚拟机操作系统
引言 在上一篇,我们组装好了虚拟机的硬件部分,那么现在我们就要把操作系统装上了,既然是服务器,那么安装linux操作系统是个比较好的选择,如果你喜欢的话,安装windows也是没有任何问题的 我这里选 ...
- nginx负载均衡基于ip_hash的session粘帖
nginx负载均衡基于ip_hash的session粘帖 nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除 ...
- Net分布式系统之二:CentOS系统搭建Nginx负载均衡
一.关于CentOS系统介绍 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat ...
- Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用
上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx ...
- 配置nginx负载均衡
配置nginx负载均衡 执行命令:vi /usr/local/nginx/sbin/nginx/conf/nginx.conf 修改为: worker_processes 2; events { ...
- 烂泥:nginx负载均衡
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 今天我们来学习下有关nginx的负载均衡配置.nginx的负载均衡是通过nginx的upstream模块和proxy_pass反向代理来实现的. 说明: ...
- nginx负载均衡集群中的session共享说明
在网站使用nginx+php做负载均衡情况下,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,就会出现很多问题,比如说最常见的登录状态. 下面罗列几种nginx负载均衡 ...
随机推荐
- Linux MySQL分库分表之Mycat
介绍 背景 当表的个数达到了几百千万张表时,众多的业务模块都访问这个数据库,压力会比较大,考虑对其进行分库 当表的数据达到几千万级别,在做很多操作都比较吃力,考虑对其进行分库或分表 数据切分(shar ...
- Nginx 如何自定义变量?
之前的两篇文章 Nginx 变量介绍以及利用 Nginx 变量做防盗链 讲的是 Nginx 有哪些变量以及一个常见的应用.那么如此灵活的 Nginx 怎么能不支持自定义变量呢,今天的文章就来说一下自定 ...
- CentOS7 开机进入emergency mode
今天突然操作了一下磁盘挂载,然后系统启动之后,就直接进入emergency模式了,然后只能输入密码进行救援,截图如下: 突然想了一下最近的一次操作,是因为要挂在镜像,然后每次开机都要挂载一次,觉得比较 ...
- 连接 mongodb 数据库 :
mongodb 数据库: 安装 mongodb 数据库: 安装 mongodb 数据库网址: https://www.mongodb.com/download-center#community 检 ...
- 性能测试之JVM的故障分析工具VisualVM
VisualVM 是随JDK一同发布的jvm诊断工具,通过插件可以扩展很多功能,插件扩展也是其精华所在. 提供了一个可视界面,用于在Java应用程序在Java虚拟机上运行时查看有关Java应用程序的详 ...
- 8.eclipse 安装 lombook插件
参考博客:https://www.liangzl.com/get-article-detail-129979.html
- elasticSearch插件metricbeat收集nginx的度量指标
ngx_http_stub_status_module模块是Nginx中用来统计Nginx服务所接收和处理的请求数量,只要在编译安装Nginx的时候加上参数--with-http_stub_statu ...
- could not resolve property(无法解析属性)
could not resolve property(无法解析属性) 顾名思义在写hql语句的时候,属性写错了! 请检查大小写,是实体类的,不是数据库表的! 一个一个检查,仔细看!
- win10 麦克风无法使用,可能是设置了权限
驱动什么的都正常,平白无故麦克风不好用了,原来是之前自己设置了麦克风权限: 把这个开关打开就可以了. (完)
- 使用centos8搭建僵尸毁灭工程(PZ)服务器
自从领到了阿里云的ECS服务器后,本着既能熟悉linux操作,又能为喜欢的游戏搭建一个可以和朋友一起联机的服务器(游戏提供自建本地服务器极渣)的想法.作为linux小白的我翻遍了网上的资料,用了五天终 ...