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负载均衡 ...
随机推荐
- program files (x86)\microsoft visual studio 14.0\vc\include\xtree,如果没有找到,下标溢出了,就报错咯
---------------------------Microsoft Visual C++ Runtime Library---------------------------Debug Asse ...
- 在群晖NAS上运行URLOS之后竟然能安装Discuz! Q!!
如果我们手头上有1台群晖NAS时,有没有考虑过把群晖NAS当成服务器来使用,这样会不会很有意思呢? 现在,我们终于可以尝试一番了,把群晖NAS变成一台实实在在的服务器,在上面跑各种运行环境!其实很简单 ...
- 【asp.net core 系列】- 11 Service层的实现样板
0.前言 在<asp.net core 系列>之实战系列中,我们在之前的篇幅中对项目有了一个大概的认知,也搭建了一个基础的项目骨架.那么就让我们继续完善这个骨架,让它更加丰满.这一篇,我将 ...
- SpringBoot中注入ApplicationContext对象的三种方式
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 在项目中,我们可 ...
- PHP利用FTP上传文件连接超时之开启被动模式解决方法
初始代码: <?php $conn = ftp_connect("localhost") or die("Could not connect"); ftp ...
- Redis:rdb和aof
由于redis的数据都直接存储在内存里,在服务器发生宕机时内存的数据会瞬间清空,那么必须要有重启时恢复数据的方法. redis通过持久化机制将数据存储到磁盘中从而在服务器重启时恢复数据,这篇文章主要简 ...
- Python实用笔记 (14)函数式编程——匿名函数
当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x)的函数外, ...
- FRP+WoL实现远程开机+远程桌面
FRP+WoL实现远程开机+远程桌面 故事背景 这是一个很复杂而且很久远的故事,如果要讲的话,这个故事可以追溯到1981年(「都是废话,没有干货,如果不感兴趣请从第二章开始」),简单来说: 1981年 ...
- apply()方法和call()介绍
我们发现apply()和call()的真正用武之地是能够扩充函数赖以运行的作用域. 1.call,apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的 ...
- 前端日常工作中常用开发小技巧 ---JavaScript
1.格式化金钱值 const ThousandNum = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "," ...