How to Configure nginx Custom Access and Error Log Formats
4.8/5 - (28 votes)

Last Updated on August 10, 2023 by Vikash Ekka

In Nginx, custom Nginx Access and Error logs are a way to define and configure the format of log entries generated by the server. These logs contain information about various events and activities that occur on your Nginx server, such as incoming requests, server responses, errors, and more. Customizing the log format allows you to tailor the log entries to your specific needs and requirements.

Also Read:

To configure custom logs in Nginx on Ubuntu, follow these steps:

Step 1. Open Terminal:
Access your Ubuntu server through SSH or directly if you’re using a local machine.

Step 2. Navigate to Nginx Configuration Directory:
The Nginx configuration files are typically located in the /etc/nginx/ directory.

Step 3. Edit the Configuration File:
The main Nginx configuration file is usually named nginx.conf or split into multiple files in the conf.d/ directory. You’ll need root privileges to edit these files, so use a command like sudo nano nginx.conf.

Step 4. Add or Modify Custom Log Configuration:
Inside the configuration file, you’ll find a section related to server blocks. Each server block represents a virtual host configuration. Locate the server block where you want to configure the custom log.

To add a custom log, use the access_log directive followed by the desired log file path and format. For example:

server {
    listen 80;
    server_name vetechno.in;

    access_log /var/log/nginx/vetechno-custom-access.log vetechno_custom_format;
    
    # Rest of your server configuration...
}

In this example, replace vetechno.in with your server’s domain or IP, /var/log/nginx/vetechno-custom-access.log with the path to the log file you want to create, and vetechno_custom_format with the desired log format.

Step 5. Define Custom Log Format:
Below your server block(s), you can define the custom log format using the log_format directive. For instance:

http {
    # ... other configurations ...

    log_format vetechno-custom_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
}

In this example, the log_format directive creates a custom log format called vetechno_custom_format. The format string includes various placeholders that will be replaced with actual values when a log entry is generated.

Step 6. Save and Exit:
After making changes to the configuration file, save the changes (usually Ctrl + O in nano) and exit the editor (usually Ctrl + X in nano).

Step 7. Test Configuration and Restart Nginx:
To ensure that your changes are valid, test the Nginx configuration:

sudo nginx -t

Step 8. If the test passes, you can restart Nginx to apply the changes:

sudo service nginx restart

Nginx Custom Access Log Format (timed_combined):

This log format creates a JSON-like format for the access log entries. The placeholders are used to capture various information about the incoming requests and responses, including timestamps, client information, request details, response status, and more.

log_format timed_combined '{ "time_kcs": "$time_local", '
                         '"remote_addr_kcs": "$remote_addr", '
                         '"remote_user_kcs": "$remote_user", '
                         '"body_bytes_sent_kcs": "$body_bytes_sent", '
                         '"request_time_kcs": "$request_time", '
                         '"host_called_kcs": "$host", '
                         '"status_kcs": "$status", '
                         '"request_uri_kcs": "$request_uri", '
                         '"request_method_kcs": "$request_method", '
                         '"http_referrer_kcs": "$http_referer", '
                         '"http_user_agent_kcs": "$http_user_agent" }';

In this custom log format, the placeholders are used as follows:

  • $remote_addr: IP address of the client.
  • $remote_user: Authenticated user, if any.
  • $time_local: Local time when the request was received.
  • $request: The full request line, including method, URI, and HTTP version.
  • $status: HTTP status code returned to the client.
  • $body_bytes_sent: Number of bytes sent to the client in the response body.
  • $http_referer: Referer header from the client’s request.
  • $http_user_agent: User-Agent header from the client’s request.

Nginx Custom Error Log Format:

log_format custom_error '$time_local [$level] $remote_addr '
                      '[$request_uri] [$server_name] [$status] '
                      '[$request_body] [$http_referer] "$error_msg"';

http {
    # ... other configurations ...

    log_format main '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

    error_log /var/log/nginx/custom-error.log custom_error;

    # Rest of your configuration...
}

In this custom error log format, the placeholders are used as follows:

  • $time_local: Local time when the error occurred.
  • $level: Log level of the error (e.g., “warn”, “error”).
  • $remote_addr: IP address of the client.
  • $request_uri: URI of the request that caused the error.
  • $server_name: Server name handling the request.
  • $status: HTTP status code returned to the client.
  • $request_body: Request body data (useful for debugging).
  • $http_referer: Referer header from the client’s request.
  • $error_msg: The error message.

Remember to adjust the log file paths (/var/log/nginx/custom-access.log and /var/log/nginx/custom-error.log) to the actual locations where you want to store the log files.

These custom log formats provide you with detailed and specific information about both successful requests (access log) and encountered errors (error log) on your Nginx server. You can modify these examples further to suit your logging needs.

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of https://www.vetechno.in. I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

Leave a Reply

Your email address will not be published. Required fields are marked *