负载均衡
负载均衡概念
1、实现用户访问调度处理,最终分配给不同web节点上
2、实现用户访问的压力进行分担,将网站压力分配给每一个节点
实现负载均衡的方式
硬件设备
F5 A10 Redware
开源软件
Nginx Haproxy Lvs
lvs、haproxy、nginx区别
从命名分析
负载均衡:用户请求的转发 (lvs)
反向代理:代替用户去找,找到后再发给用户 (nginx haproxy)
从功能分析
1、lvs是4层负载均衡 传输层tap/udp 端口号
最多进行端口转发
2、nginx haproxy是4层和7层负载均衡 应用层 进行http协议和uri转发
负载均衡与反向代理区别
image.png
准备环境工作
配置nginx安装源然后安装
[oot@lb01 ~]# vim /etc/yum.repos.d/nginx.repo
▽
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
修改web01 web02配置文件
记得提前备份
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/access_www.log main ;
root /app/www;
location / {
index index.html index.htm;
}
}
[root@web01 /etc/nginx/conf.d]# cat 02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
root /app/blog;
location / {
index index.php index.html index.htm;
}
location ~* \.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启nginx检查语法
web01 web02 创建站点目录与首页文件
俩边都相同
[root@web01 /etc/nginx/conf.d]# mkdir -p /app/{www,blog}
[root@web01 /etc/nginx/conf.d]# for n in www blog ; do echo $n.oldboy.com >/app/$n/index.html ;done
[root@web01 /etc/nginx/conf.d]# tree /app/
/app/
├── blog
│ └── index.html
└── www
└── index.html
2 directories, 2 files
[root@web02 conf.d]# tree /app
/app
├── blog
│ └── index.html
└── www
└── index.html
2 directories, 2 files
去db01上curl一下
curl -H Host:www.oldboy.com 10.0.0.[7-8]
image
编写nginx反向代理服务配置文件(lb01)
ngx_http_upstream_module 负载均衡
ngx_http_proxy_module 反向代理
[root@lb01 ~]# vim /etc/nginx/nginx.conf
...
upstream web_pools {
server 10.0.0.7:80;
server 10.0.0.8:80;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
}
}
}
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
为web01 web02首页文件追加内容让容易区分
for n in www blog ; do echo `hostname` $n.oldboy.com >/app/$n/index.html ;done
[root@web01 conf.d]# cat /app/www/index.html
web01 www.oldboy.com
[root@web02 conf.d]# cat /app/blog/index.html
web02 blog.oldboy.com
在lb01上curl一下
[root@lb01 ~]# curl 10.0.0.7
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.8
web02 www.oldboy.com
[root@lb01 ~]#
[root@lb01 ~]#
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
image
抓包
image.png
upstream模块参数:
server —— RS配置,可以是ip或域名
weight ——权重
max_fails ——失败次数
fail_timeout =10s ——多久后在检查一遍
backup ——如果加上backup 会在池塘中其他机器都挂掉 才会启动
down 让服务器不可用
image
image
image
image
配置权重
weight=1;
upstream web_pools {
server 10.0.0.7:80 weight=2;
server 10.0.0.8:80 weight=1;
}
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
CND加速缓存
网站加速 缓存网站静态页面 视频(切片)
用户先访问cdn
cdn缓存没有 就转到源站
cdn公司介绍:蓝汛 网宿 阿里云
image
配置文件中添加server模块的参数(lb01)
weight 权重;
max_fails 健康检查,失败次数;
fail_timeout 多久后在检查一遍
修改配置模块参数
upstream web_pools {
server 10.0.0.7:80 weight=2 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
测试关闭一台后,是否还能访问:
for n in {1..1000};do curl 10.0.0.5/index.html ;sleep 1;done
image.png
请求访问第二个站点blog.oldboy.com
image.png
抓包看一下情况:
image
image
修改 请求头
proxy_set_header Host $host;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
}
}
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
再访问就成功了
image.png
显示客户端的地址,并记录到日志中
proxy_set_header X-Forwarded-For $remote_addr;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
这里测试的是在lb01上curl的web01
image
image
添加访问控制
如果某些网段访问量成千上万,特别高的话,可能是被入侵了
需要给这个网址做限制访问
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($remote_addr ~ "^192.168.22.") { \\指定禁止访问的网段
return 403 "别捣乱"; \\定义的是指定网段中,客户访问后返回的内容
}
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
防火墙规则—iptables
iptables详细用法http://man.linuxde.net/iptables
image
image
--dport 指定端口号
iptables -A INPUT -p tcp -s 192.168.22.0/24 -j DROP
-A:向规则链中添加条目;
-P:定义规则链中的默认目标;
-s:指定要匹配的数据包源ip地址;
-j<目标>:指定要跳转的目标;
指定网段,配置时不要把自己挡外面,这就要跑机房了~
image
image
-F:清楚规则链中已有的条目;
-Z:清空规则链中的数据包计算器和字节计数器;
-X:删除用户自定义的链
image
image
负载均衡的设备转换、动静分离
准备环境
web01上
[root@web01 ~]# echo 'this is PC website' >/app/www/lidao.html
web02上
[root@web02 ~]# echo 'this is Mobile website' >/app/www/lidao.html
lb01上curl一下
image
根据用户客户端的设备 进行转发 请求:
image.png
配置 upstream 与location
定义upstream移动端与PC端
upstream default {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream mobile {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
移动端的服务器池
location / {
if ($http_user_agent ~* "Android|IOS") {
proxy_pass http://mobile;
}
完整配置
[root@lb01 nginx]# vim nginx.conf
...
upstream default {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream mobile {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($http_user_agent ~* "Android|IOS") {
proxy_pass http://mobile;
}
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
[root@lb01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 nginx]# systemctl reload nginx
记得将之前环境中的其他模块一起修改或暂时注释掉,不然语法会报错
curl一下查看结果
curl -A 可以指定系统
[root@lb01 nginx]# curl 10.0.0.5/lidao.html
this is PC website
[root@lb01 nginx]# curl -A ios 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 nginx]# curl -A Android 10.0.0.5/lidao.html
this is Mobile website
可以下载火狐浏览器查看
http://www.firefox.com.cn/
安装插件流程:image
image
image
image
image
根据 URI 中的目录地址实现代理转发(动静分离)
添加一台测试web03节点—10.0.0.9
将web03的配置与web01和web02配置相同
(/app站点目录与nginx.conf配置文件)
image
image.png
image.png
准备环境
www.oldboy.com/upload/index.html
www.oldboy.com/static/index.html
www.oldboy.com/index.html
#web01:
mkdir -p /app/www/upload/index.html
echo this is upload >/app/www/index.html
[root@web01 ~]# cat /app/www/upload/index.html
this is upload
#web02:
mkdir -p /app/www/static/index.html
echo this is static >/app/www/index.html
[root@web02 ~]# cat /app/www/static/index.html
this is static
#web03:
mkdir -p /app/www/index.html #之前已经有首页文件,只需修改内容
echo this is default >/app/www/index.html
[root@web03 ~]# cat /app/www/index.html
this is default
配置 upstream 与location
定义upstream.
upstream upload {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
添加location
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /default {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
完整配置
image
[root@lb01 nginx]# vim nginx.conf
....
upstream upload {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
[root@lb01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 nginx]# systemctl reload nginx
浏览器测试一下
上传
image.png
静态
image.png
动态(默认)
image.png
轮询算法
image
ip_hash
只要客户端ip地址相同就会被转发到同一台机器上image
cookie与session会话区别
会话保持
cookie
session
共同点
存放用户信息
key value类型 变量和变量内容
区别
cookie:
存放在浏览器
为保证安全性,存放简单信息、钥匙
开发设置
响应服务器给你设置
session:
存放在服务器
存放敏感信息
存放锁头
image.png
Nginx负载均衡与反向代理相关参数
资料连接:
https://www.cnblogs.com/sky00747/p/8628866.html
https://www.cnblogs.com/syaving/p/7907447.htmlimage
实现Nginx负载均衡的组件主要有两个:
ngx_http_proxy_module proxy 代理模块,用于把请求后抛给服务器节点或upstream服务器池
ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
反向代理重要参数
prox_pass http://server_pools; 通过proxy_pass功能把用户的请求转向到反向代理定义的upstream服务器
proxy_set_header Host $host; 在代理向后端服务器发送的http请求头中加入host字段信息,用于后端服务器配置有多个虚拟主机,可以识别那个虚拟主机
proxy_set_header X-Forwarded-For $remot_addr; 用于接收用户真实IP,而不是代理服务器ip
在配置文件里都会加上include proxy_params或proxy.conf;
在proxy.conf里增加参数,会显得干净
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header HOST $host; #设置http请求header项传给后端服务器节点,可实现让代理后端服务器节点获取访问客户端用户真实IP地址
proxy_set_header X-Forwarded-For $remote_addr; #用于接收用户真实IP,而不是代理服务器ip
proxy_connect_timeout 30; #表示反向代理与后端节点服务器连接的超时时间
proxy_send_timeout 60; #代理后端服务器的数据回传时间,在规定时间之内服务器必须传完所有数据,否则断开
proxy_read_timeout 60; # 设置nginx从代理的后端服务器获取信息时间,表示连接建立成功后,nginx等待后端服务器的响应时间
proxy_buffer_size 32k; #设置缓冲区大小,默认该缓冲区大小等于指令proxy_buffers设置大小
proxy_buffering on; #启用或禁用来自代理服务器的响应缓冲
proxy_buffers 4 128k; # 这是缓冲区的数量和大小,nginx从代理的后端服务器获取响应信息
proxy_busy_buffers_size 256k; #用于设置系统很忙时可以使用的proxy_buffers大小,官方推荐proxy_buffer*2
proxy_max_temp_file_size 256k; #当启用来自代理服务器的响应缓冲,并且整个响应不适合proxy_buffer_size和proxy_buffers指令设置的缓冲区时,响应的一部分可以保存到临时文件中。
Nginx根据URL中的目录地址实现代理转发
image.png
当用户请求www.daxian.com/upload/xx地址的时候,实现由upload上传服务器池处理请求
当用户请求www.daxian.com/static/xx地址的时候,实现由静态服务器池处理请求
除此之外,对于其他访问请求,全部交给默认动态服务器请求
image
image
image
image
image
image
image
image.png
image.png
image
image
image.png
image
image
image
image
image
image
image
image
image
image.png
image
image
image
image
image
image
image.png
image.png
image
image.png
image.png
image.png
image
image.png
image