0%

Nginx应用监控

Nginx是一个高性能的HTTP和反向代理服务器,同时也提供了IMAP/POP3/SMTP服务,是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上其并发能力确实在同类型的网页服务器中表现较好。

一、安装Nginx

#安装Nginx所需的依赖
[root@localhost ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel
#安装Nginx
[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
#解压
[root@localhost src]# tar zxf nginx-1.16.1.tar.gz
[root@localhost src]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz
#查看相关文件
[root@localhost src]# cd nginx-1.16.1
[root@localhost nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

注:configure是Shell脚本,执行它的作用是生成Makefile(使用Make工具编译C程序,而Make工具需要依赖Makefile)

#创建用户
[root@localhost nginx-1.16.1]# useradd -s /sbin/nologin -M www
#执行脚本
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/ngnix-1.16.1 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
#之后再查看目录下文件会发现Makefile文件
#编译并将生成的文件拷贝到指定的地方
[root@localhost nginx-1.16.1]# make && make install
#创建软链接
[root@localhost nginx-1.16.1]# cd
[root@localhost ~]# ln -s /usr/local/ngnix-1.16.1/ /usr/local/ngnix

二、启动Ngnix服务

#查看86端口有没有被占用
[root@localhost ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1178/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1281/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1178/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1281/master 
#启动服务前测试文件
[root@localhost ~]# /usr/local/ngnix/sbin/nginx -t
nginx: the configuration file /usr/local/ngnix-1.16.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/ngnix-1.16.1/conf/nginx.conf test is successful
#启动服务
[root@localhost ~]# /usr/local/ngnix/sbin/nginx

三、配置Nginx

[root@localhost ~]# cd /usr/local/ngnix/conf/
[root@localhost conf]# vim nginx.conf
...
#在一个服务后加nginx-status
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /nginx-status {
            stub_status on;
            access_log  off;
            allow 172.16.137.0/24;
            deny all;
        }
...
#保存后检测文件是否出错
[root@localhost conf]# /usr/local/ngnix/sbin/nginx -t
nginx: the configuration file /usr/local/ngnix-1.16.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/ngnix-1.16.1/conf/nginx.conf test is successful
# 重启服务
[root@localhost conf]# /usr/local/ngnix/sbin/nginx -s reload
#访问
[root@localhost conf]# curl 172.16.137.128/nginx-status
Active connections: 1 
server accepts handled requests
 2 2 2 
Reading: 0 Writing: 1 Waiting: 0 
#活动链接数1
#成功创建链接次数2  成功创建握手次数2  成功的请求次数2
#当前读取的客户端header的数量0  当前返回给客户端header的数量1  长链接状态下已经处理等待下一次请求的值0

在本机浏览器也可访问,如图所示: