How to customize the access log in NGINX?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (RHEL)
  • Red Hat Software Collections (RHSCL)
  • NGINX Web Server

Issue

  • How to enable friendly access log times in NGINX?

Resolution


Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted This content is not included.Service Level Agreements and This content is not included.Scope of Coverage. Red Hat does not support software contained in the EPEL repository, or other third-party repositories, or from third-party non-Red Hat sources. The information is provided as-is without representations or warranties about the suitability or accuracy of the information provided. The intent is to provide information to accomplish the system's needs. Use of the information below is at the user's own risk.

Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.


This solution is part of the [Master] Configuring Access Logs in Red Hat Middleware Products..

Update the log_format option in NGINX_HOME/nginx/conf.d/nginx.conf

This is the default configuration:

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

Change it with the following:

...
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" '
                  'cookie="$http_cookie" set-cookie="$sent_http_set_cookie" rt="$request_time"';
...

If you're using the upstram module (nginx-mod-stream) available for NGINX 1.10 or later, you can add the upstream time information as follows:

...
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" '
                  'cookie="$http_cookie" set-cookie="$sent_http_set_cookie" rt="$request_time" '
                  'uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
...

For more information about NGINX logging, refer the official docs:

Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.