Configuring logs

Logging is a double-edged sword. On the one hand, it offers all kinds of useful information, but on the other hand, it has a computational cost. If the application generates thousands of log lines, the cost will have a negative impact on performance. In the following section, we will discuss NGINX directives that can be used to tweak logs.

access_log

This directive configures logging for all requests served by NGINX. The directive takes multiple parameters that can be used to configure the log path, format template, buffers, and so on. The syslog value can be used to direct logs to a syslog server rather than to a log file. Alternatively, the complete logging can be disabled using off as a value.

By default, access logs are enabled to write to the log/access.log file in the prespecified combined format. This directive is available under the limit_except, http, server, location, and if (in location section) sections of an NGINX configuration. Here's an example:

http{
   access_log logs/access.log combined;
   } 

Tip

NGINX sections can contain multiple access_log directives specifying different log destinations and, optionally, different format templates.

log_format

This directive can be used to define a template for the access_log directive. The log format can contain any of the existing variables defined in NGINX. By default, NGINX defines the combined format, which it uses in the access log directive. The directive is only available under the http section of an NGINX configuration. Here's an example:

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

log_subrequest

This directive enables the logging of subrequests in the access log directive. By default, the directive is set to off. This directive can be used to enable logging under the http, server, and location sections of an NGINX configuration. Here's an example:

http{
   log_subrequest on;
   } 

error_log

This directive can be used to configure logging in NGINX. Logs can be directed to a file or the error stream (stderr) or to a syslog server. The directive also defines a level of logging, namely debug, info, notice, warn, error, crit, alert, or emerg. Log statements with more severity than configured are logged to the specified destination.

By default, error logs are enabled, with the error level, to write to the log/error.log file. This directive is available under the global, http, server, and location sections of an NGINX configuration. Here's an example:

http{
   error_log logs/warn.log warn;
   } 

Tip

NGINX sections can contain multiple error_log directives specifying different log destinations and, optionally, different log levels.

log_not_found

This directive enables/disables the reporting of files not found (HTTP 404 errors) in the error log. By default, the directive is set to on. The directive can be used to enable logging under the http, server, and location sections of an NGINX configuration. Here's an example:

http{
   log_not_found on;
   } 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.65