37-Nginx负载均衡
Nginx负载均衡基本概述
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾。
往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。
所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB
负载均衡能实现的应用场景一:四层负载均衡
所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑。
负载均衡能实现的应用场景二:七层负载均衡
七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,比如我们说的http应用的负载均衡,它可以实现http信息的改写、头信息的改写、安全应用规则控制、URL匹配规则控制、以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB
四层负载均衡与七层负载均衡区别
四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四层负载均衡高
但是七层负载均衡更贴近于服务,如http协议就是七层协议,我们用nginx可以做会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。
nginx负载均衡配置场景
Nginx要实现负载均衡需要用到proxy_pass代理模块配置。
Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器。而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池。
Nginx负载均衡调度算法
调度算法 | 概述 |
---|
轮询 | 按时间顺序逐一分配到不同的后端服务器(默认) |
weight | 加权轮询,weight值越大,分配到的访问几率越高 |
ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个服务器 |
url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 |
least_conn | 最少连接数,哪个机器链接数少就分发到哪个 |
面试题:简述一下负载均衡的几种调度算法
简述:
1.rr轮询(默认)
2.加权轮询
3.ip_hash
4.url_hash
5.最少链接数
rr轮询
默认轮询
[root@lb01 conf.d]
upstream webs {
server 10.0.0.7;
server 10.0.0.8;
}
server{
listen 80;
server_name www.static.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
加权轮询
[root@lb01 conf.d]
upstream webs {
server 10.0.0.7 weight=5;
server 10.0.0.8;
}
server{
listen 80;
server_name www.static.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
ip_hash
upstream webs {
ip_hash;
server 10.0.0.7;
server 10.0.0.8;
}
server{
listen 80;
server_name www.static.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
Nginx负载均衡后端状态
后端Web服务器在前端Nginx负载均衡调度中的状态
状态 | 概述 |
---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fails失败后,服务暂停时间 |
max_conns | 限制最大的接收连接数 |
upstream webs {
server 10.0.0.7 down;
server 10.0.0.8;
}
server{
listen 80;
server_name www.static.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
upstream webs {
server 10.0.0.7;
server 10.0.0.8 backup;
}
server{
listen 80;
server_name www.static.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
}
Nginx编译安装
想要实现一个功能,但是Nginx默认没有此模块,需要编译安装的方式将新的模块编译进已安装的Nginx服务中 nginx_upstream_check
检查nginx默认模块:
[root@lb01 conf.d]
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
第一步:安装依赖
[root@lb01 ~]
第二步:下载和已经安装的nginx版本相同的源码包
[root@lb01 ~]
nginx version: nginx/1.24.0
[root@lb01 ~]
下载第三方插件:
先下载到Windows,再上传到lb01
解压第三方插件和源码包
[root@lb01 ~]
[root@lb01 ~]
打补丁:将下载的模块加载到nginx模块中
[root@lb01 ~]
[root@lb01 nginx-1.24.0]
[root@lb01 nginx-1.24.0]
[root@lb01 nginx-1.24.0]
[root@lb01 nginx-1.24.0]
测试新加入模块是否可以使用:
[root@lb01 ~]
upstream webs {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
}
server{
listen 80;
server_name www.static.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
location /check {
check_status;
}
}
访问:www.static.com/check
Nginx负载均衡会话保持
在使用负载均衡的时候会遇到会话保持的问题,可通过如下方式进行解决。
1.使用nginx的ip_hash,根据客户端的IP,将请求分配到对应的IP上
2.基于服务端的session会话共享(NFS,MySQL,memcache,redis,file)
在解决负载均衡会话问题,我们需要了解session和cookie的区别。
浏览器端存的是cookie每次浏览器发请求到服务端时,报文头是会自动添加cookie信息的。
服务端会查询用户的cookie作为key去存储里找对应的value(session)
同一个域名下的网站的cookie都是一样的,所以无论几台服务器,无论请求分配到哪一台服务器上同一用户的cookie是不变的。也就是说cookie对应的session也是唯一的。所以,这里只要保证多台业务服务器访问同一个共享存储服务器(NFS,MySQL,memcache,redis,file)就行了。
配置phpadmin业务
1.配置nginx
[root@web01 conf.d]
[root@web01 conf.d]
server {
listen 80;
server_name www.admin.com;
root /code/admin;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]
2.配置代码目录
[root@web01 conf.d]
[root@web01 conf.d]
配置代码目录 下载代码
[root@web01 conf.d]
[root@web01 conf.d]
[root@web01 admin]
解压代码:
[root@web01 admin]
拷贝代码到当前的目录
[root@web01 admin]
3.配置数据库连接
[root@web01 admin]
[root@web01 admin]
$cfg['Servers'][$i]['host'] = '172.16.1.51';
4.远程用户登陆。
5.快速部署web02
1.scp配置文件
[root@web01 admin]
2.scp代码
[root@web01 code]
[root@web01 code]
6.配置负载均衡
[root@lb01 conf.d]
[root@lb01 conf.d]
upstream admin {
server 10.0.0.7;
server 10.0.0.8;
}
server{
listen 80;
server_name www.admin.com;
location / {
proxy_pass http://admin;
include proxy_params;
}
}
[root@lb01 conf.d]
nginx: [warn] conflicting server name "www.she.com" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]
测试登录phpadmin,发现无法登陆。原因:session会话写入了磁盘中,负载均衡会轮询访问web01和web02。session不能验证成功。
解决:会话保持redis.
phpmyadmin业务实现会话保持写入到redis
10.0.0.51安装redis
[root@db01 ~]
2.配置redis 修改监听地址为172内网段 默认只监听127.0.0.1
[root@db01 ~]
bind 127.0.0.1 172.16.1.51
3.启动redis
[root@db01 ~]
[root@db01 ~]
4.检查端口
[root@db01 ~]
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 4436/redis-server 1
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 4436/redis-server 1
5.配置php会话写入到redis中
web01和web02同时修改
vim /etc/php.ini
[root@web01 code]
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
;session.save_path = "tcp://172.16.1.51:6379?auth=123"
注释两行:
[root@web01 code]
[root@web01 code]
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
重启服务生效:
[root@web01 code]
文件同步到web02
[root@web01 code]
[root@web01 code]
[root@web02 code]
页面登录:www.admin.com
查看reids中的cookie值:
[root@db01 ~]
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:f8c7cc03ab9b3993914c230f354ad76a"
127.0.0.1:6379>