Nginx-负载均衡系列

综合架构-负载均衡系列

一个新的开始

本科/大专
学历
态度 态度,素质
较高
力所能及,地面面垃圾,人都走了空调,灯
(第一个来,最后一个走坚持至少3-4)
学习能力 学习能力
强(学习方
法)
费曼学习法, 表达(说),总结
记忆法
star s(什么情况) t(什么目标) a(如何做?)
r(什么结果)
smart(管理) 明确的需求 可以衡量结果 可
以达到 多个任务之间相关性 截止时间
3Q(IQ EQ AQ) IQ EQ AQ(只要活着,任何事情都可以重来.)

一 代理模块 proxy

2.1 概述

  • proxy 代理
    • 网站:让网站可以承受更高并发/访问量
    • 用户:通过代理上网加速/翻Q........
  • 反向代理与正向代理

2.2 正向代理用户

1621301798481

2.3 反向代理

  • 企业场景中:
    • 反向代理+web集群
    • 反向代理+单机

1621324340156

2.4 反向代理环境准备

环境
lb01 10.0.0.110/172.16.1.110 nginx
1.20.0
负载均衡 lb.zhangyuzhou.com
web01 10.0.0.101/172.16.1.101 nginx
1.20.0
后端服务器
web02 10.0.0.102/172.16.1.102 nginx
1.20.0
后端服务器
  • web01

#01 node01 web监听 8080
[root@node01 conf.d]# cat pass-web.conf 
server {
	listen 8080;
	server_name 10.0.0.101;
	location / {
	root /code/web;
	index index.html;	
	}
}


#02 书写站点目录
[root@node01 conf.d]# echo 'node01' >>/code/web/index.html
[root@node01 conf.d]# cat /code/web/index.html
node01




#03  lb01 负载均衡配置
[root@lb01 conf.d]# cat pass.conf 
server {
	listen 80;
	server_name lb.zhangyuzhou.com;
	
	location / {
	proxy_pass http://10.0.0.101:8080;
}
}

#解析下
echo "10.0.0.110 lb.zhangyuzhou.com" >>/etc/hosts
 



  • web页面访问

2.5 反正代理指令

代理指令proxy_pass 将请求转发对应地址
格式 proxy_pass url;
上下文 location , if in location , limit_except
proxy_set_header Host $http_host; 修改 Host的内容
proxy_set_header
proxy_set_header Host   $http_host;    #修改 Host的内容 

$host
proxy_set_header X-Forwarded-For 
$proxy_add_x_forwarded_for;    #增加 代理-->web请求头信息 让后端节点记录用户真实ip地址(客户端ip地址)
$proxy_add_x_forwarded_for
$remote_add



the “X-Forwarded-For” client request header field with 
the $remote_addr variable appended to it, separated by 
a comma. 
If the “X-Forwarded-For” field is not present in the 
client request header, the $proxy_add_x_forwarded_for
variable is equal to the $remote_addr variable.




#如果请求头中有X-Forwarded-For 字段 则
proxy_add_x_forwarded_for 记录用户ip地址 累加追加
#$remote_addr 记录一个ip 




########### 必备模块 
proxy_pass 将请求转发对应的地址
proxy_set_header Host $http_host;   # 设置或host请求头(代理向后端发起的host) $http_host 变量 后端可以读取

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   # 后端服务可以记录客户的真实ip 并且追加性质 (X-Forwarded-Fo 变量 记录在日志里)

proxy_connect_timeout 30s;  # 代理与后端节点建立连接的超时时间
proxy_read_timeout 60s;		# 代理服务器发送请求给后端节点 超时时间
proxy_send_timeout 60s;		# 代理服务器读取后端响应的时间

proxy_buffering on;			# 是否开启代理服务器 缓存后端节点的响应报文信息(响应头和响应主体) 如果响应报文无法放进内存 则可以放进磁盘
proxy_buffer_size 16k;		# 设置size用于读取从代理服务器接收到的响应头大小
proxy_buffers 8 128k;		# 设置单个连接响应缓存响应信息 的大小( 8 *128k)




  • 记录客户真实ip proxy_set_header
