Nginx HTTP server has an incredible logging center which is extremely personalized. In this short article, we will explain how to configure you own formats for access and error logs for Nginx in Linux.The goal of this guide is to assist you understand how logs are created, so as to configure customized log formats for purposes of debugging, troubleshooting or analysis of what unfolds within your web server as well as web applications (such as tracing demands).

Read Likewise: 4 Excellent Open Source Log Monitoring and Management Tools for Linux

This short article is made of three sections which will enlighten you about setting up access/error logs and the best ways to enable conditional logging in Nginx.Configuring Access

Logs in Nginx Under Nginx

, all client requests to the server are recored in the gain access to log in a specified format using the ngx_http_log_module module.The default

log file is log/access. log (generally / var/log/nginx/ access_log on Linux systems) and the default format for logging is generally the combined or primary format (this can differ from one distro to another).

The access_log instruction (relevant in the http, server, location, if in area and limit except context) is utilized to set the log file and the log_format regulation (relevant under the http context only) is utilized to set the log format. The log format is described by common variables, and variables that generated just at the time when a log is written.The syntax for setting up a log format is: log_format format_name'

set_of_variables_to_define_format';. and the syntax for setting up gain access to log is: access_log/ path/to/log _ file format_name; #simplest kind. OR. access_log/ path/to/log _ file [format [buffer= size] [gzip [= level]] [flush= time] [if= condition]];.

The following is a excerpt from the default Nginx configuration file / etc/nginx/nginx. conf on CentOS 7.

/ etc/nginx/nginx. conf

http nginx

This log format yields the following log entry.

dbmanager [20/Nov/2017:18:52:17 +0000] "GET/ HTTP/1.1" 401 188 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; recreational vehicle:47.0) Gecko/20100101 Firefox/47.0".

The following is another useful logging format which we utilize for tracing demands to our web applications using the some of the default variables, it most importantly has the request ID and logs client location information (country, nation code, area and city).

/ etc/nginx/nginx. conf

log_format customized '$ remote_addr - $remote_user [$ time_local].
'"$ request" $status $body_bytes_sent '.
'"$ http_referer" "$ http_user_agent" '.
'"$ http_x_forwarded_for" $request_id '.
'$ geoip_country_name $geoip_country_code '.
'$ geoip_region_name $geoip_city ';.

You can utilize it like this:

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

This will produce a log entry which appears like this.

- [21/Nov/2017:08:45:45 +0000] "POST/ ngx_pagespeed_beacon? url= https% 3A% 2F% 2Fwww. 2Fads% 2Ffresh-oranges-1509260795 HTTP/2.0" 204 0 "" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0" "-" a02b2dea9cf06344a25611c1d7ad72db Uganda UG Kampala Kampala.

You can define several logs using the access_log regulations on the same level, here we are utilizing more than one log file in the http context.

/ etc/nginx/nginx. conf

http access.log;.
## this uses the our custom log format.

The following are more sophisticated logging setups examples, which are beneficial for log formats which contain compression-related variables and for developing compressed log files:

access_log/ var/log/nginx/ custom_log customized buffer 32k;.
access_log/ path/to/log. gz compression gzip flush= 5m;.

Setting Up Mistake Logs in Nginx

In case Nginx experiences any glitches, it records information worrying them in the mistake log. These concerns fall under various seriousness levels: debug, info, see, caution, mistake (this is the default level and works worldwide), crit, alert, or emerg.

The default log file is log/error. log, but it is usually located in / var/log/nginx/ on Linux distributions. The error_log instruction is used to define the log file, and it can be used in the main, http, mail, stream, server, place context (in that order).

You should likewise keep in mind that:

  • Setups in the main context are constantly acquired by lower levels in the order above.and setups in the lower levels override the setups inherited from the greater levels.You can set up mistake logging using the following syntax:

    error_log/ path/to/log _ file log_level;. :

    error_log/ var/log/nginx/ error_log caution;

    . This will instruct Nginx to log all messages of type alert and

    more severe log level crit, alert, and emerg messages.In the next example, messages of crit, alert, and emerg levels will be logged.error _ log/ var/www/example1. com/log/error _ log crit;. Consider the configuration listed below

    , here, we have defined error visiting various levels( in the http and server context). In case of an error, the message is composed to only one error log, the one closest to the level

    where the mistake has appeared./ etc/nginx/nginx. conf http log_format compression'$ remote_addr- $remote_user [$ time_local].'"$ request"$ status $body_bytes_sent'.' "$ http_referer" "$ http_user_agent ""$ gzip_ratio "';.< If you utilize more than one error_log regulations as in the setup listed below( same level), the messages are composed to all specified logs./ etc/nginx/nginx. conf server pre>

    listen 80;. server_name;. error_log Configuring Conditional Logging in Nginx In some
    cases, we may want Nginx to perform conditional logging of messages. Not every message has to
    be logged by Nginx, for that reason we can overlook unimportant or less essential log entries from our gain access to logs for particular instances.We can utilize the ngx_http_map_module module which develops variables whose values depend upon worths of other variables. The specifications inside a map block (which need to exist in the http material only) specify a mapping in between source and resulting values.For this sort of setting, a demand will not be logged if the condition assesses to" 0" or an empty string. This example leaves out demands with HTTP status codes 2xx and 3xx./ etc/nginx/nginx. conf http map$ status $condition < Here is another helpful example for debugging a web application in a development stage. This will overlook all messages

    and just log debug info./ etc/nginx/nginx.
    conf http
    map $information$ debuggable

    server log You can find out more information

    , including logging to syslog

    < a href= target= _ blank rel
    =" nofollow noopener" > here. That recommends now! In this guide, we described the best ways to set up custom-made logging format for gain access to and error logs in Nginx. Utilize the feedback form below to ask
    questions or

    share you ideas about this article.!How ToComputing,Forward proxy,Free software,Internet protocols,Login,Network management,Nginx,Reverse proxy,Server log,Software,Syslog,Web server
    Nginx HTTP server has an incredible logging center which is extremely personalized. In this short article, we will explain how to configure you own formats for access and error logs for Nginx in Linux.The goal of this guide is to assist you understand how logs are created, so as...