Dump the code

Nginx log files and formats

Created 8 months ago
Posted By admin
5min read
Nginx generates log files to record information about server activities, request processing, errors, and more. The log files provide valuable insights for troubleshooting, performance monitoring, and security analysis. Here are some commonly used Nginx log files and log formats:

Access log:
Default Path: /var/log/nginx/access.log
Records information about client requests, including IP addresses, request methods, requested URLs, and response statuses.

   log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
   
   access_log /var/log/nginx/access.log main;
The main log format includes details like remote address, user, timestamp, request method, URL, status code, and more.

Error log:
Default Path: /var/log/nginx/error.log
Captures error messages and diagnostic information about server issues.

   error_log /var/log/nginx/error.log;
By default, the error log records various error messages, warnings, and critical alerts.

Virtual host log:
Nginx allows separate access and error logs for each virtual host.

   server {
       listen 80;
       server_name yourdomain.com;
   
       access_log /var/log/nginx/yourdomain-access.log;
       error_log /var/log/nginx/yourdomain-error.log;
   
       # Other server configurations...
   }
 Customize log paths for each virtual host to isolate logs.

Custom log formats:
Define custom log formats to capture specific information.

   log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for" '
                     '"$upstream_addr" "$upstream_response_time"';
   
   access_log /var/log/nginx/custom.log custom;
Customize log formats using variables to include desired details.

Combined log format:
A commonly used log format that combines standard Apache and Nginx log formats.

   log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
   
   access_log /var/log/nginx/access.log combined;
Similar to the main log format but follows the combined log format convention.

Logging real IP addresses:
If Nginx is behind a proxy, log the real client IP addresses.

   set_real_ip_from 192.168.1.0/24;
   real_ip_header X-Forwarded-For;
Configure Nginx to log the actual client IP addresses when using a proxy.

Buffered logging:
Enable buffered logging for improved performance in high-traffic scenarios.

   access_log /var/log/nginx/access.log main buffer=32k;
Use the buffer parameter to specify the buffer size.

Disabling logs:
Temporarily disable logging for specific requests or locations.

   location /path/to/disable/logs {
       access_log off;
       # Other configurations...
   }
Use access_log off; to disable logging for specific locations.

Log rotation:
Implement log rotation to manage log file sizes and prevent them from consuming too much disk space.

   error_log /var/log/nginx/error.log notice;
   access_log /var/log/nginx/access.log main;
Include log rotation configurations in your log paths.

Logging variables:
Nginx provides various logging variables that can be used in log formats to capture specific information.

    log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '"$upstream_addr" "$upstream_response_time"';
Examples include $upstream_addr and $upstream_response_time for proxy-related information.

Logging timestamps:
Customize timestamp formats in logs.

    log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    log_format iso8601 '$remote_addr - $remote_user [$time_iso8601] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log /var/log/nginx/custom.log custom;
    access_log /var/log

/nginx/iso8601.log iso8601;
Use `$time_iso8601` for ISO 8601 format timestamps.


These examples cover various aspects of Nginx logging, including customizing log formats, managing log paths, dealing with virtual hosts, and addressing common logging scenarios. Adapt these configurations to meet the specific needs of your environment and logging requirements.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles