Nginx负载均衡(四)
负载均衡描述
负载均衡的原理及处理流程
负载均衡的作用
负载均衡的处理方式
四/七层负载均衡
实现四层负载均衡的方式: 硬件:F5 BIG-IP、Radware等 软件:LVS、nginx、haproxy等
实现七层负载均衡的方式: 软件:Nginx、Hayproxy等
四层负载均衡数据包是在底层就进行了分发,而七层负载均衡数据包则在最顶端进行分发,所以四层负载均衡的效率比七层负载均衡的要高。
四层负载均衡不识别域名,而七层负载均衡识别域名。
四层负载(LVS)+七层负载(Nginx)
nginx七层负载
Nginx七层负载均衡的指令
upstream指令
| 语法 | upstream name {...} |
| 默认值 | - |
| 位置 | http |
server指令
| 语法 | server name [paramerters] |
| 默认值 | - |
| 位置 | upstream |
Nginx七层负载均衡的实现流程

服务端设置
server { listen 9001; server_name localhost; default_type text/html; location /{ return 200 '<h1>192.168.38.153:9001</h1>'; } }
负载均衡器设置
upstream backend{ server 192.168.200.146:9091; server 192.168.200.146:9092; server 192.168.200.146:9093; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
负载均衡状态
代理服务器在负载均衡调度中的状态有以下几个:
| 状态 | 概述 |
| down | 当前的server暂时不参与负载均衡 |
| backup | 预留的备份服务器 |
| max_fails | 允许请求失败的次数 |
| fail_timeout | 经过max_fails失败后,服务暂停时间 |
| max_conns | 限制最大的连接数 |
down
upstream backend{ server 192.168.200.146:9001 down; server 192.168.200.146:9002 server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
backup
upstream backend{ server 192.168.200.146:9001 down; server 192.168.200.146:9002 backup; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
firewall-cmd --query-port=9001/tcp
firewall-cmd --permanent --add-port=9002/tcp
firewall-cmd --permanent --add-port=9001-9003/tcp
firewall-cmd --permanent --remove-port=9003/tcp
重新加载
firewall-cmd --reload
max_conns
max_fails和fail_timeout
upstream backend{ server 192.168.200.133:9001 down; server 192.168.200.133:9002 backup; server 192.168.200.133:9003 max_fails=3 fail_timeout=15; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
负载均衡策略
| 算法名称 | 说明 |
| 轮询 | 默认方式 |
| weight | 权重方式 |
| ip_hash | 依据IP分配方式 |
| least_conn | 依据最少连接方式 |
| url_hash | 依据URL分配方式 |
| fair | 依据响应时间方式 |
轮询
upstream backend{ server 192.168.200.146:9001 weight=1; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
weight加权[加权轮询]
upstream backend{ server 192.168.200.146:9001 weight=10; server 192.168.200.146:9002 weight=5; server 192.168.200.146:9003 weight=3; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
ip_hash
| 语法 | ip_hash |
| 默认值 | - |
| 位置 | upstream |
upstream backend{ ip_hash; server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
least_conn
upstream backend{ least_conn; server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
url_hash
upstream backend{ hash &request_uri; server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
http://192.168.200.133:8083/a http://192.168.200.133:8083/b http://192.168.200.133:8083/c

fair
upstream backend{ fair; server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
下载地址为: https://github.com/gnosek/nginx-upstream-fair
unzip nginx-upstream-fair-master.zip
mv nginx-upstream-fair-master fair
4、使用./confifigure命令将资源添加到Nginx模块中
./configure --add-module=/root/fair
5、编译
make

解决方案:
in_port_t default_port

然后进行make
6、更新Nginx
6.1 将sbin目录下的nginx进行备份
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold
6.2 将安装目录下的objs中的nginx拷贝到sbin目录
cd objs
cp nginx /usr/local/nginx/sbin
6.3 更新Nginx
cd ../
make upgrade
7. 编译测试使用Nginx
负载均衡案例
案例一:对所有请求实现一般轮询规则的负载均衡
upstream backend{ server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
案例二:对所有请求实现加权轮询规则的负载均衡
upstream backend{ server 192.168.200.146:9001 weight=7; server 192.168.200.146:9002 weight=5; server 192.168.200.146:9003 weight=3; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } }
案例三:对特定资源实现负载均衡
upstream videobackend{ server 192.168.200.146:9001; server 192.168.200.146:9002; } upstream filebackend{ server 192.168.200.146:9003; server 192.168.200.146:9004; } server { listen 8084; server_name localhost; location /video/ { proxy_pass http://videobackend; } location /file/ { proxy_pass http://filebackend; } }
案例四:对不同域名实现负载均衡
upstream itcastbackend{ server 192.168.200.146:9001; server 192.168.200.146:9002; } upstream itheimabackend{ server 192.168.200.146:9003; server 192.168.200.146:9004; } server { listen 8085; server_name www.itcast.cn; location / { proxy_pass http://itcastbackend; } }
案例五:实现带有URL重写的负载均衡
upstream backend{ server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 80; server_name localhost; location /file/ { rewrite ^(/file/.*) /server/$1 last; } location / { proxy_pass http://backend; } }
Nginx四层负载均衡
添加stream模块的支持
》将原有/usr/local/nginx/sbin/nginx进行备份 》拷贝nginx之前的配置信息 》在nginx的安装源码进行配置指定对应模块 ./configure -- with-stream 》通过make模板进行编译 》将objs下面的nginx移动到/usr/local/nginx/sbin下 》在源码目录下执行 make upgrade进行升级,这个可以实现不停机添 加新模块的功能
Nginx四层负载均衡的指令
stream指令
| 语法 | stream{...} |
| 默认值 | - |
| 位置 | main |
upstream指令
该指令和http的upstream指令是类似的
四层负载均衡的案例
需求分析

tar -zxf redis-4.0.14.tar.gz
3、j进入redis的安装包
cd redis-4.0.14
4、使用make和install进行编译和安装
make PREFIX=/usr/local/redis/redis01 install
5、拷贝redis配置文件redis.conf到/usr/local/redis/redis01/bin目录中
cp redis.conf /usr/local/redis/redis01/bin
6、修改redis.conf配置文件
port 6379 #redis的端口 daemonize yes #后台启动redis
7、将redis01复制一份为redis02
cd /usr/local/redis
cp -r redis01 redis02
8、将redis02文件文件夹中的redis.conf进行修改
port 6378 #redis的端口 daemonize yes #后台启动redis
9、分别启动,即可获取两个Redis.并查看
ps -ef | grep redis
(2)准备Tomcat服务器
1、上传tomcat的安装包,apache-tomcat-8.5.56.tar.gz
2、将安装包进行解压缩
tar -zxf apache-tomcat-8.5.56.tar.gz
3、进入tomcat的bin目录
cd apache-tomcat-8.5.56/bin ./startup
nginx.conf配置
stream { upstream redisbackend { server 192.168.200.146:6379; server 192.168.200.146:6378; } upstream tomcatbackend { server 192.168.200.146:8080; } server { listen 81; proxy_pass redisbackend; } server { listen 82; proxy_pass tomcatbackend; } }
访问测试
nginx缓存集成
缓存的概念
| 场景 | 作用 |
| 操作系统磁盘缓存 | 减少磁盘机械操作 |
| 数据库缓存 | 减少文件系统的IO操作 |
| 应用程序缓存 | 减少对数据库的查询 |
| Web服务器缓存 | 减少对应用服务器请求次数 |
| 浏览器缓存 | 减少与后台的交互次数 |
Nginx缓存设置的相关指令
proxy_cache_path
| 语法 | proxy_cache_path path [levels=number] keys_zone=zone_name:zone_size [inactive=time][max_size=size]; |
| 默认值 | - |
| 位置 | http |
/usr/local/proxy_cache
levels=1:2 缓存空间有两层目录,第一次是1个字母,第二次是2个 字母 举例说明: itheima[key]通过MD5加密以后的值为 43c8233266edce38c2c9af0694e2107d levels=1:2 最终的存储路径为/usr/local/proxy_cache/d/07 levels=2:1:2 最终的存储路径 为/usr/local/proxy_cache/7d/0/21 levels=2:2:2 最终的存储路径 为??/usr/local/proxy_cache/7d/10/e2
keys_zone=itcast:200m 缓存区的名称是itcast,大小为200M,1M 大概能存储8000个keys
inactive=1d 缓存数据在1天内没有被访问就会被删除
max_size=20g
http{ proxy_cache_path /usr/local/proxy_cache keys_zone=itcast:200m levels=1:2:1 inactive=1d max_size=20g;
}
proxy_cache
| 语法 | proxy_cache zone_name|off; |
| 默认值 | proxy_cache off; |
| 位置 | http、server、location |
zone_name:指定使用缓存区的名称
proxy_cache_key
| 语法 | proxy_cache_key key; |
| 默认值 | proxy_cache_key $scheme$proxy_host$request_uri; |
| 位置 | http、server、location |
proxy_cache_valid
| 语法 | proxy_cache_valid [code] time; |
| 默认值 | - |
| 位置 | http、server、location |
proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; 为200和302的响应URL设置10分钟缓存,为404的响应URL设置1分钟缓存proxy_cache_valid any 1m; 对所有响应状态码的URL都设置1分钟缓存
proxy_cache_min_uses
| 语法 | proxy_cache_min_uses number; |
| 默认值 | proxy_cache_min_uses 1; |
| 位置 | http、server、location |
proxy_cache_methods
| 语法 | proxy_cache_methods GET|HEAD|POST; |
| 默认值 | proxy_cache_methods GET HEAD; |
| 位置 | http、server、location |
nginx缓存设置

步骤实现
http://192.168.200.146:8080/js/jquery.js
Nginx的环境准备
(1)完成Nginx反向代理配置
http{ upstream backend{ server 192.168.200.146:8080; } server { listen 8080; server_name localhost; location / { proxy_pass http://backend/js/; } } }
(2)完成Nginx缓存配置
添加缓存配置
http{ proxy_cache_path /usr/local/proxy_cache levels=2:1 keys_zone=itcast:200m inactive=1d max_size=20g; upstream backend{ server 192.168.200.146:8080; } server { listen 8080; server_name localhost; location / { proxy_cache itcast; proxy_cache_key itheima; proxy_cache_min_uses 5; proxy_cache_valid 200 5d; proxy_cache_valid 404 30s; proxy_cache_valid any 1m; add_header nginx-cache "$upstream_cache_status"; proxy_pass http://backend/js/; } } }
Nginx缓存的清除
rm -rf /usr/local/proxy_cache/......
ngx_cache_purge-2.3.tar.gz
(2)对资源文件进行解压缩
tar -zxf ngx_cache_purge-2.3.tar.gz
(3)修改文件夹名称,方便后期配置
mv ngx_cache_purge-2.3 purge
(4)查询Nginx的配置参数
nginx -V
./configure --add-module=/root/nginx/module/purge
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold
(8)将编译后的objs中的nginx拷贝到nginx的sbin目录下
cp objs/nginx /usr/local/nginx/sbin
make upgrade
(10)在nginx配置文件中进行如下配置
server{ location ~/purge(/.*) { proxy_cache_purge itcast itheima; } }
Nginx设置资源不缓存
| 语法 | proxy_no_cache string ...; |
| 默认值 | - |
| 位置 | http、server、location |
配置实例
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
proxy_cache_bypass
| 语法 | proxy_cache_bypass string ...; |
| 默认值 | - |
| 位置 | http、server、location |
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
$cookie_nocache 指的是当前请求的cookie中键的名称为nocache对应的值
$arg_nocache和$arg_comment 指的是当前请求的参数中属性名为nocache和comment对应的属性值
案例:
log_format params $cookie_nocache | $arg_nocache | $arg_comment; server{ listen 8081; server_name localhost; location /{ access_log logs/access_params.log params; add_header Set-Cookie 'nocache=999'; root html; index index.html; } }
设置不缓存资源的配置方案
server{ listen 8080; server_name localhost; location / { if ($request_uri ~ /.*\.js$){ set $nocache 1; } proxy_no_cache $nocache $cookie_nocache $arg_nocache $arg_comment; proxy_cache_bypass $nocache $cookie_nocache $arg_nocache $arg_comment; } }