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.
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 -
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]
log_format myapilogformat '$remote_addr - $remote_user xxx[$time_local]xxx '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $response_time ';
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;
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: