Once Nginx web server is up and running in a production environment, you will want to monitor its activity in real time. In fact, general network monitoring software such as Nagios, Zabbix, Munin provide support for real-time Nginx monitoring.
However, if you do not need comprehensive reporting capabilities or long-term statistics offered by such tools, but just want a quick and easy way to monitor the requests coming to an Nginx web server, I would recommend a command line tool called ngxtop.
You will immediately tell that ngxtop (both its name and interface) is inspired by the famous top command. What ngxtop does is to parse Nginx access log file (and others such as Apache2 log), and display analyzed results via top-like interface in real-time. You can tout your sophisticated monitoring tool all you want, but it cannot beat ngxtop's simplicity, and simplicity is not something that cannot be dismissed.
In this tutorial, I will describe how to monitor Nginx web server in real time with ngxtop.
Install ngxtop on Linux
First, install pip on your Linux system as a prerequisite for ngxtop.
Then install ngxtop as follows.
Usage of ngxtop
The basic usage of ngxtop is as follows.
ngxtop [options] ngxtop [options] (print|top|avg|sum) <var> ngxtop info
Here are some of the common options.
- -l <file>: the full path to an access log file (Nginx or Apache2)
- -f <format>: access log format
- --no-follow: process the current log file snapshot, instead of new lines as they are written to the log file in real time
- -t <seconds>: update interval
- -n <number>: the number of lines in the display
- -o <var>: ordering criteria (default: count)
- -a <exp> ..., --a <exp> ...: add exp (must be aggregation exp: sum, avg, min, max, etc.) into output
- -v: verbose output
- -i <filter-expression>: only process records matched with filter
Here are built-in variables (represented as <var> in the above). Their meanings are self-explanatory.
Monitor Nginx with ngxtop
By default, ngxtop will try to detect the location of Nginx access log from its config file (/etc/nginx/nginx.conf). Thus, to monitor Nginx, simply run:
It will display 10 requests served by Nginx, sorted by the number of requests.
To display top-20 most frequent requests:
To get information about Nginx (including available variable info):
If you want, you can customize the variables to display. For that, simply list variables you are interested in. "print" command will display individual requests.
To display top IP addresses of clients:
To display requests whose status code is 404:
Besides Nginx, ngxtop is capable of processing other log files, such as Apache access logs. To monitor Apache web server, use this command:
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!
Latest posts by Dan Nanni (see all)
- How to set up Apt caching server on Ubuntu or Debian - February 7, 2016
- How to monitor OpenFlow messages with packet sniffer - February 2, 2016
- How to search multiple pdf documents for words on Linux - January 13, 2016