一.systemctl启动nginx

我们前面可以看见nginx的管理方式是这样的

/usr/local/nginx/sbin/nginx      #启动
/usr/local/nginx/sbin/nginx     -s reload    #重启
/usr/local/nginx/sbin/nginx  -s stop             #关闭

平时我们为了方便,可以做一个软连接,但是我们现在想用sysytemctl方式管理,我们需要做以下设置。

实验如下所示:

将httpd的管理配置文件做为模版

[root@nodel1 html]# yum install httpd -y
[root@nodel1 html]# cd /usr/lib/systemd/system
[root@nodel1 system]# ls

[root@nodel1 system]# cp httpd.service /etc/systemd/system/
[root@nodel1 system]# cd /etc/systemd/system/
[root@nodel1 system]# ls
[root@nodel1 system]# mv httpd.service nginx.service
[root@nodel1 system]# ls
[root@nodel1 system]# vim nginx.service 

在这里插入图片描述
官方建议系统的服务和自己设定的服务最好不要放在同一个目录下
在这里插入图片描述

在这里插入图片描述

[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx  -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

在这里插入图片描述

用原来的方式关闭nginx,不能用systemctl,否则先开始可能无法正常使用systemctl,

[root@nodel1 system]# /usr/local/nginx/sbin/nginx -s stop
[root@nodel1 system]# netstat -tnlp

在这里插入图片描述

使用systemctl开启服务

[root@nodel1 system]# systemctl status nginx
[root@nodel1 system]# systemctl start nginx
[root@nodel1 system]# systemctl status nginx

在这里插入图片描述

二.nginx对客户端的请求限制

HTTP协议是基于TCP的,如果要完成一次HTTP请求的时候,首先进行TCP的三次握手。当建立连接的时候就可以进行请求和响应。
HTTP请求建立在一次TCP连接基础上,一次TCP请求至少产生一次HTTP请求(可以有多个)

请求限制:

请求限制主要有两种类型:

  • 请求频率限制: limit_req_module
  • 连接频率限制: limit_conn_module

请求限制的实验配置如下:

先将之前的gzip注释掉,为了不影响实验:

在这里插入图片描述

请求频率限制:ngx_http_limit_req_module

主要用于限制每一个定义的密钥的请求的处理速率,特别是从一个单一的IP地址的请求的处理速率。

第一种

请求的完成量限制:

[root@nodel1 system]# cd /usr/local/nginx/conf/
[root@nodel1 conf]# vim nginx.conf

38  limit_req_zone $binary_remote_addr  zone=one:10m  rate=1r/s;
 # 开辟一个10m的请求空间,命名为one。同一个IP发送的请求,平均每秒只处理一次
 
52       location /search/ {
53                       limit_req zone=one;    
54                          }

在这里插入图片描述

在这里插入图片描述

配置search目录并重启服务:

[root@nodel1 conf]# cd ..
[root@nodel1 nginx]# cd html/
[root@nodel1 html]# mkdir search
[root@nodel1 html]# ls
50x.html  index.html  search
[root@nodel1 html]# cd search/
[root@nodel1 search]# ls
[root@nodel1 search]# 

[root@foundation15 Desktop]# scp redhat.jpg root@172.25.15.1:/usr/local/nginx/html/search/

[root@nodel1 search]# ls
redhat.jpg
[root@nodel1 search]# systemctl reload nginx

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

进行压力测试:

补充:

ab命令压力测试:

网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环。只有让服务器处在高压情况下,才能真正体现出软件、硬件等各种设置不当所暴露出的问题。
性能测试工具目前最常见的有以下几种:ab、http_load、webbench、siege。今天我们专门来介绍ab。
ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

ab是apachebench命令的缩写。

ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

注意事项:

ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。

ab的参数:

-n    在测试会话中所执行的请求个数。默认时,仅执行一个请求。表示请求总数
-c    一次产生的请求个数。默认是一次一个。表示并发用户数。

详情请点击

压力测试:

[root@foundation15 ~]# ab -c 1 -n 10 http://172.25.15.1/search/redhat.jpg

在这里插入图片描述

添加参数,进行实验:

[root@nodel1 html]# cd /usr/local/nginx/conf/
[root@nodel1 conf]# vim nginx.conf

 52       location /search/ {
 53                       limit_req zone=one  burst=5;
   # 当客户端请求超过指定次数,最多宽限5次请求,并延迟处理,1秒1个请求
  # limit_req zone=one burst=5;
  # 当客户端请求超过指定次数,最多宽限5次请求,并立即处理。
  # limit_req zone=one burst=5 nodelay;
 54                          }

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

压力测试:

在这里插入图片描述

第二种

对客户端下载速率限制:

[root@nodel1 conf]# vim nginx.conf

53                       limit_rate  50k;
 
[root@nodel1 conf]# systemctl reload nginx

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@nodel1 home]# cd /usr/local/nginx/
[root@nodel1 nginx]# cd html/
[root@nodel1 html]# cd search/
[root@nodel1 search]# du -sh redhat.jpg 
56K	redhat.jpg

在这里插入图片描述

下载测试:

在这里插入图片描述
此时发现错误请求有0次,全部都为正确请求,所以10s多后成功(10s多的原因是因为图片大小是56k,限制速率是50k,56k/50k=1.2,1.12*10次=11.2,没差多少,正常偏差)

连接频率限制: limit_conn_module

该ngix_http_limit_conn_module模块用于限制每个定义的密钥的连接数量,特别是来自单个IP地址的连接数量。

并非所有连接都被计算在内 只有在服务器处理请求并且已经读取了整个请求头时才计算连接。

对客户端并发量的限制

[root@nodel1 conf]# vim nginx.conf

38 # limit_req_zone $binary_remote_addr  zone=one:10m  rate=1r/s;
39 limit_conn_zone   $binary_remote_addr  zone=addr:10m;     # 开辟一个10m的连接空间,命名为addr


54       location /search/ {
55                        limit_conn  addr 1;      # 表示进行并发的限制,一个时间段只能有一个
56                       #limit_rate  50k;
57                       #limit_req zone=one  burst=5;
58                          }


[root@nodel1 conf]# systemctl reload nginx

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

并发量测试:

[root@foundation15 ~]# ab -c 1 -n 10 http://172.25.15.1/search/redhat.jpg
[root@foundation15 ~]# ab -c 10 -n 10 http://172.25.15.1/search/redhat.jpg

在这里插入图片描述
在这里插入图片描述

更多推荐