乐闻世界logo
搜索文章和话题

How to monitor and operate Nginx? What are the monitoring tools?

2月21日 16:57

How to monitor and operate Nginx? What are the monitoring tools?

Nginx monitoring and operations are crucial for ensuring service stability and performance. Proper monitoring can detect and resolve issues in a timely manner.

Built-in Status Monitoring:

nginx
# Enable stub_status module server { listen 80; server_name localhost; location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } }

Status Information Explanation:

  • Active connections: Current active connection count
  • accepts: Total accepted connections
  • handled: Total handled connections
  • requests: Total handled requests
  • Reading: Connections reading request headers
  • Writing: Connections sending responses
  • Waiting: Idle connections

Custom Monitoring Endpoints:

nginx
server { listen 80; server_name localhost; # Health check location /health { access_log off; return 200 "OK\n"; add_header Content-Type text/plain; } # Readiness check location /ready { access_log off; # Check backend connection proxy_pass http://backend/health; proxy_intercept_errors off; } # Version information location /version { access_log off; return 200 "Nginx/1.21.0\n"; add_header Content-Type text/plain; } }

Log Monitoring:

nginx
# Custom log format log_format monitoring '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'rt=$request_time uct="$upstream_connect_time" ' 'uht="$upstream_header_time" urt="$upstream_response_time" ' 'cache=$upstream_cache_status'; server { listen 80; server_name example.com; access_log /var/log/nginx/monitoring.log monitoring; location / { proxy_pass http://backend; } }

Prometheus Monitoring:

nginx
# Install nginx-prometheus-exporter # https://github.com/nginxinc/nginx-prometheus-exporter # Configure Nginx server { listen 80; server_name localhost; location /metrics { proxy_pass http://localhost:9113/metrics; access_log off; allow 127.0.0.1; deny all; } }

Grafana + Prometheus Monitoring:

yaml
# prometheus.yml global: scrape_interval: 15s scrape_configs: - job_name: 'nginx' static_configs: - targets: ['localhost:9113']

ELK Stack Monitoring:

nginx
# JSON format logs log_format json_combined escape=json '{' '"time_local":"$time_local",' '"remote_addr":"$remote_addr",' '"remote_user":"$remote_user",' '"request":"$request",' '"status":"$status",' '"body_bytes_sent":"$body_bytes_sent",' '"request_time":"$request_time",' '"http_referrer":"$http_referer",' '"http_user_agent":"$http_user_agent"' '}'; server { listen 80; server_name example.com; access_log /var/log/nginx/access.log json_combined; location / { proxy_pass http://backend; } }

Zabbix Monitoring:

bash
# Install Zabbix Agent # Configure monitoring items # nginx_status[accepts] # nginx_status[handled] # nginx_status[requests] # nginx_status[reading] # nginx_status[writing] # nginx_status[waiting]

Performance Monitoring Metrics:

nginx
# Enable detailed logging log_format performance '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' 'rt=$request_time ' 'uct=$upstream_connect_time ' 'uht=$upstream_header_time ' 'urt=$upstream_response_time ' 'cache=$upstream_cache_status'; server { listen 80; server_name example.com; access_log /var/log/nginx/performance.log performance; location / { proxy_pass http://backend; } }

Alert Configuration:

nginx
# Log-based alerting map $status $alert_level { ~^[5] critical; ~^[4] warning; default ok; } server { listen 80; server_name example.com; access_log /var/log/nginx/access.log performance; location / { proxy_pass http://backend; # Add alert header add_header X-Alert-Level $alert_level; } }

Automated Operations Scripts:

bash
#!/bin/bash # nginx_monitor.sh # Check Nginx status check_nginx_status() { if ! curl -f http://localhost/nginx_status > /dev/null 2>&1; then echo "Nginx status page is not accessible" return 1 fi return 0 } # Check process check_nginx_process() { if ! pgrep -x nginx > /dev/null; then echo "Nginx process is not running" return 1 fi return 0 } # Check port check_nginx_port() { if ! netstat -tlnp | grep :80 > /dev/null; then echo "Nginx is not listening on port 80" return 1 fi return 0 } # Main function main() { check_nginx_status check_nginx_process check_nginx_port echo "All checks passed" } main

Operations Commands:

bash
# Reload configuration (no service interruption) nginx -s reload # Graceful stop nginx -s quit # Fast stop nginx -s stop # Reopen log files nginx -s reopen # Test configuration nginx -t # View version nginx -v # View compilation parameters nginx -V

Log Rotation:

bash
# /etc/logrotate.d/nginx /var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 0640 nginx adm sharedscripts postrotate [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript }

Complete Monitoring Configuration Example:

nginx
user nginx; worker_processes auto; http { # Log formats log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; log_format performance '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' 'rt=$request_time ' 'uct=$upstream_connect_time ' 'uht="$upstream_header_time" ' 'urt="$upstream_response_time" ' 'cache=$upstream_cache_status'; log_format json_combined escape=json '{' '"time_local":"$time_local",' '"remote_addr":"$remote_addr",' '"remote_user":"$remote_user",' '"request":"$request",' '"status":"$status",' '"body_bytes_sent":"$body_bytes_sent",' '"request_time":"$request_time",' '"http_referrer":"$http_referer",' '"http_user_agent":"$http_user_agent"' '}'; # Main site server { listen 80; server_name example.com; root /var/www/html; index index.html; # Performance logs access_log /var/log/nginx/performance.log performance; error_log /var/log/nginx/error.log warn; # Monitoring endpoints location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } location /health { access_log off; return 200 "OK\n"; add_header Content-Type text/plain; } location /ready { access_log off; proxy_pass http://backend/health; proxy_intercept_errors off; } location /metrics { proxy_pass http://localhost:9113/metrics; access_log off; allow 127.0.0.1; deny all; } location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
  1. Prometheus + Grafana: Powerful monitoring and visualization platform
  2. ELK Stack: Log collection, storage, and analysis
  3. Zabbix: Enterprise-level monitoring system
  4. Nagios: Mature monitoring solution
  5. Datadog: Cloud monitoring service
  6. New Relic: Application performance monitoring
  7. AppDynamics: Application performance management

Operations Best Practices:

  1. Comprehensive monitoring: Monitor performance, logs, resource usage
  2. Timely alerting: Set reasonable alert thresholds
  3. Regular backups: Backup configurations and important data
  4. Automated operations: Use scripts and tools for automation
  5. Documentation: Record operations and issues in detail
  6. Regular drills: Conduct regular fault drills
  7. Performance optimization: Continuously monitor and optimize performance
  8. Security audits: Conduct regular security checks
  9. Capacity planning: Plan capacity based on business growth
  10. Continuous improvement: Continuously improve based on monitoring data
标签:Nginx