Nginx
NGINX is a high-performance web server, reverse proxy, and load balancer known for its ability to handle thousands of concurrent connections efficiently with minimal resource usage. Its widespread adoption makes it a cornerstone of modern web infrastructure.
Monitoring NGINX's performance metrics is essential for maintaining reliable web services. Through metrics like connection states, request rates, response codes, and upstream response times, operators can quickly identify issues, optimize performance, and ensure high availability of their web services.
Prerequisites
1 MW Host Agent
Middleware Host Agent (MW Agent) v1.6.1+
must be installed on your local machine. To install the MW Agent, see our Installation Guide.
2 Expose Nginx Metrics
To collect metrics from NGINX, your server must expose a local status endpoint which requires the NGINX binary to be compiled with the status module enabled. This module is crucial for the monitoring solution to scrape and report NGINX metrics.
There are two module which can expose these metrics.
Nginx Status Stub
This collects very basic level metrics and mostly enabled by default in your nginx installation. To check if it is available try the following command:
nginx -V 2>&1 | grep -o http_stub_status_module
This should output
http_stub_status_module
Most standard NGINX distributions include this module by default, but if you're using a custom build, you'll need to compile it from source.
DEB
: Compatible with Debian or Debian-based operating systems. For AWS instances this includes Ubuntu and Debian.
# Install build dependencies sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev # Download and extract nginx wget http://nginx.org/download/nginx-1.24.0.tar.gz tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0 # Configure with status module ./configure --with-http_stub_status_module
RPM
: Compatible with Red Hat Package Manager and operating systems like Red Hat or CentOS. Use this for AWS instances using Amazon Linux.
# Install build dependencies sudo dnf groupinstall 'Development Tools' sudo dnf install pcre pcre-devel zlib zlib-devel openssl openssl-devel # Download and extract nginx wget http://nginx.org/download/nginx-1.24.0.tar.gz tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0 # Configure with status module
To enable stub status monitoring, add this to your NGINX configuration. This is generally at /etc/nginx/nginx.conf
location /nginx_status { stub_status on; access_log off; # Restrict access as needed allow 127.0.0.1; deny all; }
Nginx virtual host traffic status module
To check if VTS module is installed:
nginx -V 2>&1 | grep -o nginx-module-vts
This should output:
nginx-module-vts
If the module isn't present, you have to install manually:
DEB
: Compatible with Debian or Debian-based operating systems. For AWS instances this includes Ubuntu and Debian.
To install manually first check nginx version.
nginx -V
This outputs:
nginx version: nginx/1.24.0 (Ubuntu) ### HERE IS YOUR NGINX VERSION built with OpenSSL 3.0.2 15 Mar 2022 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-dSlJVq/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-dSlJVq/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module
Before we download and install nginx from source, we need to remove the current installation
sudo apt remove nginx
Check if nginx is removed:
# This should output: # bash: command not found: nginx nginx -V
Now we need to download Nginx source, depending on our Nginx version.
# First, ensure you have the necessary tools to build Nginx: sudo apt update sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev wget
Clone the VTS module repository:
git clone https://github.com/vozlt/nginx-module-vts.git
Download Nginx Source Code
wget http://nginx.org/download/nginx-1.24.0.tar.gz tar -xzvf nginx-1.24.0.tar.gz cd nginx-1.24.0
Run the configure script, including the path to the VTS module:
./configure --prefix=/usr/local/nginx \ --add-module=../nginx-module-vts \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_v2_module \ --with-stream
Build and install Nginx with the VTS module:
make sudo make install
Verify Installation:
# This should output: # nginx-module-vts /usr/local/nginx/sbin/nginx -V 2>&1 | grep -o nginx-module-vts
Add nginx to PATH. Open your .bashrc
and add the following line.
export PATH=$PATH:/usr/local/nginx/sbin
Source the .bashrc
file
source .bashrc
RPM
: Compatible with Red Hat Package Manager and operating systems like Red Hat, CentOS or Fedora. Use this for AWS instances using Amazon Linux.
First, remove any pre-installed version of Nginx:
sudo yum remove nginx -y
Check if Nginx is removed:
nginx -V # Expected output: bash: command not found: nginx
Update your system and install necessary build tools:
sudo yum update -y sudo yum groupinstall "Development Tools" -y sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel wget -y
Clone the VTS module repository:
git clone https://github.com/vozlt/nginx-module-vts.git
Visit nginx.org/download to find the version of Nginx that matches your needs. For example:
wget http://nginx.org/download/nginx-1.24.0.tar.gz tar -xzvf nginx-1.24.0.tar.gz cd nginx-1.24.0
Run the configure script with the necessary flags and modules:
./configure --prefix=/usr/local/nginx \ --add-module=../nginx-module-vts \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_v2_module \ --with-stream
Build and install Nginx:
make sudo make install
Check if Nginx is installed with the VTS module:
/usr/local/nginx/sbin/nginx -V 2>&1 | grep -o nginx-module-vts # Expected output: nginx-module-vts
Add nginx to PATH. Open your .bashrc
and add the following line.
export PATH=$PATH:/usr/local/nginx/sbin
Source the .bashrc
file
source .bashrc
Dockerfile
: If you want to run Nginx inside a docker container.
FROM nginx:1.22.1 ENV NGINX_VERSION="1.22.1" ENV NGINX_VTS_VERSION="0.2.2" # Install build dependencies and add nginx source repository RUN apt-get update \ && apt-get install -y gnupg2 \ && curl -s http://nginx.org/packages/keys/nginx_signing.key | apt-key add - \ && echo "deb-src http://nginx.org/packages/debian/ bullseye nginx" >> /etc/apt/sources.list
To enable vts, add this to your NGINX configuration. This is generally at /etc/nginx/nginx.conf
location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; access_log off; # Restrict access as needed allow 127.0.0.1; deny all; }
This will expose nginx metrics.
3 Access Integrations
Log in to Middleware, navigate to the Installations Page in the bottom left corner, select Integrations and click Nginx:
To configure the integration of your Nginx Web Server with our platform.
Enter Server Details:
Endpoint: Provide the endpoint URL of the server status for the http status stub. This is generally
<SERVER-URL>:<NGINX-PORT>/nginx_status
VTS Endpoint: Provide the endpoint URL of the server status for the VTS Endpoint . This is generally
<SERVER-URL>:<NGINX-PORT>/status/format/json
Collection Interval: Specify the interval at which telemetry data is collected.
Metrics collected
Metrics
Metric | Description | Unit | Type |
---|---|---|---|
nginx.requests | Total number of requests made to the server since it started | requests | cumulative (int) |
nginx.connections_accepted | The total number of accepted client connections | connections | cumulative (int) |
nginx.connections_handled | The total number of handled connections | connections | cumulative (int) |
nginx.connections_current | The current number of nginx connections by state | connections | cumulative (int) |
nginx.load_timestamp | Time of the last reload of configuration (time since Epoch) | ms | gauge (int) |
nginx.upstream.peers.response_time | The average time to receive the last byte of data from this server | ms | gauge (int) |
nginx.net.reading | Current number of connections where NGINX is reading the request header | connections | gauge (int) |
nginx.net.writing | Current number of connections where NGINX is writing the response back to the client | connections | gauge (int) |
nginx.net.waiting | Current number of connections where NGINX is waiting for a response | connections | gauge (int) |
nginx.server_zone.responses.1xx | The number of responses with 1xx status code | responses | gauge (int) |
nginx.server_zone.responses.2xx | The number of responses with 2xx status code | responses | gauge (int) |
nginx.server_zone.responses.3xx | The number of responses with 3xx status code | responses | gauge (int) |
nginx.server_zone.responses.4xx | The number of responses with 4xx status code | responses | gauge (int) |
nginx.server_zone.responses.5xx | The number of responses with 5xx status code | responses | gauge (int) |
nginx.server_zone.received | Bytes received by server zones | By | cumulative (int) |
nginx.server_zone.sent | Bytes sent by server zones | By | cumulative (int) |
nginx.upstream.peers.requests | Number of requests made to upstream servers | requests | cumulative (int) |
nginx.upstream.peers.received | Bytes received from upstream servers | By | cumulative (int) |
nginx.upstream.peers.sent | Bytes sent from upstream servers | By | cumulative (int) |
nginx.upstream.peers.responses.1xx | Number of responses from upstream with 1xx status codes | responses | cumulative (int) |
nginx.upstream.peers.responses.2xx | Number of responses from upstream with 2xx status codes | responses | cumulative (int) |
nginx.upstream.peers.responses.3xx | Number of responses from upstream with 3xx status codes | responses | cumulative (int) |
nginx.upstream.peers.responses.4xx | Number of responses from upstream with 4xx status codes | responses | cumulative (int) |
nginx.upstream.peers.responses.5xx | Number of responses from upstream with 5xx status codes | responses | cumulative (int) |
nginx.upstream.peers.weight | Weight of upstream server | weight | gauge (double) |
nginx.upstream.peers.backup | Whether upstream server is a backup server | {state} | gauge (int) |
nginx.upstream.peers.health_checks.last_passed | Boolean indicating if the last health check request was successful and passed tests | {status} | gauge (int) |