proxy_set_header
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 增加 代理-->web请求头信息 让后端节点记录用户真实ip地址(客户端ip地址) 累计追加
$proxy_add 记录一个ip
X-Forwarded-For 如果请求头中有X-Forwarded-For 字段, 记录用户ip地址 累加追加
  • 连接,读取,写入超时时间
# 代理与后端节点建立连接的超时时间  不建议超过75秒 			
	Syntax:	proxy_connect_timeout time;
	Default: proxy_connect_timeout 60s;		
	Context: http, server, location
	

# 代理服务器读取后端响应时间 read
	Syntax:	proxy_read_timeout time;
	Default: proxy_read_timeout 60s;			
	Context: http, server, location


# 代理服务器发送请求给后端节点 超时时间 
	Syntax:	proxy_send_timeout time;
	Default:proxy_send_timeout 60s;				
	Context:http, server, location

  • 各种缓存
#buffer 系列
####proxy_buffering   是否开启代理服务器 缓存后端节点的响应
报文信息(响应头和响应主体)
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location



####启用缓冲后,nginx会尽快从代理服务器接收响应,并将其保存到
proxy_buffer_size和proxy_buffers指令设置的缓冲区中。
如果整个响应无法放入内存,则可以将部分响应保存到磁盘上的临时文件
中。
写入临时文件由proxy_max_temp_file_size和
proxy_temp_file_write_size指令控制。
proxy_connect_timeout  
格式 proxy_connect_timeout 时间;  


##proxy_busy_buffers_size
####proxy_buffer_size
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location


##设置用于读取从代理服务器接收的响应的第一部分的缓冲区大小。
##这部分通常包含一个小的响应头。
默认情况下,缓冲区大小等于一个内存页。这是4K或8K,取决于平台。
不过,它可以做得更小。
设置8k | 16k



##为单个连接缓存响应信息 8 128k;
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location


#为单个连接设置用于从代理服务器读取响应的缓冲区的数量和大小。
默认情况下,缓冲区大小等于一个内存页。这是4K或8K,取决于平台。



二 负载均衡

2.1 定义

  • OSI 7层模型

  • 7层负载均衡:应用场景 用于处理用户uri 处理用户客户端,rewrite(修改url/uri/伪静态)

  • 4层负载均衡:处理端口 最多识别四层

2.1 环境准备

环境
lb01 10.0.0.110/172.16.1.110 nginx
1.20.0
负载均衡 lb.zhangyuzhou.com
web01 10.0.0.101/172.16.1.101 nginx
1.20.0
后端服务器 zhangbingbing 主机名 + IP
web02 10.0.0.102/172.16.1.102 nginx
1.20.0
后端服务器 zhangbingbing 主机名 + IP

2.2 单域名单个upstream

# 负载均衡静态网站
# 搭建环境

#01 web环境
[root@node01 conf.d]# cat proxy.conf 
server {
        listen 80;
        server_name 10.0.0.101;
        root /code/proxy;
        location / {
        index index.html;
        }
}


[root@node02 ~]# cat /etc/nginx/conf.d/proxy.conf
server {
	listen 80;
	server_name 10.0.0.102;
	location / {
	root /code/proxy;
	index index.html;
	}
}

##站点目录
[root@node01 conf.d]# echo `hostname` `hostname -I` >>/code/proxy/index.html
[root@node01 conf.d]# cat /code/proxy/index.html
node01 10.0.0.101



#03 lb01 配置文件
[root@lb01 conf.d]# cat pass-web.conf 
upstream proxy_web {
	server 10.0.0.101:80;
	server 10.0.0.102:80;
}

server {
	listen 80;
	server_name lb.zhangyuzhou.com;

	location / {
	proxy_pass http://proxy_web;
	include proxy_params;
}

}


nginx -s reload

#04 引用的模块功能
[root@lb01 conf.d]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;

proxy_buffering on;
proxy_buffer_size 16k;
proxy_buffers 8 128k;


#05 访问测试
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node01 10.0.0.101
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node01 10.0.0.101
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102



