Last9 Last9

Feb 6th, ‘25 / 7 min read

NGINX Log Monitoring: What It Is, How to Get Started, and Fix Issues

Learn what NGINX log monitoring is, how to set it up, and how to troubleshoot issues to keep your server running smoothly and efficiently.

NGINX Log Monitoring: What It Is, How to Get Started, and Fix Issues

Ensuring that your web applications run smoothly and securely is essential. NGINX, known for its high performance and scalability, plays a key role in delivering web content. But to keep everything running efficiently, you need to monitor and analyze its logs properly.

This guide will walk you through how to configure, analyze, and make the most of NGINX logs to stay on top of your server’s health.

What Are NGINX Logs?

NGINX uses two main types of logs to track activity:

  1. Access Logs: These logs every client request the server handles. They give you information like:
    • The client’s IP address
    • The requested URLs
    • Response status codes
    • User-agent details (browser or device type)
  2. Error Logs: These logs record any server-side problems, such as:
    • Configuration errors
    • Failed requests
    • Other issues that could affect server performance

By default, NGINX stores these logs in the /var/log/nginx/ directory, with access.log for access logs and error.log for error logs.

💡
For a detailed look at real-time error log monitoring, check out this guide from Last9.

How to Configure NGINX Logging

Proper configuration of NGINX logs is essential for effective monitoring and troubleshooting.

Setting Up the Access Log

The access_log directive in NGINX lets you specify the location and format of access logs.

By default, NGINX uses the “combined” log format, which includes detailed information about each request.

Example Configuration:

http {
    log_format combined '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    server {
        access_log /var/log/nginx/access.log combined;
        ...
    }
}

In this configuration, the log_format directive defines the structure of the log entries, and the access_log directive specifies the file path and format to be used.

Customizing Log Formats

NGINX allows you to create custom log formats to capture specific information tailored to your monitoring needs.

Example: Logging Gzip Compression Ratio

http {
    log_format compression '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$gzip_ratio"';
    server {
        gzip on;
        access_log /var/log/nginx/access.log compression;
        ...
    }
}

In this example, a custom log format named compression is defined to include the $gzip_ratio variable, which gives insights into the effectiveness of gzip compression for each request.

💡
To learn more about log parsing fundamentals, check out this guide from Last9.

Setting Up the Error Log

The error_log directive configures the logging of server errors. You can specify the log file's location and the severity level of messages to be logged.

Example Configuration:

http {
    error_log /var/log/nginx/error.log warn;
    ...
}

How to Analyze NGINX Logs

Several tools can assist in parsing and visualizing log data.

GoAccess: Real-Time Log Analyzer

GoAccess is an open-source, real-time log analyzer that provides a comprehensive dashboard for monitoring web server metrics.

Installation:

  • For Debian-based systems:
sudo apt-get update && sudo apt-get install goaccess -y
  • For Red Hat-based systems:
sudo yum install goaccess -y

Usage:

goaccess /var/log/nginx/access.log --log-format=COMBINED -o /var/www/html/report.html

This command generates an HTML report at /var/www/html/report.html, which can be viewed in a web browser for real-time insights.

💡
For an overview of syslog levels and their importance, check out this guide from Last9.

ngxtop: Real-Time Monitoring

ngxtop is a flexible, real-time monitoring tool that parses NGINX access logs and provides valuable metrics.

Installation:

sudo pip3 install ngxtop

Usage:

ngxtop

Running ngxtop without arguments will display a summary of request counts, requested URIs, and status codes, updating in real-time.

Last9: Cloud-Native Log Monitoring

Last9 is a modern observability platform designed to help you monitor NGINX logs in cloud-native environments. It provides deep insights into server performance, security, and user behavior.

Features:

  • Real-time monitoring and alerting
  • Visualizations and dashboards to track log metrics
  • Integrated with cloud services for seamless observability

Example: Here's how you can use Last9 for analyzing logs

Step 1: Install the Last9 Agent

The first step is to install the Last9 agent on your server. You can typically do this using a package manager or script provided by Last9.

For example, on a Debian-based system:

curl -sL https://install.last9.io | bash

Step 2: Configure NGINX Log Forwarding

Once the agent is installed, you need to forward NGINX logs to Last9. Update the NGINX configuration file (nginx.conf) to ensure the logs are in the correct format and can be forwarded.

Add the following configuration to NGINX to enable log forwarding to Last9:

http {
    log_format last9 '$remote_addr - $remote_user [$time_local] "$request" $status '
                      '$body_bytes_sent "$http_referer" "$http_user_agent" "$gzip_ratio" '
                      '$request_time';

    access_log /var/log/nginx/access.log last9;
}

This configuration sends data such as request time, client IP, and user agent, which Last9 can use for deeper insights.

💡
To explore how to use Pino Pretty for better log visualization, check out this guide from Last9.

Step 3: Configure the Last9 Agent to Collect Logs

You will need to configure the Last9 agent to collect and send the logs from /var/log/nginx/access.log. In most cases, the agent has a configuration file (/etc/last9/config.yml or similar) where you specify the log files to monitor.

Here’s an example configuration snippet for the Last9 agent:

logs:
  - path: /var/log/nginx/access.log
    type: nginx
    service: my-nginx-service
    metrics:
      - request_time
      - gzip_ratio
      - status
      - body_bytes_sent

Step 4: Monitor in Last9

Once the logs are being forwarded, you can use the Last9 dashboard to view real-time logs, set up alerts, and analyze performance data.

Troubleshooting with Log Tracing

When diagnosing issues in an NGINX-powered environment, log tracing is a crucial technique that helps track the sequence of events leading to errors or performance bottlenecks.

Steps for Effective Log Tracing

1. Identify the Relevant Logs

NGINX generates two primary log files:

  • Access logs (access.log) – Records every request made to the server, including IP addresses, request methods, response codes, and user agents.
  • Error logs (error.log) – Captures warnings, critical errors, and failures that may affect server performance.

2. Use Timestamps for Event Correlation

When troubleshooting, align timestamps between access and error logs to trace the sequence of events leading up to an issue. For example, if users report slow response times, you can compare access logs with error logs during that period.

💡
For a comprehensive guide on event logs in Windows, check out this article from Last9.

3. Use Log Filtering and Search Commands

You can use various command-line tools to search through your logs:

  • Grep for specific requests or errors:
grep "500" /var/log/nginx/error.log
  • Tail logs in real-time for live debugging:
tail -f /var/log/nginx/access.log
  • Use awk for structured analysis:
awk '{print $9, $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr
  • This command helps find frequently occurring HTTP response codes and their associated URLs.

4. Trace Request Flow with Unique Identifiers

If your application assigns request IDs (e.g., via NGINX $request_id), use them to trace the entire request lifecycle across logs.

Example:

grep "request_id=abc123" /var/log/nginx/*.log

This method helps link a specific request across different log files, making debugging more efficient.

5. Look for Patterns and Recurring Errors

Identifying repeated errors (e.g., gateway timeouts, and connection resets) can indicate persistent configuration issues, upstream failures, or security threats.

6. Use Visual Log Viewers for Deeper Insights

While command-line tools are powerful, log visualization tools provide dashboards and analytics to streamline log analysis. Some popular tools include:

  • GoAccess – A real-time log analyzer with an interactive dashboard.
  • Last9 – A cloud-native telemetry data observability platform that helps you monitor NGINX logs with real-time insights, visualizations, and detailed metrics. It provides a modern, integrated solution for log tracing and performance monitoring.
  • ELK Stack (Elasticsearch, Logstash, Kibana) – A robust log management platform with powerful searching and visualization capabilities.
  • Datadog – A comprehensive monitoring and analytics tool.

Best Practices for NGINX Log Management

To ensure efficient log management and analysis, consider the following best practices:

1. Log Rotation

Implementing log rotation is essential to prevent log files from consuming excessive disk space over time. Tools like logrotate can automate the process of rotating, compressing, and deleting old log files, ensuring that the system doesn’t run out of storage.

Example: Logrotate Configuration for NGINX

Here's an example configuration for NGINX log rotation using logrotate:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        /usr/sbin/service nginx reload > /dev/null 2>/dev/null || true
    endscript
}

This configuration rotates NGINX logs daily, keeps 30 days of logs, compresses old logs, and reloads NGINX after rotation to ensure logs are written to new files.

💡
To learn more about log rotation in Linux, check out this guide from Last9.

2. Log Level Configuration

Set appropriate log levels in the error_log directive to capture only necessary information, without overwhelming the log files with trivial data. This ensures that logs remain manageable and relevant.

Example: Setting Log Level for Error Logs

error_log /var/log/nginx/error.log warn;

In this configuration, only logs with a severity level of warn or higher will be captured. Adjust the log level based on your needs:

  • debug – Most detailed logs, useful for debugging.
  • info – General information, but not too verbose.
  • warn – Warnings that might require attention.
  • error – Error messages that affect the operation of the server.
  • crit – Critical errors, such as system failures.
  • alert – Alerts for critical issues that need immediate attention.
  • emerg – Emergency messages.

3. Regular Monitoring

Establish routine monitoring to promptly identify and respond to potential issues. Setting up automated alerts for critical errors or performance bottlenecks ensures that any issues are addressed before they affect server performance.

Example: Automating Log Monitoring with Last9

Using tools like Last9, you can automate log monitoring and receive real-time alerts for specific log patterns or anomalies:

logs:
  - path: /var/log/nginx/access.log
    type: nginx
    service: my-nginx-service
    alerts:
      - threshold: "response_time > 500ms"
        action: "notify"

This configuration will notify you when the response time exceeds 500ms, enabling proactive monitoring.

💡
For a deeper understanding of application logs and how to manage them, check out this guide from Last9.

4. Centralized Log Management

Consider centralizing your logs to a single platform for easier access, analysis, and reporting. Tools like ELK Stack, Last9, or Datadog can aggregate logs from multiple NGINX servers, offering a unified view of your environment.

5. Secure Your Logs

Ensure that your logs are protected from unauthorized access or tampering. Restrict access to log files using appropriate file permissions and consider encrypting sensitive log data, especially if it contains personally identifiable information (PII) or other confidential details.

Example: Restricting Access to Logs

chmod 640 /var/log/nginx/*.log
chown root:adm /var/log/nginx/*.log

This configuration restricts access to log files, ensuring that only the root user and users in the adm group can access them.

Conclusion

To wrap up, NGINX log monitoring is crucial for maintaining server performance and quickly resolving issues.

Understanding the different types of logs, setting up proper configurations, and using the right tools will help you efficiently monitor and troubleshoot your NGINX setup.

💡
And if you’d like to discuss further, our Discord community is always open. We have a dedicated channel where you can connect with other developers and dive into your specific use case.

Contents


Newsletter

Stay updated on the latest from Last9.