In this section, we will explore how NGINX handles logging, the different types of logs it can generate, and how to customize log formats to suit your needs. Logging is crucial for monitoring the performance of your server, diagnosing issues, and understanding user behavior.
Key Concepts
- Access Logs: These logs record all requests processed by the server.
- Error Logs: These logs capture any errors encountered by the server.
- Log Formats: Customizable formats for how log entries are recorded.
Access Logs
Access logs provide detailed information about each request made to the server. By default, NGINX logs this information in a predefined format, but you can customize it to include additional details.
Default Access Log Configuration
The default access log configuration is usually found in the main configuration file (nginx.conf
):
http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; }
Explanation
- log_format: Defines a custom log format named
main
. - access_log: Specifies the file where access logs are stored and the format to use.
Customizing Access Logs
You can customize the log format to include additional information or to change the format of existing fields. For example:
http { log_format custom '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$request_time $upstream_response_time'; access_log /var/log/nginx/custom_access.log custom; }
In this example, we added $request_time
and $upstream_response_time
to the log format to capture the time taken to process the request and the time taken by upstream servers.
Error Logs
Error logs capture any issues encountered by the server, such as configuration errors, failed requests, or other runtime issues.
Default Error Log Configuration
The default error log configuration is also found in the main configuration file (nginx.conf
):
Explanation
- error_log: Specifies the file where error logs are stored and the log level.
- Log Levels: The log level can be set to
debug
,info
,notice
,warn
,error
,crit
,alert
, oremerg
.
Customizing Error Logs
You can change the log level to capture more or less detail:
In this example, the log level is set to info
, which will capture informational messages, warnings, and errors.
Practical Example
Let's create a simple NGINX configuration that includes custom access and error logs.
Configuration File (nginx.conf
)
http { log_format custom '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$request_time $upstream_response_time'; access_log /var/log/nginx/custom_access.log custom; error_log /var/log/nginx/custom_error.log info; server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html; } } }
Explanation
- log_format custom: Defines a custom log format.
- access_log: Specifies the custom access log file and format.
- error_log: Specifies the custom error log file and log level.
- server: Defines a simple server block that serves static content.
Exercises
Exercise 1: Custom Access Log Format
- Modify the
nginx.conf
file to include the$http_host
and$request_length
variables in the access log format. - Restart NGINX and make a few requests to your server.
- Check the access log to verify the new format.
Solution
http { log_format custom '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$http_host $request_length'; access_log /var/log/nginx/custom_access.log custom; error_log /var/log/nginx/custom_error.log info; server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html; } } }
Exercise 2: Change Error Log Level
- Change the error log level to
debug
. - Restart NGINX and check the error log for detailed debug information.
Solution
Common Mistakes and Tips
- Incorrect Log Format Syntax: Ensure that the log format syntax is correct. Missing or extra characters can cause errors.
- Log File Permissions: Make sure that NGINX has the necessary permissions to write to the log files.
- Log Rotation: Implement log rotation to prevent log files from growing too large and consuming disk space.
Conclusion
In this section, we covered the basics of NGINX logging, including access logs, error logs, and how to customize log formats. Proper logging is essential for monitoring and troubleshooting your server. In the next module, we will explore how to use NGINX as a reverse proxy.