2.3 多域名多个upstream

环境 域名
lb01 10.0.0.110/172.16.1.110 nginx
1.20.0
负载均衡 lb.zhangyuzhou.com
web01 10.0.0.101/172.16.1.101 nginx
1.20.0
dong.zhangyuzhou.com 后端服务器 dong 默认
web02 10.0.0.102/172.16.1.102 nginx
1.20.0
jing.zhangyuzhou.com 后端服务器 jing 静态
  • 配置负载
#01 书写负载 配置
[root@lb01 conf.d]# cat proxys.conf
upstream dong {
server 10.0.0.101:80;
}
upstream jing {
server 10.0.0.102:80;
}

server {
	listen 80;
	server_name dong.zhangyuzhou.com;
	location / {
	proxy_pass http://dong;
	include proxy_params;
	}
}
server {
        listen 80;
        server_name jing.zhangyuzhou.com;
        location / {
        proxy_pass http://jing;
        include proxy_params;
  


#02 配置hosts 解析
[root@lb01 conf.d]# tail -1 /etc/hosts
10.0.0.110 lb.zhangyuzhou.com dong.zhangyuzhou.com jing.zhangyuzhou.com



  • 后端web 配置
#01  配置代码
1)web02
echo 'jing' >> /code/proxy/index.html 

2) web01
echo 'dong ' >> /code/proxy/index.html 



[root@node01 conf.d]# cat proxy.conf
server {
	listen 80;
	server_name 10.0.0.101;
	root /code/proxy;
	location / {
	index index.html;
} 
}

[root@node02 conf.d]# cat proxy.conf
server {
	listen 80;
	server_name 10.0.0.102;
	root /code/proxy;
	location / {
	index index.html;
} 
}


  • 访问测试
[root@lb01 conf.d]# curl jing.zhangyuzhou.com
node02 10.0.0.102
jing
[root@lb01 conf.d]# curl dong.zhangyuzhou.com
node01 10.0.0.101
dong 


2.4 负载均衡-7层-配置详解

调度算法

调度算法 概述
轮询 按时间顺序注意分配到后端服务器(默认)
weight 加权轮询weight值越大 分配到的访问几率越高
least_conn 最少连接数 那个机器链接数少就分发
ip_hash 每个请求按访问ip的hash结果分配 这个来自同一IP的固定访问一个后端服务器
url_hash 按照访问url的hash结果来分配请求 是每个URL定向到同一个后端服务器

注意 ip_hash和加权不能同时使用

  • 负载均衡+轮询具体配置
#调度算法: 负载均衡如何处理/分发用户的请求,如何给后端的服务器

#静态算法
## rr 轮询算法: 1一次1个
## wrr 加权轮询:(后端节点 服务器配置不同)
### weight 
web01 1c1g
web02 1c1g
web03 4c8g
upstream blog {
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80 weight=4;
}


#动态算法
#least_conn 最小连接数,负载均衡会定期检查后端服务器的连接数
指定upstream使用的负载均衡算法,将请求传递到活动连接数最少的服务
器,并考虑服务器的权重。
如果有多个这样的服务器,则结合wrr和least_conn一起调度。
upstream blog {
least_conn;
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80 weight=4;
}


#ip_hash 对用户客户端ip进行hash,每次用户都会访问同一台服务器
好处: 解决用户登录状态问题(会话共享/登录会话)
缺点: 导致后端节点负载不均
upstream blog {
ip_hash;
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80 weight=1;
}

#url_hash 对应用户请求中的url进行hash,每次用户访问相同的url,访
问相同的后端节点
应用场景: 配置缓存服务器,让用户指定的url/uri,访问缓存服务器.
upstream blog {
hash $requst_uri; #对指定的变量进行hash
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80 weight=1;
}



# 测试
# curl -H Host:proxy.oldboy.com 172.16.1.5
# curl -H Host:proxy.oldboy.com 10.0.0.5


2.5 upstream 模块配置

  • upstream 模块可用参数

# upstream 模块
Syntax:	server address [parameters];
Default:	—
Context:	upstream

