How to Monitor NGINX Logs and Performance in Real-Time with Watchlog

Introduction
Monitoring NGINX logs and performance is essential for ensuring high availability, optimizing response times, and detecting anomalies before they impact users. With Watchlog, you can track critical NGINX metrics, including request statuses, response times, active connections, and upstream delays, all in real time.
Step 1: Install Watchlog Agent
Before configuring NGINX, you need to install the Watchlog Agent on your server. If you haven't installed it yet, use the following command:
sudo apiKey="your-api-key" server="your-server" bash -c "$(curl -L https://watchlog.io/ubuntu/watchlog-script.sh)"
Once installed, the agent will be ready to collect data from NGINX.
Step 2: Configure NGINX for Metric Collection
NGINX does not expose detailed metrics by default, so you need to configure it to provide the required data.
Enable stub_status Module
Add the following configuration to your NGINX settings file (/etc/nginx/nginx.conf
or a specific site configuration file):
server { listen 127.0.0.1:8080; # Adjust the IP and port if necessary server_name localhost; # Change this if needed location /nginx_status { stub_status on; # Optionally restrict access: # allow 127.0.0.1; # deny all; } }
This enables real-time NGINX statistics via the /nginx_status
endpoint, which Watchlog will use to collect key metrics.
Add Custom Log Format for Watchlog
To capture more detailed request and response metrics, define a custom log format by adding this to your NGINX configuration:
log_format watchlogFormat '$remote_addr - $remote_user [$time_local] ' '"$request" "$scheme://$host$request_uri" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' '$request_time $upstream_response_time $pipe $ssl_protocol $ssl_cipher'; access_log /var/log/nginx/access.log watchlogFormat;
This logs:
- ✅ Client IP, request details, and HTTP status.
- ✅ Response time & upstream response time (important for detecting slow requests).
- ✅ SSL/TLS details for security monitoring.
Apply Changes and Restart NGINX
Test your configuration and reload NGINX:
sudo nginx -t sudo systemctl reload nginx
Your NGINX server is now set up to expose logs and real-time metrics!
Step 3: Configure Watchlog for NGINX Monitoring
To enable NGINX monitoring in Watchlog, update the integration configuration file (integration.json
):
{ "monitor": true, "service": "nginx", "accessLog": "/var/log/nginx/access.log", "nginx_status_url": "http://localhost:8080/nginx_status" }
Key Configurations:
Enables NGINX monitoring.
- Specifies the monitored service.
Defines the log file location.
- Points to the stub_status endpoint.
Save the file to apply changes.
Step 4: Restart Watchlog Agent
Restart the Watchlog Agent to apply the new configurations.
For Ubuntu Agent:
sudo systemctl stop watchlog-agent sudo systemctl start watchlog-agent
For PM2-Managed Agent:
pm2 reload watchlog-agent
After restarting, Watchlog will start collecting NGINX logs and metrics in real time.
Step 5: Verify NGINX Monitoring in Watchlog
Now, log in to your Watchlog dashboard, navigate to the NGINX monitoring section, and confirm that:
- ✅ Real-time requests, response times, and status codes are visible.
- ✅ The `` endpoint metrics are displayed.
- ✅ Alerts are configured for abnormal traffic spikes or slow response times.
Conclusion
By following this guide, you have successfully configured NGINX monitoring with Watchlog. You can now:
- ✅ Track NGINX performance metrics in real time.
- ✅ Monitor request logs, response times, and upstream delays.
- ✅ Set up alerts to detect performance bottlenecks early.
This ensures your web server remains highly available, efficient, and secure. Start monitoring your NGINX server today with Watchlog and take control of your application’s performance! 🚀