一、检查配置文件语法
[root@node2 /]# nginx -tc /usr/local/nginx/conf/nginx.conf nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
二、重载配置
[root@node2 /]# nginx -s reload
三、查看已安装模块列表
[root@localhost src]# nginx -V nginx version: nginx/1.18.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
四、加载连接状态统计模块
编辑nginx.conf文件,加入一个location,加载stub_status模块
创建配置文件目录,将默认配置文件复制到此目录
[root@node2 /]# mkdir /etc/nginx [root@node2 /]# cp /usr/local/nginx/conf/nginx.conf /etc/nginx
vim /etc/nginx/nginx.conf
在server里加入一个location
location /stats{
stub_status;
}
保存、退出,检查语法
[root@node2 sbin]# nginx -tc /etc/nginx/nginx.conf nginx: [emerg] open() "/etc/nginx/mime.types" failed (2: No such file or directory) in /etc/nginx/nginx.conf:18 nginx: configuration file /etc/nginx/nginx.conf test failed
报错,将mime.types文件以及html目录复制到/etc/nginx/下
cp /usr/local/nginx/conf/mime.types /etc/nginx cp -r /usr/local/nginx/html/ /etc/nginx/
再检查语法
[root@node2 nginx]# nginx -tc /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
重载配置
[root@node2 nginx]# nginx -s reload
浏览器打开http://ip/stats
显示以上信息:
Active connections: 1
当前活动连接数:1
server accepts handled requests
3 3 28
分别表示:握手次数、连接次数、请求次数
正常情况下,握手次数和连接次数是相等的,表明没有丢失的连接
Reading: 0 Writing: 1 Waiting: 2
Reading:表示当前状态正在读的连接个数、
Writing:表示当前状态正在写的连接个数
Waiting:表示当前状态空闲的连接个数,当nginx开启了长连接keepalive后,会出现空闲连接
在nginx.conf配置文件中有一个keepalive_timeout参数,用于设置长连接超时时间
keepalive_timeout 65;
五、加载请求限制模块
vim /etc/nginx/nginx.conf
在server之前加上
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
location里加上
location / { root html; index index.html index.htm; limit_req zone=req_zone; }
保存,检查语法并重载
[root@node2 nginx]# nginx -tc /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@node2 nginx]# nginx -s reload [root@node2 nginx]#
压测
[root@node2 logs]# ab -n 10 -c 1 http://127.0.0.1/ 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 127.0.0.1 (be patient).....done Server Software: nginx/1.14.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: / Document Length: 612 bytes Concurrency Level: 1 Time taken for tests: 0.002 seconds Complete requests: 10 Failed requests: 9 (Connect: 0, Receive: 0, Length: 9, Exceptions: 0) Write errors: 0 Non-2xx responses: 9 Total transferred: 7424 bytes HTML transferred: 5445 bytes Requests per second: 4854.37 [#/sec] (mean) Time per request: 0.206 [ms] (mean) Time per request: 0.206 [ms] (mean, across all concurrent requests) Transfer rate: 3519.42 [Kbytes/sec] received
显示只有1次请求正常,其他9次被限制了
添加延迟响应参数
location / { root html; index index.html index.htm; limit_req zone=req_zone burst=2; }
保存,重载,再次压测
[root@node2 logs]# ab -n 10 -c 1 http://127.0.0.1/ 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 127.0.0.1 (be patient).....done Server Software: nginx/1.14.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: / Document Length: 612 bytes Concurrency Level: 1 Time taken for tests: 9.002 seconds Complete requests: 10 Failed requests: 0 Write errors: 0 Total transferred: 8450 bytes HTML transferred: 6120 bytes Requests per second: 1.11 [#/sec] (mean) Time per request: 900.161 [ms] (mean) Time per request: 900.161 [ms] (mean, across all concurrent requests) Transfer rate: 0.92 [Kbytes/sec] received
结果显示,请求全部成功,但是耗时变成了9秒,请求被延迟响应。