linux下nginx日常操作

一、检查配置文件语法

[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秒,请求被延迟响应。

原文地址:https://www.cnblogs.com/sky-cheng/p/10756777.html