0
0
NginxHow-ToBeginner · 3 min read

Where Are Nginx Logs Stored: Default Locations and Configuration

Nginx logs are stored in files specified by the access_log and error_log directives in the nginx.conf file. By default, access logs are in /var/log/nginx/access.log and error logs in /var/log/nginx/error.log.
📐

Syntax

The access_log and error_log directives define where nginx writes its logs.

  • access_log <file_path> [format]; sets the access log file and optional format.
  • error_log <file_path> [level]; sets the error log file and optional log level.

These directives are usually placed in the http, server, or location blocks in nginx.conf.

nginx
access_log /var/log/nginx/access.log combined;
error_log /var/log/nginx/error.log warn;
💻

Example

This example shows a basic nginx.conf snippet configuring log file locations for access and error logs.

nginx
http {
    access_log /var/log/nginx/access.log combined;
    error_log /var/log/nginx/error.log warn;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}
⚠️

Common Pitfalls

Common mistakes when dealing with nginx logs include:

  • Not having write permissions for the log directory, causing nginx to fail writing logs.
  • Forgetting to reload nginx after changing log paths, so changes don't take effect.
  • Using relative paths instead of absolute paths, which nginx does not support for logs.

Always use absolute paths and ensure the nginx user can write to the log files.

nginx
## Wrong: relative path (will not work)
access_log logs/access.log;

## Right: absolute path
access_log /var/log/nginx/access.log;
📊

Quick Reference

Summary of default nginx log locations and directives:

DirectiveDefault LocationDescription
access_log/var/log/nginx/access.logStores client request logs
error_log/var/log/nginx/error.logStores error messages and warnings

Key Takeaways

Nginx logs are controlled by the access_log and error_log directives in nginx.conf.
Default log files are /var/log/nginx/access.log for access logs and /var/log/nginx/error.log for error logs.
Always use absolute paths for log files and ensure nginx has write permissions.
Reload nginx after changing log file locations to apply changes.
Check log file permissions if logs are not being written as expected.