DocsMonitor

Nginx logs

The agent reads the logs nginx already writes and turns failed requests and upstream errors into something you can search and alert on. Nginx keeps serving traffic exactly as before.

4 min readUses the server agentPro

1Add a log format with response times

nginx's default combined format has no timing, so add this format inside the http {} block:

nginx.conf
log_format watchdock '$remote_addr [$time_local] "$request" $status '
'$body_bytes_sent $request_time $upstream_response_time '
'$request_id';

Then point each site you want watched at it, ideally with its own files:

sites-enabled/api
server {
server_name api.example.com;
access_log /var/log/nginx/api_access.log watchdock;
error_log /var/log/nginx/api_error.log warn;
}

Check the config and reload:

bash
sudo nginx -t && sudo systemctl reload nginx

2Add the source in Watchdock

Open Logs, open the source menu and choose Add nginx source. Pick the server, name the service, and enter the access and error log paths you set above. It's collected from the agent's next report.

If several sites share one log file, give each source a path prefix like /api/ so every request is counted once. Two sources reading the same file without a prefix count everything twice.

Forward nginx's request ID to your app and the Python SDK attaches it to every error it captures. From a failed request in the logs you can then jump straight to the exception it caused.

sites-enabled/api
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Request-Id $request_id;
}

$request_id is built into nginx 1.11 and later. Skip this step and collection still works, you just lose the link.

Nginx in Docker

The agent reads log files on the host, the way tail -f does. If nginx runs in a container, its logs only exist inside that container until you get them onto the host. Installed on the server directly? Skip this section.

Mount the log directory onto the host

services:
nginx:
image: nginx:latest
volumes:
- /var/log/nginx-app:/var/log/nginx
- ./nginx.conf:/etc/nginx/nginx.conf:ro

Use a dedicated host path like /var/log/nginx-app so it can't collide with an nginx installed on the host. Keep the mount in your compose file or run command, or it disappears the next time the container is recreated.

Write to your own file names

The official nginx image links access.log and error.log to the container's stdout and stderr, so the mount alone gives you links on the host, not log lines. Use your own file names, as in step 1, and the container writes real files.

Enter the host path

When you add the source, use the path as the host sees it: /var/log/nginx-app/api_access.log, not the path inside the container.

Container log capture, which reads docker logs, picks up a containerised nginx without any of this, but only as raw lines. Response times, status codes and per-endpoint failures need the file-based setup.

What gets stored

Watchdock keeps failed requests only: every 4xx and 5xx, with its endpoint, status, response time and upstream time. Successful and redirected requests are read and thrown away, along with common scanner and bot probes. A spike of 500s is worth paging someone over; a count of 200s isn't, and storing every request doesn't scale.

That shapes what the numbers mean:

You seeIt counts
Request volumeFailed requests per minute, not total traffic
Status codes4xx and 5xx only
Response and upstream timeFailed requests only
Top endpointsRanked by failures, not popularity

From the error log you get a timeline of nginx's own failures. A few worth knowing on sight:

MessageMeans
connect() failed (111: Connection refused)Your app isn't listening
no live upstreamsEvery backend worker is down
upstream timed outYour app took longer than proxy_read_timeout
too many open filesThe server hit its file descriptor limit
ProNginx log collection needs the Pro plan. History is kept 30 days on Pro and 90 on Team.

Troubleshooting

Nothing is collected

Check the files exist with ls -la /var/log/nginx/, that the source's paths match them exactly, and that the source is switched to Collecting.

Response time shows as -

nginx is still writing the default format. Apply the watchdock format to that site and reload.

Counts look doubled

Two sources read the same file without a path prefix. Give each one a prefix.

The error log is empty but nginx is clearly failing

Set error_log to warn or lower. crit and emerg drop most errors before they're written.

In Docker, the host file is empty or a link

nginx is still writing the image's default access.log and error.log. Run docker exec <container> nginx -T to see the config nginx actually loaded, and check your access_log and error_log lines use your own file names.

In Docker, collection stopped after the container was recreated

The mount wasn't part of the container's definition. Add it to your compose file or run command.