How to Monitor NGINX server

Filed Under: NGINX

NGINX is becoming an increasingly popular HTTP server and has been deployed in more than 400 million sites. If left unattended, issues may crop up from time to time. As such it is imperative to monitor NGINX server regularly for performance and troubleshooting point of view.

Let us look at few most common and easy procedures that you can undertake regularly to monitor and troubleshoot an NGINX server effectively.

Prerequisites

  • You have already installed NGINX in your server by following our tutorial from here.

Enable NGINX access and error log

Enable access logs in NGINX to monitor real-time traffic. To do that, edit the Nginx configuration file and add the following access_log directive.


server {
...
...
        access_log /var/log/nginx/access_log combined;
...
...

}

It is also possible that there is not enough information logged to the error log. In that case, change the log level of the error_log directive to debug in the NGINX configuration file.


server {
...
...
         error_log /var/log/nginx/error_log debug;
...
...
}

Once you are done with enabling access log and log level of error log to debug, restart Nginx and tail the log file to see them in real time.


# systemctl restart nginx
# tail -f /var/log/nginx/access_log
# tail -f /var/log/nginx/error_log

Enable Nginx Status Page

The collection of NGINX metrics is paramount for monitoring it effectively. These metrics provide valuable insights on the activity of NGINX servers like request per second, response time, active connections and more, thereby enabling you to preempt any possible issues that your NGINX server will probably face.

The ngx_http_stub_status_module (stub_status) module does this for you by exposing information about NGINX activity. This module is not compiled by default if you have installed NGINX by compiling from source. Just use the following command to find if NGINX have been built with stub_status module:


 # nginx -V 2>&1 | grep -o with-http_stub_status_module

If the above command produces a blank output then you need to rebuild NGINX from source by including --with-http_stub_status_module parameter to the configure script.

Now enable the status module on nginx virtual host file by adding the following location block. Make sure this page is accessible to only you by adding allow and deny directive.


...
...
location /nginx_status {
        stub_status on;
        allow 127.0.0.1;
        deny all;
}
...
...

Reload NGINX to apply new settings:


# systemctl restart nginx

Now to get the status metrics, make a CURL query to the NGINX server using the URL http://SERVER_IP/nginx_status, An output like below confirms that the status page is configured correctly:


# curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
 549 549 21583
Reading: 0 Writing: 1 Waiting: 0

The Nginx status page will provide you with real-time data about Nginx’s health enabling you to tweak Nginx configuration if needed.

Therefore by monitoring NGINX metrics, you will have valuable insights of both active and rudimentary issues within your nginx server.

Monitor NGINX with Amplify

NGINX Amplify is a fantastic monitoring tool for NGINX and NGINX Plus. Using NGINX Amplify one can view the system and NGINX performance metrics in a visual dashboard. Amplify enables you to not only monitor the performance of your application but also monitor the overall health and status of your NGINX server.

Create amplify account

To start using amplify, visit the home page of NGINX amplify and create an account. Once the account creation process completes successfully, a popup screen will provide you with the download URL of Amplify along with API key.

Create Amplify Account

Create Amplify Account

Just copy the installation command of Amplify in a notepad and Click ‘Continue’. Wait for a minute, while Amplify connects to the remote NGINX instance.

Install amplify

Head back to the terminal to download and run the installation script in the terminal using following commands in the terminal.


# wget https://github.com/nginxinc/nginx-amplify-agent/blob/master/packages/install.sh
# chmod u+x install.sh
# API_KEY='fae543a26f7664f84c61846f66d998f7' sh ./install.sh
Successful Installation Of Amplify

Successful Installation Of Amplify

You will get a confirmation of the successful installation of amplify in your server just like above.

Add stub_status page

Amplify makes use of the stub_status module to access the NGINX server URL via HTTP. It does this by looking for a location block containing stub_status directive in the NGINX configuration file.


# vi your_nginx_virtual_host.conf
server {
         ...
         ...    
         location /nginx_status {
             stub_status;
             allow 127.0.0.1;
             deny all;
       }
       ...
       ...
}

Configure logging format

It is also possible to log the metrics that you are interested in. To do that, Create a custom log format and just add it in the HTTP section of your NGINX configuration file. The following section defines a custom log format by the name custom_format that can be used in any NGINX server block by specifying its name with access_log directive.


http {
...
...
log_format custom_format '$remote_addr - $remote_user [$time_local]"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" rt=$request_time rt="$upstream_response_time"';
...
...
}

Next add access_log and error_log directive in your specific NGINX virtual host file


# vi your_nginx_virtual_host.conf
server {
...
...
    access_log  /var/log/nginx/access.log  custom_format;
    error_log  /var/log/nginx/error.log warn;
...
...
}

Check the configuration files for any syntactical error and restart NGINX.


# nginx -t
# systemctl restart nginx

The metrics will be available immediately in the amplify dashboard. Use the tabs at the top of the page to view the metrics that are appropriate for you.

NGINX Amplify Dashboard

NGINX Amplify Dashboard

Monitor NGINX logs with ngxtop

ngxtop is another awesome utility that can parse nginx access log and outputs few important metrics about nginx server.

Using ngxtop command, you can view the summary of request count, requested URI, the number of request by status code and much more.

Once you have executed ngxtop, you will notice that it looks much like top output but with Nginx related information.

To install ngxtop in Ubuntu 18.04, you need to install Python PIP beforehand. Once you are done with installing python pip, use it to install ngxtop in your server.


# apt-get install python-pip
# pip install ngxtop

Type ngxtop command to view the summary of request count, requested URI, the number of request by status code.


# ngxtop

Find more about usages on ngxtop from here.

Use Log Analytics Service

Finally, You can also choose a log analytics service from a wide range of options that are available freely in the market to parse and monitor NGINX logs.

For example, using syslog-ng or filebeat you can store and forward NGINX logs to any log-analytics services. This enables you to create a separate dedicated instance for processing NGINX logs.

Once the logs are parsed and filtered, just create dashboards or generate alerts using the log analytics service.

Therefore, by using any log analytic service it is possible to monitor NGINX logs in real time and take corrective measures if some anomalies are detected.

Summary

Although we have covered only a few options to monitor NGINX servers. If implemented properly, it will definitely improve the monitoring capabilities of NGINX servers at your disposal. You are now ready to monitor NGINX servers in your infrastructure by using any of the above-mentioned methods.

Comments

  1. huang huazai says:

    Hi,the commands are not pasted in this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages