Sub topic 10
Separate Logs for Each Site
Assume you are comfortable with server blocks, systemctl, sudo, and basic HTTP requests. This chapter focuses on finding the right log when one Nginx server hosts several sites.
Why separate logs matter
Nginx writes two main kinds of logs:
- An access log records completed HTTP requests. It normally includes the client address, request path, status code, response size, and request time.
- An error log records operational problems, such as missing files, permission failures, upstream connection errors, and configuration-related warnings.
A shared log can show that something failed, but it makes site-specific investigation harder. Separate files let you inspect only the requests and errors for one domain.
The access_log and error_log directives can be placed inside a server block. They apply to that site unless a more specific location block overrides them.
Configure logs in a server block
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log warn;
location / {
try_files $uri $uri/ =404;
}
}The warn value sets the minimum severity recorded in the error log. Common levels include debug, info, notice, warn, error, crit, alert, and emerg. A warn log also includes more severe messages, such as error and crit.
The access log uses the log format configured elsewhere, usually in the http block. Defining a different file does not automatically create a different format.
Create, validate, and observe the logs
Create the log directory if your distribution does not already provide it. Then test the configuration before reloading:
sudo install -d -m 0755 /var/log/nginx
sudo nginx -t
sudo systemctl reload nginxGenerate a request and inspect the result:
curl -i -H 'Host: example.com' http://127.0.0.1/
sudo tail -n 20 /var/log/nginx/example.com.access.log
sudo tail -n 20 /var/log/nginx/example.com.error.logThe Host header ensures that Nginx selects the example.com server block when testing locally. A successful request should add a line to the access log. If the requested file does not exist, the access log should show status 404, and the error log will often identify the path Nginx tried to open.
Understand inheritance and log rotation
An access_log or error_log declared in the http block provides a default for server blocks that do not declare their own file. A declaration inside a server block replaces that inherited setting for the site.
Log files grow continuously, so configure logrotate, which periodically renames old files, compresses them, and asks Nginx to reopen the log paths. Do not delete an active log file and assume Nginx will automatically switch to a new file. After manual rotation, use:
sudo nginx -s reopenThe distribution's existing /etc/logrotate.d/nginx configuration usually handles this safely.
Troubleshooting missing entries
If no access entry appears, check that the request reached this server block. Test the Host header, inspect the enabled configuration, and look for a competing default_server.
If the access log exists but the error log does not, the request may have succeeded without an error. If neither file exists, check the paths, run nginx -t, and inspect the service journal:
sudo journalctl -u nginx --since "10 minutes ago"Exercise
Which directive records normal HTTP requests, and which directive records Nginx problems?
Show answer
access_log records requests, while error_log records Nginx errors and operational messages.