Linux下Nginx性能监控与调优

参考源:https://www.linuxidc.com/Linux/2019-07/159486.htm

//系统
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
# uname -r
3.10.0-957.el7.x86_64
//关闭SELinux
//firewall按需打开或关闭
//Nginx编译安装
//解决依赖
# yum -y install gcc gcc-c++ wget cmake pcre-devel curl-devel openssl-devel net-tools
//wget 安装包,解压编译等
# wget http://nginx.org/download/nginx-1.16.0.tar.gz
# tar zxvf nginx-1.16.0.tar.gz
# cd nginx-1.16.0
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
# make && make install

//查看版本与模块
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module

//自定义配置文件,进行配置调整
# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
# touch /usr/local/nginx/conf/nginx.conf
# vim /usr/local/nginx/conf/nginx.conf		//后附nginx.conf
# mkdir /usr/local/nginx/conf/vhost
# touch /usr/local/nginx/conf/vhost/default.conf
# vim /usr/local/nginx/conf/vhost/default.conf		//后附default.conf

//检查nginx配置文件语法
# /usr/local/nginx/sbin/nginx -t
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

//配置nginx启动脚本、启动Nginx、配置开机启动服务
# vim /lib/systemd/system/nginx.service		//后附Nginx启动脚本
# systemctl start nginx.service
# systemctl enable nginx.service

//测试模块
# curl 127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 13 13 14 
Reading: 0 Writing: 1 Waiting: 0 

//nginx_status 模块参数说明:
Active connections 当前活动的连接数量(包括等待的)
accepts 已接收的连接总数
handled 已处理的连接总数
requests 当前的请求总数
Reading nginx正在读取的连接数量
Writing nginx正在响应的连接数量
Waiting 当前空闲的连接数量
# cat /usr/local/nginx/conf/nginx.conf		//Nginx配置如下
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 1024;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    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  /usr/local/nginx/logs/access.log  main;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-Javascript text/css text/htm 
    application/xml;
    include vhost/*.conf;
}
# cat /usr/local/nginx/conf/vhost/default.conf 		//default.conf 内容如下
server{
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;

        location = /nginx_status{  # 配置访问路径,即uri
            stub_status on;  # 开启该模块
            access_log off;  # 关闭日志
            #allow 192.168.203.14;  # 允许访问的ip,即白名单ip 
            #allow 127.0.0.1;
            #deny all;  # 拒绝白名单ip以外的ip访问
       }
}

# cat /lib/systemd/system/nginx.service		//nginx启动脚本
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
//Nginx请求分析工具ngxtop
# yum install -y epel-release
# yum install -y python-pip
# pip install ngxtop
# ngxtop --help
ngxtop - ad-hoc query for nginx access log.

Usage:
    ngxtop [options]
    ngxtop [options] (print|top|avg|sum) <var> ...
    ngxtop info
    ngxtop [options] query <query> ...

Options:

-l <file>, --access-log <file>  # 需要分析的访问日志
    -f <format>, --log-format <format>  # log_format指令指定的日志格式 [默认: combined]
    --no-follow  ngxtop default behavior is to ignore current lines in log
                     and only watch for new lines as they are written to the access log.
                     Use this flag to tell ngxtop to process the current content of the access log instead.  # 简而言之,对历史信息进行统计
    -t <seconds>, --interval <seconds>  report interval when running in follow mode [default: 2.0]  # 指定监控信息刷新的间隔,单位为秒 [默认: 2.0]
    -g <var>, --group-by <var>  # 根据变量分组 [默认: request_path]
    -w <var>, --having <expr>  # 具备子句 [默认: 1] having clause [default: 1]
    -o <var>, --order-by <var>  # 排序 [默认: count]
    -n <number>, --limit <number>  # 显示的条数 [默认: 10]
    -a <exp> ..., --a <exp> ...  add exp (must be aggregation exp: sum, avg, min, max, etc.) into output  # 添加聚合表达式到输出信息中

    -v, --verbose  # 更多的输出
    -d, --debug  # 打印所有行和解析记录,debug
    -h, --help  # 当前帮助信息.
    --version  # 输出版本信息.
高级选项:

-c <file>, --config <file>  # 运行ngxtop解析nginx配置文件
-i <filter-expression>, --filter <filter-expression>  filter in, records satisfied given expression are processed.  # 根据指定的表达式进行过滤,仅显示过滤后的信息
-p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.  # 在筛选器表达式中检查预解析阶段

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值