一、檢查配置文件語法
[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
三、加載連接狀態統計模塊
編輯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秒,請求被延遲響應。