server中可以用的参数 含义
weight 设置权重
max_conns 设置某个节点最大连接数
max_fails 设置nginx负载均衡检查后端节点失败次数(一般公司 3) (CDN 10)
fail_timeout 超过最大失败次数后 经过fail_timeout时间 再次检查节点是否可用 默认10s
backup 设置为备胎模式 其他节点都挂了的时候 启动backup 服务器
down 处于关闭状态 一般是用来指定一些服务器 这些服务器处于维护状态
#01 测试权重
[root@lb01 conf.d]# cat pass-web.conf 
upstream proxy_web {
	server 10.0.0.101:80 weight=1 ;
	server 10.0.0.102:80 weight=2 ;

}

server {
	listen 80;
	server_name lb.zhangyuzhou.com;

	location / {

	proxy_pass http://proxy_web;
	include proxy_params;
}
}


#访问测试
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102
jing
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node01 10.0.0.101
dong 
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102
jing
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102
jing
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node01 10.0.0.101
dong 
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102
jing
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
node02 10.0.0.102
jing



#02 测试健康检查
[root@lb01 conf.d]# cat pass-web.conf 
upstream proxy_web {
	server 10.0.0.101:80 weight=1 max_fails=3 fail_timeout=20;
	server 10.0.0.102:80 weight=2 max_fails=3 fail_timeout=20;

}

server {
	listen 80;
	server_name lb.zhangyuzhou.com;

	location / {

	proxy_pass http://proxy_web;
	include proxy_params;
}
}




# 测试 
[root@lb01 conf.d]$
for n in {1..1000000};
do
curl -H Host:lb.zhangyuzhou.com 10.0.0.110 ;
date +%T; sleep 1 ; 
done

# 关闭其中一台web 查看再次连接时间


2.6 uri 转发 动静分离**重要

域名
负载均衡进行转发 lb01 10.0.0.110 lb.zhangyuzhou.com
web01 请求/static 静态请求
10.0.0.101 处理
jing.zhangyuzhou.com /code/uri/static/index.html
static web01 (首页)
web02 请求/dong 动态请求
10.0.0.102 处理
dong.zhangyuzhou.com /code/uri/dong/index.html
active web02
  • web 配置
# web01服务器
#01 书写配置文件
[root@node01 conf.d]# cat jing.conf 
 server {
        listen 80;
        server_name jing.zhangyuzhou.com;
        root /code/uri/ ;
        location /static {
        index index.html;
        }
	location / {
	index index.html;
}
}



nginx -s reload

#02 创建站点目录
mkdir -p /code/uri/static/
echo 'web01 static' >/code/uri/static/index.html
echo 'type web01 static' >/code/uri/index.html

#03 配置解析
echo 10.0.0.101 jing.zhangyuzhou.com >>/etc/hosts

#04 访问测试
[root@node01 conf.d]# curl  jing.zhangyuzhou.com/static/
web01 static



# web02
mkdir -p /code/uri/dong/
echo 'web02 dong' >/code/uri/dong/index.html
echo 10.0.0.102 dong.zhangyuzhou.com >>/etc/hosts

#01 书写配置文件
[root@node02 conf.d]# cat dong.conf 
 server {
        listen 80;
        server_name dong.zhangyuzhou.com;
        root /code/uri;
        location /dong {
        index index.html;
        }
}



# 重启测试
nginx -s reload

[root@node02 conf.d]# curl  dong.zhangyuzhou.com/dong/
web02 dong


#02 负载均衡上访问测试
[root@lb01 ~]# curl -H Host:jing.zhangyuzhou.com 10.0.0.101/static/
web01 static
[root@lb01 ~]#  curl -H Host:dong.zhangyuzhou.com 10.0.0.102/dong/
web02 dong



  • 负载均衡配置
[root@lb01 conf.d]$ cat zhuan.conf 
upstream static {
  server 10.0.0.101:80;
}
upstream dong {
  server 10.0.0.102:80;
}
server {
	listen 80;
	server_name lb.zhangyuzhou.com;
	location / {
	proxy_pass http://static;
	include proxy_params;
	}
	location /static {
	proxy_pass http://static;
	include proxy_params;
	}
	location /dong {
	proxy_pass http://dong;
	include proxy_params;
	}
}


