How to create custom Nginx log file

Rajarshi Ghosh
Sep 3, 2018 · 2 min read

Nginx is one of the most widely used web server, regarded highly for its performance & simplicity. Log files are a common requirement for web server configurations. Let’s see how can we create nginx log files with customised values.

“turned on laptop computer” by Fabian Grohs on Unsplash

For example. let’s say we have setup a web API with Nginx. Now we need to monitor various metrics like number of API access, user-agent, response status etc. Below are the steps -

  1. Define access & error log file: As per default nginx configuration at /etc/nginx/nginx.conf, below log files are defined
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

We can define different log files as well, in virtual blocks configuration, at /etc/nginx/sites-available/[virtual_block_file]

2. Create one or more custom log format: We can create log format specifications with a name.

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

3. Update log file with log format: Now we have to update the access_log / error_log to work as per provided log format as below

access_log /var/log/nginx/access.log myapilogformat;

4. Save & test if no error present:

nginx -t   #tests all nginx configurations

If error shown, check for syntax/ spelling errors etc. If no error found, restart nginx

sudo service nginx restart

Reference:

  1. Nginx log format options here

Let me know if you found it useful & any feedback!

Rajarshi Ghosh

Written by

Avid reader, Software Developer, explorer at heart

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade