Log data in Nginx

Guide how to log data (POST, GET) in Nginx.

By default, Nginx does not log POST data. However, you can update your nginx.conf configuration file to enable POST and GET data logging.

Log POST data

To log POST data, update the nginx.conf file at the http block directive. This code snippet will log POST data to the access.log log file.

http {
    log_format custom_post_log '$request_body';
    access_log /var/log/nginx/access.log custom_post_log;

    server {
        location / {
            echo_read_request_body;
            default_type text/html;
        } 
    }
}

It's important to note the following:

  • The log_format and access_log parameters must be placed inside the http {} block directive.
  • Then, it's important to add in echo_read_request_body paramater (without value) must be placed in the location {} block directive.

Another code snippet you can use to enable POST data logging is the following, where the POST data will be logged into a different log file, the postdata.log file:

server {
    location = / {
        log_format custom_post_data_log $request_body;
        access_log /var/log/nginx/postdata.log custom_post_data_log;
    }
}

Log GET data

To log GET data, update your nginx.conf configuration file to log any GET data to your access.log log file:

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

It's important to note the following:

  • The log_format and access_log parameters must be placed inside the http {} block directive.

You can format the GET data as needed:

'$time_local "$request" $status '  
'"$http_referer" '
'"$http_user_agent" $request_body';

Logging for parameters

Parameters
$remote_addr
$remote_user
$time_local
$request
$status
$body_bytes_sent
$http_referer
$http_user_agent
$gzip_ratio
$upstream_connect_time
$upstream_header_time
$upstream_response_time
$request_time
$ssl_protocol
$ssl_cipher
$clientid