Web服务器 什么是web服务 web就是B/S架构
Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,可以处理浏览器等Web客户端的请求并返回相应响应,也可以放置网站文件,让全世界浏览;可以放置数据文件,让全世界下载。
WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。WWW 是 Internet 的多媒体信息查询工具,是 Internet 上近年才发展起来的服务,也是发展最快和目前用的最广泛的服务。正是因为有了WWW工具,才使得近年来 Internet 迅速发展,且用户数量飞速增长。
网络模型(IO多路复用之select、poll、epoll)
epoll跟select都能提供多路I/O复用的解决方案。在现在的Linux内核里有都能够支持,其中epoll是Linux所特有,而select则应该是POSIX所规定
,一般操作系统均有实现。
1 2 3 4 5 6 7 8 9 10 11 12 select - 基本原理:select 函数监视的文件描述符分3类,分别是writefds、readfds、和exceptfds。调用后select函数会阻塞,直到有描述符就绪(有数据 可读、可写、或者有except),或者超时(timeout指定等待时间,如果立即返回设为null即可),函数返回。当select函数返回后,可以通过遍历fdset,来找到就绪的描述符。 - select目前几乎在所有的平台上支持,`其良好跨平台支持也是它的一个优点`。`select的一个缺点在于单个进程能够监视的文件描述符的数量存在最大限制`,在Linux上一般为1024,`可以通过修改宏定义甚至重新编译内核的方式提升这一限制`,但是这样也会造成效率的降低。 poll - *基本原理:`poll本质上和select没有区别,它将用户传入的数组拷贝到内核空间`,然后查询每个fd对应的设备状态,如果设备就绪则在设备等待队列中加入一项并继续遍历,如果遍历完所有fd后没有发现就绪设备,则挂起当前进程,直到设备就绪或者主动超时,被唤醒后它又要再次遍历fd。这个过程经历了多次无谓的遍历。 epoll - 基本原理:`epoll支持水平触发和边缘触发,最大的特点在于边缘触发,它只告诉进程哪些fd刚刚变为就绪态,并且只会通知一次`。还有一个特点是,`epoll使用“事件”的就绪通知方式`,通过epoll_ctl注册fd,`一旦该fd就绪,内核就会采用类似callback的回调机制来激活该fd`,epoll_wait便可以收到通知。 - epoll是在2.6内核中提出的,是之前的select和poll的增强版本。相对于select和poll来说,epoll更加灵活,没有描述符限制。`epoll使用一个文件描述符管理多个描述符,将用户关系的文件描述符的事件存放到内核的一个事件表中,这样在用户空间和内核空间的copy只需一次`。
参考链接:https://www.cnblogs.com/Anker/p/3265058.html
select,poll,epoll都是IO多路复用的机制。I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。但select,poll,epoll本质上都是同步I/O,因为他们都需要在读写事件就绪后自己负责进行读写,也就是说这个读写过程是阻塞的
,而异步I/O则无需自己负责进行读写,异步I/O的实现会负责把数据从内核拷贝到用户空间。关于这三种IO多路复用的用法,链接如下所示:
select:http://www.cnblogs.com/Anker/archive/2013/08/14/3258674.html
poll:http://www.cnblogs.com/Anker/archive/2013/08/15/3261006.html
epoll:http://www.cnblogs.com/Anker/archive/2013/08/17/3263780.html
Web服务器软件 1、apache
Apache(音译为阿帕奇)是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。
1 2 3 4 5 6 7 Nginx和Apache一样,都是HTTP服务器软件,在功能实现上都采用模块化结构设计,都支持通用的语言接口,如PHP、Perl、Python等,同时还支持正向和反向代理、虚拟主机、URL重写、压缩传输、SSL加密传输等。 1.在功能实现上,Apache的所有模块都支持动、静态编译,而Nginx模块都是静态编译的, 2.对FastCGI的支持,Apache对Fcgi的支持不好,而Nginx对Fcgi的支持非常好; 3.在处理连接方式上,Nginx支持epoll,而Apache却不支持; 4.在空间使用上,Nginx安装包仅仅只有几百K,和Nginx比起来Apache绝对是庞然大物。
2、Nginx
Nginx (engine x) 是一个很强大的、高性能的HTTP和反向代理web服务器,同时也提供IMAP/POP3/SMTP服务。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函数库,并且除了zlib、PCRE和OpenSSL之外,标准模块只使用系统C库函数。而且,如果不需要或者考虑到潜在的授权冲突,可以不使用这些第三方库。
1 2 3 4 5 6 7 1. 作为Web服务器: Nginx处理静态文件、索引文件,自动索引的效率非常高。 2. 作为代理服务器,Nginx可以实现无缓存的反向代理加速,提高网站运行速度。 3. 作为负载均衡服务器,Nginx既可以在内部直接支持Rails和PHP,也可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载均衡。 4. 在性能方面,Nginx是专门为性能优化而开发的,在实现上非常注重效率。它采用内核Poll模型(epoll and kqueue ),可以支持更多的并发连接,最大可以支持对50 000个并发连接数的响应,而且只占用很低的内存资源。 5. 在稳定性方面,Nginx采取了分阶段资源分配技术,使得CPU与内存的占用率非常低。Nginx官方表示,Nginx保持10 000个没有活动的连接,而这些连接只占用2.5MB内存,因此,类似DOS这样的攻击对Nginx来说基本上是没有任何作用的。 6. 在高可用性方面,Nginx支持热部署,启动速度特别迅速,因此可以在不间断服务的情况下,对软件版本或者配置进行升级,即使运行数月也无需重新启动,几乎可以做到7×24小时不间断地运行。
Nginx
安装Nginx 官网:https://nginx.org/ 软件:https://nginx.org/download/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1、yum安装 [root@web01 ~] [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [root@web01 ~] [root@web01 ~] [root@web01 ~] 2 、二进制安装 略..... 3 、编译安装 [root@web01 ~] [root@web01 ~] [root@web01 nginx-1 .20 .2 ] [root@web01 nginx-1 .20 .2 ] [root@web01 nginx-1 .20 .2 ]
平滑增加Nginx模块
众所周知Nginx是分成一个个模块的,比如core模块,gzip模块,proxy模块,每个模块负责不同的功能,除了基本的模块,有些模块可以选择编译或不编译进Nginx。官网文档中的Modules reference部分列出了nginx源码包的所有模块。我们可以按照自己服务器的需要来定制出一个最适合自己的Nginx服务器。
除了Nginx官网源码包提供了各种模块,Nginx还有各种各样的第三方模块。官方文档NGINX 3rd Party Modules也列出了Nginx的很多第三方模块,除此官网列出的之外,还有很多很有用的模块也能在Github等网站上找到。
这些模块提供着各种各样意想不到的功能,灵活使用Nginx的第三方模块,可能会有非常大的意外收获。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@web01 ~] [root@web02 ~] [root@web02 ~] [root@web02 nginx-1 .20 .2 ] [root@web02 nginx-1 .20 .2 ] [root@web02 nginx-1 .20 .2 ] [root@web01 nginx-1 .20 .2 ] [root@web02 nginx-1 .20 .2 ] [root@web02 nginx-1 .20 .2 ] [root@web02 nginx-1 .20 .2 ] [root@web02 nginx-1 .20 .2 ] [root@web01 nginx-1 .20 .2 ]
Nginx的命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 [root@web01 ~] nginx version: nginx/1 .20 .2 Usage: nginx [-?hvVtTq] [-s signal] [-p prefix] [-e filename] [-c filename] [-g directives] Options: 1 、-v : 版本信息 [root@web01 ~] nginx version: nginx/1 .20 .2 2 、-V : 版本和配置选项信息 [root@web01 ~] nginx version: nginx/1 .20 .2 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 ...... 3 、-t : 检查配置文件是否有语法错误 [root@web01 ~] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 4 、-T : 测试配置文件并启动 [root@web01 ~] nginx: the configuration file /etc/nginx/nginx.conf syntax is onginx: the configuration file /etc/nginx/nginx.conf syntax is oTk nginx: configuration file /etc/nginx/nginx.conf test is successful user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice ; pid /var/run/nginx.pid; ...... 5、-q : 打印错误日志 [root@web01 ~] nginx: [emerg] bind() to 0.0.0.0:80 failed (98 : Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98 : Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98 : Address already in use) 6 、-s : 指定Nginx启动配置(给nginx主进程发送信号) stop : 停止(快速关闭) [root@web01 ~] quit : 退出(正常关闭) [root@web01 ~] reopen : 重启 [root@web01 ~] reload : 重载 [root@web01 ~] 7 、-p : 指定nginx的安装路径(默认是:/etc/nginx/) 8 、-e : 指定错误日志路径 9 、-c : 指定配置文件的路径(默认是:/etc/nginx/nginx.conf) 10 、-g : 设置一个全局的Nginx配置项 [root@web01 ~]
Nginx配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 -------------------- 全局配置(全局生效) ----------------- user www; worker_processes auto; error_log /var/log/nginx/error.log notice ; pid /var/run/nginx.pid; -------------- 系统事件配置模块(全局生效) --------------- events { worker_connections 1024 ; use epool; } -------------- Http请求模块(处理Http请求的模块) -------------- http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' ; access_log /var/log/nginx/access.log main; sendfile on ; keepalive_timeout 65 ; include /etc/nginx/conf.d/*.conf ; } server { listen 80 ; server_name 127.0.0.1 ; location / { root /blog; index index.php; }
超级玛丽和象棋 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 1、上传代码 [root@web01 ~] [root@web01 opt] [root@web01 opt] 总用量 176 drwxr-xr-x 2 root root 329 1 月 3 21 :44 images -rw-r--r-- 1 root root 1703 1 月 3 21 :44 index.html -rw-r--r-- 1 root root 72326 1 月 3 21 :44 jquery.js -rw-r--r-- 1 root root 78982 1 月 3 21 :44 QAuIByrkL.js -rw-r--r-- 1 root root 4777 1 月 3 21 :44 VNkyVaVxUV.css -rw-r--r-- 1 root root 9539 1 月 3 21 :44 wNGu2CtEMx.js 2 、编辑配置文件[root@web01 opt] server { listen 80 ; server_name game.test.com; location / { root /opt/Super_Marie; index index.html; } } 3、测试配置文件是否正常 [root@web01 opt] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 4 、重启Nginx[root@web01 opt] 5 、Windows域名解析C:\Windows\System32\drivers\etc\hosts 172.16.1.7 game.test.com6 、Mac域名解析 sudo -i vim /etc/hosts 172.16.1.7 game.test.com
Nginx配置虚拟主机 可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
虚拟主机技术是互联网服务器采用的节省服务器硬件成本的技术,虚拟主机技术主要应用于HTTP服务,将一台服务器的某项或者全部服务内容逻辑划分为多个服务单位,对外表现为多个服务器,从而充分利用服务器硬件资源。
虚拟主机是使用特殊的软硬件技术,把一台真实的物理服务器主机分割成多个逻辑存储单元。每个逻辑单元都没有物理实体,但是每一个逻辑单元都能像真实的物理主机一样在网络上工作,具有单独的IP地址(或共享的IP地址)、独立的域名以及完整的Internet服务器(支持WWW、FTP、E-mail等)功能。
虚拟主机的关键技术在于,即使在同一台硬件、同一个操作系统上,运行着为多个用户打开的不同的服务器程式,也互不干扰。而各个用户拥有自己的一部分系统资源(IP地址、文档存储空间、内存、CPU等)。各个虚拟主机之间完全独立,在外界看来,每一台虚拟主机和一台单独的主机的表现完全相同。所以这种被虚拟化的逻辑主机被形象地称为“虚拟主机”。
基于多IP的方式
基于多端口的方式
基于多域名的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 1、基于多IP的方式 [root@web01 conf.d] [root@web01 conf.d] server { listen 80 ; server_name 192.168.15.7 ; location / { root /opt/Super_Marie; index index.html; } } server { listen 80 ; server_name 172.16.1.7 ; location / { root /opt/tank; index index.html; } } 192.168.15.7 172.16.1.7 2、基于多端口的方式 [root@web01 conf.d] [root@web01 conf.d] server { listen 80 ; server_name 192.168.15.7 ; location / { root /opt/Super_Marie; index index.html; } } server { listen 81 ; server_name 192.168.15.7 ; location / { root /opt/tank; index index.html; } } 192.168.15.7 192.168.15.7:81 3、基于多域名的方式 [root@web01 conf.d] [root@web01 conf.d] server { listen 80 ; server_name www.game.com; location / { root /opt/Super_Marie; index index.html; } } server { listen 80 ; server_name www.game1.com; location / { root /opt/tank; index index.html; } } www.game.com www.game1.com
Nginx日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 access_log /var/log/nginx/access.log main;access_log /var/log/nginx/access.log main; 日志的格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' ; $remote_addr $remote_user $time_local $time_iso8601 $request $status $body_bytes_sent $bytes_sent $msec $http_referer $http_user_agent $http_x_forwarded_for $X-Real-IP $request_length $request_time ps:出现报错等问题首先应该查看报错日志 排错: [root@web01 ~] 查看报错日志: [root@web01 ~]
json日志模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 log_format json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"service":"nginxTest",' '"trace":"$upstream_http_ctx_transaction_id",' '"log":"log",' '"clientip":"$remote_addr",' '"remote_user":"$remote_user",' '"request":"$request",' '"http_user_agent":"$http_user_agent",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"status":"$status"}' ; access_log /var/log/nginx/access.log json;
Nginx模块介绍 Nginx模块官方文档TP
Nginx访问控制模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 允许或者拒绝某些客户端地址的访问 deny : 拒绝allow : 允许 案例1 :允许192.168.15.1 访问,不允许其他IP访问 server { listen 80 ; server_name www.game.com; location / { root /opt/Super_Marie; index index.html; } allow 192.168.15.1 ; deny all; } 案例2:允许192.168.15.0这个网段访问,不允许其他网段访问 server { listen 80 ; server_name www.game.com; location / { root /opt/Super_Marie; index index.html; } allow 192.168.15.0 /24 ; deny all; } 案例3:只允许通过VPN来访问 server { listen 80 ; server_name www.game.com; location / { root /opt/Super_Marie; index index.html; } allow 172.16.1.81 ; deny all; } ps:每次修改完配置需要重启nginx [root@web01 ~]
Nginx访问认证模块
ngx_http_auth_basic_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 访问之前需要登录 auth_basic "Welcome To Login" ;auth_basic_user_file /etc/nginx/auth;1、安装httpd-tools [root@web01 ~] 2 、生成用户名密码文件[root@web01 ~] New password: Re-type new password: Adding password for user gengfeng 3 、查看密码文件内容[root@web01 ~] gengfeng:$apr1$b5wYYZ9b$PzcNn9gq.fgYUR4l0M3OL/ 4 、将文件路径加入Nginx配置[root@web01 ~] server { listen 80 ; server_name game.test.com; location / { root /opt/Super_Marie; index index.html; } auth_basic "Welcome To Login" ; auth_basic_user_file /etc/nginx/auth; } 5、重启Nginx [root@web01 ~] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 ~] 6 、测试 浏览器访问 http://game.test.com
Nginx目录索引模块
ngx_http_autoindex_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 展示目录索引 autoindex on | off ;autoindex_exact_size on | off ;autoindex_localtime on | off ;autoindex_format html | xml | json | jsonp;[root@web01 ~] server { listen 80 ; server_name game.test.com; location / { root /opt/Super_Marie; } autoindex on ; autoindex_exact_size on ; autoindex_localtime on ; autoindex_format html; } [root@web01 ~] 浏览器访问 http://game.test.com
Nginx状态监控模块
ngx_http_stub_status_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 监控Nginx运行状态 [root@web01 ~] [root@web01 ~] server { listen 80 ; server_name game.test.com; location / { root /opt/Super_Marie; index index.html; } location /status { stub_status; } } [root@web01 down]
Nginx的七种状态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Active connections 当前活动客户端连接数,包括Waiting连接数。 accepts 接受的客户端连接总数。 handled 处理的连接总数。通常,accepts 除非达到某些资源 限制(例如,worker_connections限制),否则 该参数值是相同的。 requests 客户端请求的总数。 Reading nginx 正在读取请求头的当前连接数。 Writing nginx 将响应写回客户端的当前连接数。 Waiting 当前等待请求的空闲客户端连接数。 keepalive_timeout 0 ; keepalive_timeout 65 ;
Nginx连接限制模块
ngx_http_limit_conn_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 用于限制每个定义的键的连接数,特别是来自单个IP地址的连接数。 limit_conn : 该指令指定每个给定键值的最大同时连接数,当超过这个数字时返回503 错误limit_conn_log_level : 当达到最大限制连接数后,记录日志的等级,默认error 级别 limit_conn_status : 指定当超过限制时,返回的状态码。默认是503 。 limit_conn_zone : 主要用来定义变量、zone名称、共享内存大小 $remote_addr 变量的长度为7 字节到15 字节,而存储状态在32 位平台中占用32 字节或64 字节, 在64 位平台中占用64 字节。 $binary_remote_addr 变量的长度是固定的4 字节,存储状态在32 位平台中占用32 字节或64 字节, 在64 位平台中占用64 字节。 ps:1M 共享空间可以保存3 .2 万个32 位的状态,1 .6 万个64 位的状态。如果共享内存空间被耗尽, 服务器将会对后续所有的请求返回 503 (Service Temporarily Unavailable) 错误。 控制Nginx连接数 [root@web01 ~] [root@web01 ~] limit_conn_zone $remote_addr zone=addr:1m ; limit_conn_log_level error ;limit_conn_status 503 ;server { listen 80 ; server_name 192.168.15.8 ; limit_conn addr 1 ; location / { root /opt/Super_Marie; index index.html; } } [root@web01 down] 测试 1 、安装ab压力测试命令 [root@web01 ~] 2 、ab 参数 -n : 总共需要访问多少次 -c : 每次访问多少个 测试结果: [root@web01 ~] ..... Benchmarking 192.168.15.8 (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests Server Software: nginx/1 .20 .2 Server Hostname: 192.168.15.8 Server Port: 80 Document Path: / Document Length: 1703 bytes Concurrency Level: 1000 Time taken for tests: 6 .416 seconds Complete requests: 10000 Failed requests: 128 (Connect: 0 , Receive: 0 , Length: 64 , Exceptions: 64 ) Write errors: 0 Total transferred: 19246032 bytes HTML transferred: 16921008 bytes Requests per second: 1558 .69 [ Time per request: 641 .562 [ms] (mean) Time per request: 0 .642 [ms] (mean, across all concurrent requests) Transfer rate: 2929 .56 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 243 253 .4 203 1310 Processing: 75 371 111 .0 365 656 Waiting: 0 311 106 .6 310 612 Total: 173 613 270 .2 569 1752 Percentage of the requests served within a certain time (ms) 50 % 569 66 % 599 75 % 654 80 % 667 90 % 719 95 % 1479 98 % 1637 99 % 1718 100 % 1752 (longest request)
Nginx请求限制模块
ngx_http_limit_req_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 基于漏桶算法实现的请求限流模块,用于对指定KEY对应的请求进行限流,比如按照IP维度限制请求速率 limit_req : 配置限流区域、桶容量(突发容量,默认0 )、是否延迟模式(默认延迟)limit_req_log_level : 配置记录被限流后的日志级别,默认error 级别 limit_req_status : 配置被限流后返回的状态码,默认返回503 limit_req_zone : 配置限流KEY、及存放KEY对应信息的共享内存区域大小、固定请求速率 $binary_remote_addr 变量,可以将每条状态记录的大小减少到64 个字节,这样1M 的内存可以保存大约1 万6 千个64 字节的记录。 zone=one:10m 区域名称为one,大小为10m 固定请求速率 使用rate参数配置,支持10r/s和60r/m,即每秒10 个请求和每分钟60 个请求,不过最终都会转换 为每秒的固定请求速率(10r/s为每100 毫秒处理一个请求;60r/m,即每1000 毫秒处理一个请求) 控制Nginx访问量 1 、连接池 limit_req_zone $remote_addr zone=one:10m rate=1r/s; 声明连接池 变量 名称 连接池的大小 速率 2、限制数 案例:要求每秒只能有一个访问。 [root@web01 ~] [root@web01 ~] limit_req_zone $remote_addr zone=one:10m rate=1r/s; limit_req_log_level error ;limit_req_status 503 ;server { listen 80 ; server_name game.test.com; limit_req zone=one burst=5 ; location / { root /opt/Super_Marie; index index.html; } } [root@web01 down]