nginx的负载介绍
指定上游服务器的upstream与server指令
指令介绍
Syntax: upstream name { ... }
Default: —
Context: http
Syntax: server address [parameters];
Default: —
Context: upstream
功能介绍:指定一组上游服务器地址,其中,可以是域名、IP地址或者Unix socket地址。可以在域名或者IP地址后面加端口,如果不加,默认80
参数介绍:backup 指定当前server为备份服务,当非备份server不可用时,请求才会转发到server里;down 标识某台服务以及下线,不在服务
加权轮Round-Robin算法:
功能:在加权轮讯的方式访问server指令指定上游服务。集成在nginx的upstream框架
指令:weight 服务访问的权重,默认是1;
max_conns:server 的最大并发连接数,仅做用于单worker进程。默认0 ,表示没有限制;
max_falis:在fail_timeout时间内,最大的失败次数。当达到最大失败是,会在fail_timeout秒内这台server不允许再次被选择;
fail_timeout :单位为秒,默认10 秒,具有两个功能指定一段时间内,最大的失败次数max_fails;达到max_fails后,该server不能访问的时间
对上游服务使用长连接
功能:通过复用连接,降低nginx与上游服务器简历关闭连接的消耗,提升吞吐量的同时降低延迟
模块:ngx_http_upstream_keepalive_module 模块;默认编译进nginx的通过--without-http_upstream_keepalive_module 可移除此模块
对上游连接的http头部设定
proxy_http_version 1.1; #因为http1.0不支持长连接;防止下游服务器传过来的是http1.0的协议,这边需要对它的协议重置
proxy_set_header Connection "";#
upstream_keepalive指令介绍
Syntax: keepalive connections; 最多保持多少空闲连接
Default: —
Context: upstream
Syntax: keepalive_requests number;
Default: keepalive_requests 100; 一条连接上最多能有多少请求
Context: upstream
Syntax: keepalive_timeout timeout;
Default: keepalive_timeout 60s; 如果上一条请求请求完最多等多长时间,没有连接的话断开请求
Context: upstream
指定上游服务域名解析的resolver指令
Syntax: resolver address ... [valid=time] [ipv6=on|off]; 当我们使用域名访问的时候可以指定一个dns服务;设置访问dns访问超时时间
Default: —
Context: http, server, location
Syntax: resolver_timeout time;
Default: resolver_timeout 30s;
Context: http, server, location
配置
[root@python vhast]# cat shangyou.conf
server {
listen 8011;
default_type text/plain;
return 200 '8011 server response.\n';
} server {
listen 8012;
default_type text/plain;
return 200 '8012 server response.\n';
}
[root@python vhast]# cat upstream.conf
upstream rrups{
server 127.0.0.1:8011 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
server 127.0.0.1:8012;
keepalive 32;
}
server {
server_name rrups.com;
error_log rrups_error.log info;
location /{
proxy_pass http://rrups;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
测试
[root@python vhast]# curl rrups.com
8011 server response.
[root@python vhast]# curl rrups.com
8011 server response.
[root@python vhast]# curl rrups.com
8012 server response.
[root@python vhast]# curl rrups.com
8011 server response.
测试场链接;并没有断开
[root@python vhast]# tcpdump -i lo port 8011
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
15:28:37.168360 IP localhost.40308 > localhost.8011: Flags [S], seq 2102663491, win 43690, options [mss 65495,sackOK,TS val 259
080435 ecr 0,nop,wscale 7], length 015:28:37.168418 IP localhost.8011 > localhost.40308: Flags [S.], seq 127783339, ack 2102663492, win 43690, options [mss 65495,s
ackOK,TS val 259080435 ecr 259080435,nop,wscale 7], length 015:28:37.168454 IP localhost.40308 > localhost.8011: Flags [.], ack 1, win 342, options [nop,nop,TS val 259080435 ecr 259080435
], length 015:28:37.168540 IP localhost.40308 > localhost.8011: Flags [P.], seq 1:70, ack 1, win 342, options [nop,nop,TS val 259080435 ec
r 259080435], length 6915:28:37.168562 IP localhost.8011 > localhost.40308: Flags [.], ack 70, win 342, options [nop,nop,TS val 259080435 ecr 25908043
5], length 015:28:37.168678 IP localhost.8011 > localhost.40308: Flags [P.], seq 1:171, ack 70, win 342, options [nop,nop,TS val 259080435
ecr 259080435], length 17015:28:37.168700 IP localhost.40308 > localhost.8011: Flags [.], ack 171, win 350, options [nop,nop,TS val 259080435 ecr 2590804
35], length 015:29:05.936003 IP localhost.40308 > localhost.8011: Flags [P.], seq 70:139, ack 171, win 350, options [nop,nop,TS val 25910920
3 ecr 259080435], length 6915:29:05.936305 IP localhost.8011 > localhost.40308: Flags [P.], seq 171:341, ack 139, win 342, options [nop,nop,TS val 2591092
03 ecr 259109203], length 17015:29:05.936358 IP localhost.40308 > localhost.8011: Flags [.], ack 341, win 359, options [nop,nop,TS val 259109203 ecr 2591092
03], length 0
超时后;断开
15:30:05.944449 IP localhost.40308 > localhost.8011: Flags [F.], seq 139, ack 341, win 359, options [nop,nop,TS val 259169211 e
cr 259109203], length 015:30:05.944588 IP localhost.8011 > localhost.40308: Flags [F.], seq 341, ack 140, win 342, options [nop,nop,TS val 259169212 e
cr 259169211], length 0
基于客户端地址的IPhash算法与
配置
[root@python vhast]# cat upstream.conf
upstream rrups{
ip_hash;
server 127.0.0.1:8011 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
server 127.0.0.1:8012;
#keepalive 32;
}
server {
set_real_ip_from 192.168.183.4; #可信地址real,使用--with-http_realip_module 开启她;默认nginx未编译
real_ip_recursive on; #启用此模块;
real_ip_header X-Forwarded-For; #表示X-Forwarded-For变量的值替换real_ip原来的值
server_name rrups.com;
error_log rrups_error.log info;
location /{
proxy_pass http://rrups;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
测试
[root@python vhast]# curl -H 'X-Forwarded-For:12.1.45.53' rrups.com
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For: 12.100.45.53' rrups.com
8011 server response.
[root@python vhast]# curl -H 'X-Forwarded-For:12.1.45.53' rrups.com
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For:12.1.45.56' rrups.com
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For:11.8.45.56' rrups.com
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For:100.8.45.56' rrups.com
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For:100.9.67.51' rrups.com
8011 server response.
基于参数做hash
[root@python vhast]# cat upstream.conf
upstream rrups{
#ip_hash;
hash user_$arg_username; 用户名
server 127.0.0.1:8011;
server 127.0.0.1:8012;
#keepalive 32;
}
server {
set_real_ip_from 192.168.183.4;
real_ip_recursive on;
real_ip_header X-Forwarded-For;
server_name rrups.com;
error_log rrups_error.log info;
location /{
proxy_pass http://rrups;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
测试
[root@python vhast]# curl -H 'X-Forwarded-For: 100.8.45.56' rrups.com?username=chenxi
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For: 100.8.45.56' rrups.com?username=chenxi-990
8011 server response.
[root@python vhast]# curl -H 'X-Forwarded-For: 100.8.45.56' rrups.com?username=chenxi
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For: 100.8.45.56' rrups.com?username=chenxiuughgh
8012 server response.
[root@python vhast]# curl -H 'X-Forwarded-For: 100.8.45.56' rrups.com?username=chenxiuughgh-09876
8011 server response.
一致性hash
Syntax: hash key [consistent];
Default: —
Context: upstream
最少连接算法
功能模块:ngx_http_upstream_ least_conn _module;默认编译进nginx里
指令介绍
Syntax: least_conn;
Default: —
Context: upstream
使用共享内存让负载均衡算法跨work进程生效
功能模块:ngx_http_upstream_ zone _module;默认启用
指令介绍
Syntax: zone name [size]; 名字:共享内存多大
Default: —
Context: upstream
upstream 提供一些变量介绍(先不包含缓存的变量)
upstream_addr :上游服务器的IP为可读的127.0.0.1:8012
upstream_connect_time: 与上游服务建立连接消耗的时间,单位为秒。可精确到毫秒
upstream_header_time:接收上游服务器发送http头部响应的时间,单位为秒。可精确到毫秒
upstream_response_time:接收完上游服务器所有响应消耗的时间,单位为秒。精确到毫秒
upstream_http_名称:从上游服务器返回的响应头部的值
upstream_bytes_received:从上游服务器接收到响应长度,默认为字节
upstream_response_length:从上游服务器返回的包体的长度。默认字节
upstream_status:从上游服务器返回的http状态码;如果未连接502
upstream_cookie_名称:从上游服务器发回的Set-Cookie中取出cookie值
upstream_trailer_名称:从上游服务响应尾部取到的值
nginx的负载介绍的更多相关文章
- Nginx简单介绍以及linux下使用Nginx进行负载均衡的搭建
1.Nginx简介 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5 ...
- [转载] nginx的负载均衡
原文:http://www.srhang.me/blog/2014/08/27/nginx-loabbalance/ Nginx负载均衡 一.特点 1.1 应用情况 Nginx做为一个强大的Web服务 ...
- Nginx之负载均衡
转自:http://www.360doc.com/content/13/1114/12/7694408_329125489.shtml 注,大家可以看到,由于我们网站是发展初期,nginx只代理了后端 ...
- linux+asp.net core+nginx四层负载均衡
Linux Disibutaion:Ubuntu 16.04.1 LTS Web Server:Nginx.Kestrel 关于如何在linux中部署asp.net core我这里不再详细介绍,可以参 ...
- [转]CENTOS 6.5 配置YUM安装NGINX+服务器负载均衡
原文连接: CENTOS 6.5 配置YUM安装NGINX http://blog.sina.com.cn/s/blog_69f467b70102uyux.html 参考博文: Centos下安装. ...
- 架构之路 之 Nginx实现负载均衡
[前言] 在大型网站中,负载均衡是有想当必要的.尤其是在同一时间访问量比较大的大型网站,例如网上商城,新闻等CMS系统,为了减轻单个服务器的处理压力,我们引进了负载均衡这一个概念,将一个服务器的压力分 ...
- 利用nginx实现负载均衡和动静分离
1.Nginx介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 . Nginx 是由 ...
- 6.Nginx作为负载均衡服务器应用
案例:Nginx作为负载均衡服务器应用 nginx的负载均衡功能是通过upstream命令实现的,因此他的负载均衡机制比较简单,是一个基于内容和应用的7层交换负载均衡的实现.Nginx负载均衡默认对后 ...
- nginx的负载均衡配置,常用策略
场景:nginx是一款非常优秀的负载均衡服务器,小巧而且性能强悍,中小型企业的首选. 下面介绍nginx的负载均衡的几种常见的配置以及优缺点 第一种:轮询(默认) 优点:实现简单 缺点:不考虑每台服务 ...
随机推荐
- maven的安装与使用(运行单元测试和打包等)
maven的下载与安装 maven是用于java的自动化构建工具. 1.下载: http://maven.apache.org/download.cgi 下载maven包,比如 apache-mave ...
- Linux centos7 shell 介绍、 命令历史、命令补全和别名、通配符、输入输出重定向
一.shell介绍 shell脚本是日常Linux系统管理工作中必不可少的,不会shell,就不是一个合格管理员. shell是系统跟计算机硬件交互使用的中间介质,一个系统工具.实际上在shell和计 ...
- vue去掉地址栏# 方法
超简单 export default new Router({ //将mode 设置为‘history‘ 即可.默认情况是’hash’ 所以会有丑陋的# mode: 'history', routes ...
- 8.5-Day1T1--Asm.Def 谈笑风生
题目大意 m个操作, 1:添加一个字符串 2:查询字符串s是否被添加过(中至多包含一个通配符“*”) 题解 trie树可以得部分分 用map映射 '*'就枚举26个英文字母来判断就可以了 #inclu ...
- Ubuntu 国内安装 kubernetes
由于墙的原因,国内要安装 kubernetes 非常的麻烦,因此只要解决这个问题,就可以顺利安装 kubernetes 的 三个官法工具 kubelet.kubeadm.kubectl. 安装环境: ...
- UIButton按钮的高亮状态颜色
首先是adjustsImageWhenHighlighted属性的正确使用: UIButton的adjustsImageWhenHighlighted属性是当UIButton设置了背景图片时,并且没有 ...
- 喵星之旅-狂奔的兔子-myeclipse搭建ssm
. 可以使用试用期限内的myeclipse,也可以找到有授权的机器进行操作.搭建好的项目框架可以直接移植到免费软件eclipse使用.或者直接购买myeclipse授权. 一.创建一个java web ...
- Raid 10配置流程(五块磁盘)
Raid 10配置流程(五块磁盘) 1.在虚拟机中再添加5块硬盘. 2.使用mdadm命令创建raid10,名称为“/dev/md0” -C代表创建操作,-v显示创建过程,-a yes检查RAID名 ...
- JupyterLab远程访问配置方法(CentOS7)
下载 Anaconda3安装包,并执行安装命令: bash Anaconda3-2019.07-Linux-x86_64.sh 确定安装并初始化: Do you wish the installer ...
- devexpress layoutview
1.设定数据源 2.设置view 3.设置 templat cardview 4 显示