Excellent software and practical tutorials
What is Nginx log
Nginx is an open source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. When administering an NGINX web server, one of the most common tasks you will perform is checking log files.
Knowing how to configure and read logs is very useful when troubleshooting server or application problems because they provide detailed debugging information.
Nginx records its events in two types of logs:Access logs and error logs. Access logs record information about client requests, and error logs record information about server and application problems.
Configuring Access Logs
Whenever a client request is processed, Nginx generates a new event in the access log. Each event record contains a timestamp and includes various information about the client and the requested resource. The access log can show the location of the visitor, the page they visited, how long they spent on the page, etc.
The log_format directive allows you to define the format in which logged messages are logged. The access_log directive enables and sets the location of the log file and the format used.
The most basic syntax of the access_log directive is as follows:
access_log log_file log_format;
Where log_file is the full path to the log file and log_format is the format used by the log file.
Access logging can be enabled in http, server or location directive blocks.
By default, access logs are enabled globally by the http directive in the main Nginx configuration file.
/etc/nginx/nginx.conf
http {
...
access_log /var/log/nginx/access.log;
...
}
For better maintainability, it is recommended to set a separate access log file for each server block. An access_log directive set in a server directive will override one set in an http (higher level) directive.
/etc/nginx/conf.d/domain.com.conf
http {
...
access_log /var/log/nginx/access.log;
...
server {
server_name domain.com
access_log /var/log/nginx/domain.access.log;
...
}
}
If the log format is not specified, Nginx will use a predefined combined format as follows:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
To change the logging format, override the default setting or define a new one. For example, to define a new logging format called main that extends the combined format with the value of the displayed X-Forwarded-For header, add the following definition in the http or server directive:
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
To use the new format, specify its name after the log file, as follows:
access_log /var/log/nginx/access.log custom;
Although the access log provides very useful information, it takes up disk space and can affect server performance. If your server is low on resources and your website is busy, you may want to disable the access log. To do this, set the value of the access_log directive to off:
access_log off;
Configuring Error Logging
Nginx writes messages about application and general server errors to the error log file. If you encounter errors in your web application, the error log is the first place you should start troubleshooting.
The error_log directive enables and sets the location and severity level of the error log. It takes the following format and can be set in an http, server, or location block:
error_log log_file log_level
The log_level parameter sets the logging level. Here are the levels listed in order of severity from low to high:
- debug - debug messages.
- info - Informational messages.
- notice - announcement.
- warn - warning.
- error - an error occurred while processing the request.
- crit - A critical issue. Requires immediate action.
- alert - Alert. Immediate action is required.
- emerg - Emergency. The system is in an unusable state.
Each log level includes higher levels. For example, if you set the log level to warn, Nginx will also log error, crit, alert, and emerg messages.
When the log_level parameter is not specified, the default is error.
By default, the error_log directive is defined in the http directive inside the main nginx.conf file:
/etc/nginx/nginx.conf
http {
...
error_log /var/log/nginx/error.log;
...
}
Same as the access log, it is recommended to set up a separate error log file for each server block, which will override settings inherited from higher levels.
For example, to set the error log for domain.com to warn, you would use:
http {
...
error_log /var/log/nginx/error.log;
...
server {
server_name domain.com
error_log /var/log/nginx/domain.error.log warn;
...
}
}
Whenever you modify the configuration file, you must restart the Nginx service for the changes to take effect.