Once the NginxWeb server is set up and running in the production environment, you want to monitor its activities in real time. In fact, common network monitoring software can support real-time monitoring of Nginx, such as Nagios, Zabbix, and Munin. However, if you do not need this type of tool label: once the Nginx Web server is set up and running in the production environment, you want to monitor its activities in real time. In fact, common network monitoring software can support real-time monitoring of Nginx, such as Nagios, Zabbix, and Munin.
However, if you do not need the comprehensive report feature or long-term statistics provided by such tools, you just want to quickly and easily monitor requests made to the Nginx Web server, we recommend that you use a command line tool named ngxtop.
At a glance, you will find that, regardless of the name or interface, ngxtop is inspired by the famous top command. The ngxtop function is to analyze Nginx access log files (and other log files, such as Apache2 logs) and display the analysis results in real time through a top-like interface. You may boast that your integrated monitoring tool has a variety of functions you need, but speaking of conciseness, it is definitely better than ngxtop, and conciseness is a major advantage that cannot be ignored.
In this tutorial, I will introduce how to use ngxtop to monitor Nginx Web servers in real time.
Install ngxtop on Linux
First, install pip on your Linux system. this is a prerequisite for ngxtop.
Then install ngxtop, as shown below.
- ___FCKpd___0nbsp;sudo pip install ngxtop
How to use ngxtop
The basic usage of ngxtop is as follows:
- ngxtop [options]
- ngxtop [options] (print|top|avg|sum)
- ngxtop info
Below are some common options.
- -L : Complete path for accessing log files (Nginx or Apache2)
- -F : Access log format
- -- No-follow: process the snapshot of the current log file, instead of the new line written to the log file in real time.
- -T : Update interval
- -N : Number of lines displayed
- -O: Sorting standard (default standard: quantity)
- - ..., -- ...: Add the expression (which must be an aggregate expression: sum, avg, min, and max) to the output.
- -V: detailed output
- -I : Only records matching the filter are processed.
The following are some built-in variables (). Their meanings are self-evident.
- Body_bytes_send
- Http_referer
- Http_user_agent
- Remote_addr
- Remote_user
- Request
- Status
- Time_local
Use ngxtop to monitor Nginx
By default, ngxtop tries to locate nginx access logs from its configuration file (/etc/nginx/Nginx. conf. To monitor Nginx, run the following command:
- ___FCKpd___2nbsp;ngxtop
It displays 10 requests processed by Nginx, sorted by the number of requests.
To display the first 20 most frequently requested requests, run the following command:
- ___FCKpd___3nbsp;ngxtop -n 20
To obtain Nginx information (including available variable information), run the following command:
- ___FCKpd___4nbsp;ngxtop info
If you want to, you can also customize the variables to be displayed. To do this, just list the variables you are interested in. The "print" command displays each request.
- ___FCKpd___5nbsp;ngxtop print request http_user_agent remote_addr
To display the most common IP address of a client, run the following command:
- ___FCKpd___6nbsp;ngxtop top remote_addr
To display requests whose status code is 404, run the following command:
- ___FCKpd___7nbsp;ngxtop -i 'status == 404' print request status
In addition to Nginx, ngxtop can also process other log files, such as Apache access logs. To monitor the Apache Web server, run the following command:
- ___FCKpd___8nbsp;tail -f /var/log/apache2/access.log | ngxtop -f common