Nginx调度器|Nginx调度器 Nginx优化

一、Nginx调度器什么是代理服务器

  • Nginx除了可以做web服务器(网站)之外还可以做反向代理服务器
  • 代理服务器的作用相当于代理人相当于中介
代理服务器的过程
当客户端访问代理服务器要页面的时候,代理服务器没有页面没关系,代理服务器帮你访问服务器,由后台服务器把访问结果交回代理服务器,再由代理服务器把结果传回给客户端.这个过程叫做代理服务器
即可以让外网的用户访问到内网的服务器
代理服务器也叫调度器
调度器的作用:负载均衡 健康检查
  • 负载均衡:将客户端的并发访问量均衡的分配给后台服务器.
  • 对于高并发量来说,负载均衡就显得重要
  • 健康检查: 时不时的检查后台服务器的健康状态.一旦发现有一台服务器坏了就不会把客户端的请求交给它.什么时候这台服务器修好了就自动的恢复访问
  • Nginx目前支持的调度器
  • 7层代理(OSI) 应用层(http网站代理)
  • 4层代理(TCP/UDP) 传输层(传输协议代理)
Nginx调度算法
Nginx目前支持的调度算法
  • 轮询(roundrobin) : 逐一循环调度(默认的)
  • weight(weight roundrobin) : 指定轮询几率,权重值和访问比率成正比
  • ip_hash : 根据客户端IP分配固定的后端服务器
服务器组主机状态
状态类型
  • down : 表示当前server暂时不参与负载
  • max_fils : 允许请求失败的次数(默认为1)
  • fail_timeout : max_fauls次失败后,暂停提供服务的时间
HTTP调度
Nginx反向代理架构
  • 实验拓扑
Nginx调度器|Nginx调度器 Nginx优化
文章图片

Nginx反向代理
问题
  • 后端Web服务器两台,可以使用httpd实现
  • Nginx采用轮询的方式调用后端Web服务器
  • 两台Web服务器的权重要求设置为不同的值
  • 最大失败次数为1,失败超时时间为30秒
方案
使用4台RHEL7虚拟机,其中一台作为Nginx代理服务器,该服务器需要配置两块网卡,
IP地址分别为192.168.4.5和192.168.2.5,两台Web服务器IP地址分别为192.168.2.100和192.168.2.200。
客户端测试主机IP地址为192.168.4.10。如图-1所示
Nginx调度器|Nginx调度器 Nginx优化
文章图片

图-1
部署7层代理(OSI)的Nginx服务器
步骤一:
1)部署后端Web1和Web2服务器
后端Web服务器可以简单使用yum方式安装httpd实现Web服务,为了可以看出后端服务器的不同,可以将两台后端服务器的首页文档内容设置为不同的内容。
[root@web1 ~]# yum-yinstallhttpd [root@web1 ~]# echo "192.168.2.100" > /var/www/html/index.html [root@web1 ~]# systemctl restart httpd [root@web2 ~]# yum-yinstallhttpd [root@web2 ~]# echo "192.168.2.100" > /var/www/html/index.html [root@web2 ~]# systemctl restart httpd