# 测试 
[root@lb01 conf.d]# curl lb.zhangyuzhou.com
type web01 static
[root@lb01 conf.d]# curl lb.zhangyuzhou.com/static/
web01 static
[root@lb01 conf.d]# curl lb.zhangyuzhou.com/dong/
web02 dong




2.7 if 判断 user_agent转发

  • user_agent 用户使用的代理(iphone pc ipad )
lb01
web01 /code/uri/static/index.html phone web01
web02 /code/uri/dong/index.htm pc web02
http_user_agent 
$request_uri
location #只能匹配 请求里面的uri


# 配置文件
[root@lb01 conf.d]$ cat zhuan.conf 
upstream static {
  server 10.0.0.7:80;
}
upstream dong {
  server 10.0.0.8:80;
}
server {
	listen 80;
	server_name zhuan.oldboy.com;
	location / {
	if ($http_user_agent ~* "android|iphone|ipad") {
		proxy_pass http://static;
	}
	if ($http_user_agent ~* MSIE ) {
		return 200 "please do not use ie";
	}
	proxy_pass http://dong;
	include proxy_params;
	}
	}




# 测试
[root@lb01 conf.d]$ curl -H Host:zhuan.oldboy.com 10.0.0.5
web02 pc
[root@lb01 conf.d]$ curl -Aiphone  -H Host:zhuan.oldboy.com 10.0.0.5
web 01 iphone
[root@lb01 conf.d]$ curl  -H Host:zhuan.oldboy.com 10.0.0.5
web02 pc
[root@lb01 conf.d]$  curl -A MSIE  -H Host:zhuan.oldboy.com 10.0.0.5
please do not use ie[root@lb01 conf.d]$ 

# -A 指定终端访问


# 详解
if ($http_user_agent ~* "android|iphone|ipad") {
		proxy_pass http://static;
	}
	if ($http_user_agent ~* MSIE ) {
		return 200 "please do not use ie";
	}

# 如果客户使用android|iphone|ipad 访问 则分配给proxy_pass http://static;资源池 
# 如果客户使用MSIE访问 则return 200 "please do not use ie"; 返回码200 并显示"内容"

# proxy_pass http://dong; 如果都不是 则走 默认 


三 四层负载均衡

  • 7层负载均衡处理http/https请求 做一个代理 高并发情况下 每次负载请求后端节点 占用一个端口(随机)
  • 4层负载均衡,lvs(对数据进行转发),效率更高. nginx 4层负载均衡,本质上
    还是代理模式.

nginx 4层 负载均衡 需要 stream(stream_proxy stream_upstream)
模块
nginx 7层 负载均衡 http (http_proxy http_upstream )
ssh 负载均衡

# web01 
stream {
	log_format basic  '$remote_addr  [$time_local] '
	'$protocol $status $bytes_sent $bytes_received '
	'$session_time';
    access_log  /var/log/nginx/4layer-nginx-access.log basic;

	server {
	listen 9000;
	proxy_pass 10.0.0.7:22;
	}
	}


## 
stream {
   log_format basic '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent 
$bytes_received '
                 '$session_time';
   access_log /var/log/nginx/4layer-nginx-access.log 
basic ;
   upstream ssh {
     server 10.0.0.7:22;
     server 10.0.0.8:22;
   }
   server {
   listen 9000;
   proxy_pass ssh;
   }
}


  • mysql 3306 负载均衡
upstream mysql {
     server 10.0.0.7:3306;
     server 10.0.0.51:3306;
   }
   server {   listen 8888;
   proxy_pass mysql;
   }
   
   
   
   
   
#lb01 
   upstream chat {
     server 10.0.0.7:8848;
     server 10.0.0.8:8848;
   } 
   server {
   listen 8848;
   proxy_pass chat;
   }
#web01 02 
nc -lk 8848

posted @   宁采臣open  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示