Nginx性能监控与调优

ngx_http_stub_status监控连接信息

nginx现已成为目前使用最广泛的web服务器和反向代理服务器,我们线上的Tomcat服务器一般都会由nginx进行代理,以此实现负载均衡的效果。既然nginx被应用得那么广泛,我们自然也得学习如何去对nginx做性能监控。本小节将介绍如何使用nginx的ngx_http_stub_status模块来对连接信息进行监控。本文默认读者有nginx的基础,所以一些基础性的东西不会过多介绍。

关于该模块的官方文档地址如下:

如果你的nginx是使用yum进行安装的,那么一般会自带这个模块,可以忽略以下这段为了安装ngx_http_stub_status模块而重新编译安装nginx的部分。因为我的nginx是编译安装的,当时并没有加上这个模块进行编译,所以现在还需要重新去编译安装一次。过程如下:

[root@01server ~]# /usr/local/nginx/sbin/nginx -V # 列出安装了哪些模块,可以看到我这里没有安装任何模块 nginx version: nginx/1.12.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) configure arguments: --prefix=/usr/local/nginx [root@01server ~]# rm -rf /usr/local/nginx/ # 删除原本的nginx [root@01server ~]# cd /usr/local/src/ # 进入存放安装包的路径 [root@01server /usr/local/src]# ls nginx-1.12.1 nginx-1.12.1.tar.gz [root@01server /usr/local/src]# cd nginx-1.12.1 # 进入之前已经解压好的目录 [root@01server /usr/local/src/nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module # 加入编译参数,指定需要安装的模块 [root@01server /usr/local/src/nginx-1.12.1]# make && make install # 编译安装 [root@01server /usr/local/src/nginx-1.12.1]# cd /usr/local/nginx/sbin/ [root@01server /usr/local/nginx/sbin]# ./nginx -V # 可以已经把模块安装好了 nginx version: nginx/1.12.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module [root@01server /usr/local/nginx/sbin]#

安装好之后,还需要编辑一下配置文件,不使用nginx默认的配置文件:

[root@01server /usr/local/nginx/sbin]# cd ../conf/ [root@01server /usr/local/nginx/conf]# mv nginx.conf nginx.conf.bak # 不使用nginx自带的配置文件 [root@01server /usr/local/nginx/conf]# vim nginx.conf # 将以下内容粘贴进去 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; } [root@01server /usr/local/nginx/conf]# mkdir vhost [root@01server /usr/local/nginx/conf]# vim vhost/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 101.106.102.129; # 允许访问的ip,即白名单ip allow 127.0.0.1; deny all; # 拒绝白名单ip以外的ip访问 } } [root@01server ~]#

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d47316e0ee1afc9d00195ea501348c1a.html