步骤二:
配置Nginx服务器,添加服务器池,实现反向代理功能
1)修改/usr/local/nginx/conf/nginx.conf配置文件
轮询: [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 34upstream webserver {##定义集群为webserver 35server 192.168.2.100:80; ##不写网页的端口默认访问80,其实两个都可以不写. 36server 192.168.2.200; 37} upstream :##使用upstream定义后端集群,集群名称任意 server :##使用server定义集群中的具体服务器和端口 46location / { 47proxy_pass http://webserver; ##调用集群webserver 48roothtml; 49indexindex.html index.htm; 50} proxy_pass : ##proxy_pass将用户的请求转发给webserver集群 [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.200 出现的效果为轮询效果. 调度器带健康检查功能,当web服务器其中一台坏掉了,就会自动停止访问 下面以web1作为示例: [root@web1 ~]# systemctl stop httpd [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 由于实验中只用了两台web,所以停止web1服务,客户端还是能正常访问,没有任何感觉.

配置upstream服务器集群池属性
1)设置失败次数,超时时间,权重
weight可以设置后台服务器的权重,max_fails可以设置后台服务器的失败次数,fail_timeout可以设置后台服务器的失败超时时间。
权重:一般根据服务器的性能来设置 [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 34upstream webserver { 35server 192.168.2.100:80 weight=2; (##设置web1的权重为2) 36server 192.168.2.200:80; 37} weight : ##加权轮询 权重值默认为1 [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.200 出现的效果为加权轮询效果. 失败次数,超时时间: [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 34upstream webserver { 35server 192.168.2.100:80 max_fails=1 fail_timeout=30; 36server 192.168.2.200:80; 37} #max_fails设置最大失败次数,测试服务器几次才确认服务器失败 #fail_timeout设置失败超时时间,单位为秒,一旦访问失败,30秒之内不在做健康检查 #down标记服务器已关机,不参与集群调度 [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@web1 ~]# systemctl stop httpd##30秒内模拟web1服务器坏了 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@web1 ~]# systemctl start httpd##30秒内模拟web1服务器又好了 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 为什么还是200的原因: 是nginx自带的健康检查检测到web1一旦访问失败就30秒之内不在对它进行健康检查. [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.100 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.100 30秒后再对它进行健康检查,发现web1好了又能访问了

配置upstream服务器集群的调度算法
1)设置相同客户端访问相同Web服务器
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 34upstream webserver { 35ip_hash; 36server 192.168.2.100:80 max_fails=1 fail_timeout=30; 37server 192.168.2.200:80; 38} ip_hash : #通过ip_hash设置调度规则为:相同客户端访问相同服务器 [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 [root@client ~]# curl 192.168.4.5 192.168.2.200 根据IP前几位%后端服务器分配服务器

Nginx的TCP/UDP调度器
模块
  • ngx_stream_core_module模块
  • 使用==–with-stream==开启该模块
  • 注意:
  • nginx从1.9版本才开始支持该功能
语法格式
stream { upstream backend { server backend1.example.com:12345weiht=5; server 127.1.1.1:22 max_fails=3 fail_timeout=30s; } server { listen 12345; proxy_pass backend; } } http{ ...... }

注意事项:做四层代理修改配置文件一定要在在http{}外面配置
问题:
使用Nginx实现TCP/UDP调度器功能,实现如下功能:
后端SSH服务器两台
Nginx编译安装时需要使用–with-stream,开启ngx_stream_core_module模块
Nginx采用轮询的方式调用后端SSH服务器
如图:
Nginx调度器|Nginx调度器 Nginx优化
文章图片

部署支持4层TCP/UDP代理的Nginx服务器
1)部署nginx服务器
编译安装必须要使用–with-stream参数开启4层代理模块。
注意:以下两个步骤是基于nginx已安装但是未安装支持四层代理模块--with-stream,所以需要卸载重装. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop [root@proxy ~]# rm -rf /usr/local/nginx -------------------------------------------------------------------------------- 重新安装nginx: [root@proxy sbin]# cd /opt/nginx-1.12.2/ [root@proxy nginx-1.12.2]# ./configure\ > --with-http_ssl_module \##开启SSL加密功能 > --with-stream##开启4层反向代理功能 [root@proxy nginx-1.12.2]# make && make install [root@proxy ~]# /usr/local/nginx/sbin/nginx [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 16 stream { 17upstream backend { 18server 192.168.2.100:22; ##后端SSH服务器的IP和端口 19server 192.168.2.200:22; 20} 21server { 22listen 12345; ##只要没有人用的端口就行,Nginx监听的端口 23proxy_pass backend; 24} 25 } [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@client ~]# ssh 192.168.4.5 -p 12345##-p:指定端口号 Warning: Permanently added '[192.168.4.5]:12345' (ECDSA) to the list of known hosts. root@192.168.4.5's password: [root@web1 ~]# [root@web1 ~]# exit 登出 Connection to 192.168.4.5 closed. [root@client ~]# ssh 192.168.4.5 -p 12345 root@192.168.4.5's password: [root@web2 ~]# 出现轮询效果

端口冲突问题
  • 网络原则: IP地址唯一对应一台主机,端口唯一对应一个服务
  • 当两个服务都想霸占一个端口时会产生冲突
二、Nginx优化
【Nginx调度器|Nginx调度器 Nginx优化】Nginx常见问题处理
如何自定义返回给客户端的404错误页面
如何查看服务器状态信息
如果客户端访问服务器提示“Too many open files”如何解决
如何解决客户端访问头部信息过长的问题
如何让客户端浏览器缓存数据
客户机访问此Web服务器验证效果:
使用ab压力测试软件测试并发量
编写测试脚本生成长头部信息的访问请求
客户端访问不存在的页面,测试404错误页面是否重定向
HTTP错误代状态码
状态码功能描述
100请求已接收,客户端可以继续发送请求
101Switching Protocals 服务器根据客户端的请求切换协议
200一切正常
201服务器已经创建了文档
202已经接收了请求,但处理尚未完成
203文档正常返回,但一些头部信心可能不正确
300客户端请求的资源可以在多个位置找到
301客户端请求的资源可以在其他位置找到. 永久重定向
302临时重定向
305使用代理服务
400请求语法错误
401用户或密码错误. 访问被拒绝
401.1登录失败
403资源不可用. 禁止访问(客户端IP地址被拒绝)
406.6IP地址被拒绝
403.9用户数过多
404无法找到指定资源. 文件不存在
406指定资源已找到,但MIME类型与客户端要求不兼容
407要求进行代理身份验证
414请求URI头部过长
500服务器内部错误
501服务器不支持客户端的请求功能
502Bad Gateway 网关错误,一般是做了集群,找不着后台服务器,服务器作为网关或者代理时,为了完成请求访问下一个服务器,当该服务器返回了非法应答
503服务不可用
504网关超时,服务器处于维护或者负载过高无法响应
505服务器不支持客户端请求的HTTP版本
返回状态码规律
  • 200开头代表一切正常
  • 300开头代表重定向
  • 400开头代表客户端问题
  • 500开头代表服务器内部问题
自定义报错页面
1)优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到
[root@proxy ~]# vim /usr/local/nginx/sbin/conf/nginx.conf 49charset utf-8; 58error_page404/404.html; [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@proxy ~]# vim /usr/local/nginx/html/404.html 一首凉凉送给你!!!!! [student@room9pc01 ~]$ firefox 192.168.4.5/xxxx 注意: 可以是自定义图片或者是其他的.

测试效果:
Nginx调度器|Nginx调度器 Nginx优化
文章图片

Nginx状态页面
status模块
  • –with-http_staub_status_module开启模块功能
  • 可以查看Nginx连接数等信息
步骤二:如何查看服务器状态信息(非常重要的功能)
1)编译安装时使用–with-http_stub_status_module开启状态页面模块
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop [root@proxy ~]# rm -rf /usr/local/nginx/ -------------------------------------------------------------------------------- [root@proxy ~]# cd /opt/nginx-1.12.2/ [root@proxy nginx-1.12.2]# ./configure \ > --with-http_ssl_module \ > --with-stream\ > --with-http_stub_status_module [root@proxy nginx-1.12.2]# make && make install [root@proxy nginx-1.12.2]# /usr/local/nginx/sbin/ngi代码段 小部件nx

2)启用Nginx服务并查看监听端口状态
ss命令可以查看系统中启动的端口信息,该命令常用选项如下: -a显示所有端口的信息 -n以数字格式显示端口号 -t显示TCP连接的端口 -u显示UDP连接的端口 -l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口代码段 小部件 -p显示监听端口的服务名称是什么(也就是程序名称) 注意:在RHEL7系统中可以使用ss命令替代netstat命令,功能一样,选项一样。

3 ) 修改Nginx配置文件,定义状态页面
[root@proxy ~]# vim /usr/local/nginx/sbin/conf/nginx.conf 35server { 36listen80; 37server_namelocalhost; 38location /status { 39stub_status on; #allow IP地址; ##允许哪个IP地址查看页面状态 #deny IP地址; ##拒绝哪个IP地址查看页面状态 40} [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [student@room9pc01 ~]$ firefox 192.168.4.5/status##打开状态页面

页面效果:
Nginx调度器|Nginx调度器 Nginx优化
文章图片

Active connections: 当前活动的连接数量。 当前实时并发量,并发量高的时候会增加,并发量低的时候会减少 server accepts handled requests: 服务端已经接受客户端连接总数和已经处理客户端的连接总数 数量只会增高. 重启nginx的时候会重置为0. 第一个数字代表客户端发送多少次请求连接 第二个数字代表服务端处理了多少次客户端的请求 第三个数字代表网页点击量,刷新一次要一次页面 Reading:Writing:Waiting: Reading : 代表当前服务器读取客户端请求数据包有几个 Writing : 代表当前服务器正在写数据包有几个 Waiting : 代表有多少个客户端在等待服务器的响应。

优化Nginx并发量
1)优化前使用ab高并发测试
ab压力测试软件: -c: 人 -n: 总访问次数 [root@proxy ~]# ab -c100 -n 100 http://192.168.4.5/ 不优化的结果: [root@proxy ~]# ab -c1024 -n 1024 http://192.168.4.5/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.4.5 (be patient) socket: Too many open files (24)

2)修改Nginx配置文件,增加并发量
[root@proxy ~]# vim /usr/local/nginx/sbin/conf/nginx.conf 3 worker_processes2; ##与CPU核心数量一致 12 events { 13worker_connections6000; ##每个worker最大并发连接数,理论值5万 14 } 这两行的两个参数与并发量有关系 端口号最大数: 65535 [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@proxy ~]# ab -c1024 -n 1024 http://192.168.4.5/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.4.5 (be patient) socket: Too many open files (24) [root@proxy ~]# ab -c1024 -n 1024 http://192.168.4.5/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.4.5 (be patient) socket: Too many open files (24) ##还是报错,还有参数未改

