Sub topic 10
Separate logs for each site
This chapter assumes you can edit Nginx configuration under /etc/nginx, reload the service with systemd, and read basic HTTP status codes. You should already have at least two server blocks working from earlier chapters. The goal is to give each site its own access and error logs, so that one site's traffic and failures can be read without filtering a shared file.
Why separate logs
By default, Nginx writes every request to one shared access log and every error to one shared error log. On Debian and Ubuntu packages these are /var/log/nginx/access.log and /var/log/nginx/error.log. With one site that is fine. With five sites, you have to filter by host name every time you investigate something.
Per-site logs give you several practical benefits:
- You can read one site's traffic without noise from the others.
- You can run log analysis tools per site.
- You can apply different retention rules to different sites.
- You can hand a site owner their own logs without exposing other sites' data.
The cost is more files to rotate, monitor and keep track of. That cost is small if you follow a naming convention.
The two directives
Two directives control logging in a server block.
access_logrecords each completed request. It takes a file path and, optionally, a format name.error_logrecords problems, such as missing files, permission failures and upstream connection errors. It takes a file path and, optionally, a minimum severity level.
Both can be set in the http, server or location context. A setting in an inner context replaces the setting from the outer one. If a server block sets no access_log, it inherits the one from the http context.
This is the example block from the plan. It gives the site app.example.net its own files and proxies to a local application:
server {
listen 80;
listen [::]:80;
server_name app.example.net;
access_log /var/log/nginx/app.example.net.access.log;
error_log /var/log/nginx/app.example.net.error.log;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}The proxy directives are covered in a later chapter. Here, only the two log lines matter.
A naming convention
Name each file after the site and the log type, for example:
| Site | Access log | Error log |
|---|---|---|
| example.com | `/var/log/nginx/example.com.access.log` | `/var/log/nginx/example.com.error.log` |
| example.org | `/var/log/nginx/example.org.access.log` | `/var/log/nginx/example.org.error.log` |
| app.example.net | `/var/log/nginx/app.example.net.access.log` | `/var/log/nginx/app.example.net.error.log` |
Keeping the files in /var/log/nginx/ matters for two reasons. The packaged log rotation rule matches /var/log/nginx/*.log, so your new files are rotated without extra work. The directory is also already writable by the Nginx master process.
How the files get created
Nginx opens log files when it starts or reloads. The master process runs as root at that moment, so it can create the file even if the directory is restricted. The worker processes, which run as an unprivileged user such as www-data, then write to the open file handle.
This explains a common failure: if the directory does not exist, nginx -t fails. If you put a log in /var/log/mysite/, create that directory first.
sudo mkdir -p /var/log/nginx
sudo nginx -tThe configuration test reports open() ... failed (2: No such file or directory) for a missing directory. Fix the path before reloading.
Putting it into practice
Add the two lines to each server block. For the static site from earlier chapters:
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;
location / {
try_files $uri $uri/ =404;
}
}Then validate, reload and send a request:
sudo nginx -t && sudo systemctl reload nginx
curl --resolve example.com:80:127.0.0.1 http://example.com/
curl --resolve example.com:80:127.0.0.1 http://example.com/missing
sudo tail -n 2 /var/log/nginx/example.com.access.log
sudo tail -n 2 /var/log/nginx/example.com.error.logThe access log shows both requests, one with status 200 and one with 404. The error log shows a "No such file or directory" entry for the missing path, at severity error.
What a request line contains
With no format named, Nginx uses the predefined combined format. A line looks like this:
127.0.0.1 - - [28/Nov/2026:04:40:12 +0000] "GET /missing HTTP/1.1" 404 153 "-" "curl/8.5.0"The fields are the client address, two identity fields that are almost always -, the time, the request line, the status, the response size in bytes, the referrer and the user agent. The host name is not in this format. In a shared log you could not tell which site answered. That is the main reason to split logs by site, or to add the host to a custom format.
Custom log formats
The log_format directive defines a named format. It is only valid in the http context, so put it in /etc/nginx/nginx.conf or in a file under conf.d/, not inside a server block.
log_format vhost '$host $remote_addr [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" rt=$request_time';Then name it in each server block:
access_log /var/log/nginx/example.com.access.log vhost;Adding $host is useful even with separate files, because requests that reach a default server or a wrong block still show the name the client asked for. The $request_time variable records seconds spent on the request, which helps when a proxied application is slow.
Error log severity
The error log takes a level after the path. From most to least verbose, the common levels are debug, info, notice, warn, error, crit, alert and emerg. The default is error. A level logs itself and everything more severe.
error_log /var/log/nginx/example.com.error.log warn;Use warn when you want more detail than the default. Avoid debug in production. It needs an Nginx binary built with debug support and produces very large output.
Turning logs off, and buffering
You can disable an access log for a context:
location = /healthz {
access_log off;
return 200 "ok\n";
}This is common for health checks, which otherwise fill a log with identical lines. There is no equivalent for the error log. Sending errors to /dev/null is possible, but it hides real problems, so avoid it.
On busy sites, writing to disk for every request costs time. The buffer and flush parameters collect lines in memory:
access_log /var/log/nginx/example.com.access.log vhost buffer=32k flush=5s;Lines may then appear up to five seconds late. Keep that in mind when you test, or you will think logging is broken.
Inheritance rules
Log directives follow Nginx's usual inheritance: an inner context that defines access_log replaces the outer definition. An access_log inside a location replaces the server-level one for requests handled by that location. Those requests are then no longer written to the server's file, unless you repeat it.
server {
server_name example.com;
access_log /var/log/nginx/example.com.access.log;
location /api/ {
access_log /var/log/nginx/example.com.api.log;
proxy_pass http://127.0.0.1:3000;
}
}Requests to /api/ go only to example.com.api.log. If you want them in both, list both files in the location. Multiple access_log lines at one level are allowed.
The default server needs logs too
Requests with an unknown host name go to the default server. The catch-all block from the plan returns 444 and writes nothing special unless you tell it to:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
access_log /var/log/nginx/default.access.log;
return 444;
}Reading default.access.log shows scanners, misconfigured DNS records and clients that connect by IP address. If a legitimate site appears there, its server_name probably has a typo. Note that status 444 appears in the log as 444 with a response size of 0, even though the client received nothing.
Log rotation
Log files grow without limit unless rotated. Debian and Ubuntu install /etc/logrotate.d/nginx, which covers /var/log/nginx/*.log. It rotates the files on a schedule and signals Nginx to reopen them. The signal matters. After a file is renamed, Nginx keeps writing to the old, renamed file until it reopens its logs. You can trigger this by hand:
sudo nginx -s reopenIf you keep logs outside /var/log/nginx/, add that path to a logrotate rule yourself. Otherwise the disk fills slowly and the cause is hard to spot.
The diagram shows how the contexts decide where one request is written.
Troubleshooting log problems
- Empty file after reload. Check that the request reached the right block. Send one with
curl --resolveand confirm it is not landing in the default server's log. nginx -treportsopen() failed (13: Permission denied). The directory is not accessible to the master process, or the file exists and is owned by another user. Check ownership withls -l.- Lines arrive late. A
buffer=orflush=parameter is set. - Rotated file keeps growing. Nginx did not reopen its logs. Run
sudo nginx -s reopenand check the logrotate postrotate script. - Requests missing from the server log. A
locationhas its ownaccess_logthat replaces the server one.
Exercise
A server block for example.org sets access_log /var/log/nginx/example.org.access.log; at server level. It also has location /downloads/ { access_log /var/log/nginx/downloads.log; }. A client requests /downloads/file.zip. Which file receives the access log line, and what would you add to also write it to the site's main log?
Show answer
Only /var/log/nginx/downloads.log receives the line, because the location-level access_log replaces the server-level one. To write to both, add a second line inside the location: access_log /var/log/nginx/example.org.access.log;.
Exercise
You reload Nginx after adding access_log /var/log/nginx/shop.access.log vhost buffer=64k flush=10s;, send one request, and the file stays empty. Give two possible causes.
Show answer
The line is buffered and will appear after up to 10 seconds, or once 64 KB accumulates. Alternatively, the vhost format is not defined, which would make nginx -t fail, or the request was served by a different server block, such as the default server, and is in that block's log.