3)优化Linux内核参数(最大文件数量)
在Linux中最大文件数量为1024
[root@proxy ~]# ulimit -a##查看所有属性值 core file size(blocks, -c) 0 data seg size(kbytes, -d) unlimited scheduling priority(-e) 0 file size(blocks, -f) unlimited pending signals(-i) 5569 max locked memory(kbytes, -l) 64 max memory size(kbytes, -m) unlimited open files(-n) 1024 pipe size(512 bytes, -p) 8 POSIX message queues(bytes, -q) 819200 real-time priority(-r) 0 stack size(kbytes, -s) 8192 cpu time(seconds, -t) unlimited max user processes(-u) 5569 virtual memory(kbytes, -v) unlimited file locks(-x) unlimited ##open files : Linux中最多打开1024个文件 ##max user processes : 最多程序5569 [root@proxy ~]# ulimit -Hn 100000##设置硬限制(临时规则) [root@proxy ~]# ulimit -Sn 100000##警告值,设置软限制(临时规则) 永久配置文件: [root@proxy ~]# vim /etc/security/limits.conf .. .. *softnofile100000 *hardnofile100000 #该配置文件分4列,分别如下: #用户或组硬限制或软限制需要限制的项目限制的值 [root@proxy ~]# ab -c1024 -n 1024 http://192.168.4.5/##能轻松访问 Percentage of the requests served within a certain time (ms) 50%33 66%38 75%43 80%46 90%55 95%61 98%66 99%67 100%68 (longest request) [root@proxy ~]# ab -c10000 -n 10000 http://192.168.4.5/ ##轻松访问 Percentage of the requests served within a certain time (ms) 50%4 66%6 75%10 80%129 90%140 95%146 98%162 99%485 100%514 (longest request) [root@proxy ~]# ab -c20000 -n 20000 http://192.168.4.5/##接近2万 This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.4.5 (be patient) Completed 2000 requests Completed 4000 requests Completed 6000 requests Completed 8000 requests Completed 10000 requests Completed 12000 requests Completed 14000 requests Completed 16000 requests Completed 18000 requests apr_socket_recv: Connection reset by peer (104) Total of 19300 requests completed

优化Nginx数据包头缓存
1)优化前,使用脚本测试长头部请求是否能获得响应
[root@proxy ~]# ls /opt/lnmp_soft/buffer.sh /opt/lnmp_soft/buffer.sh [root@proxy ~]# cd /opt/lnmp_soft/ [root@proxy lnmp_soft]# ./buffer.sh 414 Request-URI Too Large - 锐客网##出现414报错 414 Request-URI Too Large##提示URI地址栏头部信息过大 nginx/1.12.2

2)修改Nginx配置文件,增加数据包头部缓存大小
root@proxy ~]# vim /usr/local/nginx/sbin/conf/nginx.conf client_header_buffer_size1k; ##默认请求包头信息的缓存 large_client_header_buffers4 4m; ##4个4m 大请求包头部信息的缓存个数与容量 [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3)优化后,使用脚本测试长头部请求是否能获得响应
[root@proxy lnmp_soft]# ./buffer.sh Welcome to nginx! - 锐客网body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } Welcome to nginx!If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

浏览本地缓存静态数据
1)使用Firefox浏览器查看缓存
以Firefox浏览器为例,在Firefox地址栏内输入about:cache将显示Firefox浏览器的缓存信息
[student@room9pc01 ~]$ firefox about:cache

Nginx调度器|Nginx调度器 Nginx优化
文章图片

disk (目前缓存的信息有多少) Number of entries: 缓存数 Maximum storage size: 最大缓存 Storage in use: 缓存大小 Storage disk location: 缓存路径 List Cache Entries 查看曾经缓存的资料

2)清空firefox本地缓存数据,如图所示。
Nginx调度器|Nginx调度器 Nginx优化
文章图片

3)修改Nginx配置文件,定义对静态页面的缓存时间
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 37server { 38listen80; 39server_namelocalhost; 40location ~ \.(jpg|png|flv)$ { 41expires30d; ##定义客户端缓存时间为30天 42} ##如果用户访问jpg图片,那么帮我缓存30天,30天之内不要访问我. [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload [root@proxy ~]# cp /usr/share/backgrounds/day.jpg/usr/local/nginx/html [root@proxy lnmp_soft]# firefox 192.168.4.5/day.jpg [student@room9pc01 ~]$ firefox about:cache

缓存信息: (点击List Cache Entries查看)
Nginx调度器|Nginx调度器 Nginx优化
文章图片

    推荐阅读