Server Administration
Server Logs Guide: journalctl, Nginx Logs and logrotate
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيWhen something breaks on a server, logs are the first witnesses you should question. They tell you which service failed, when it started, which request triggered an error, and whether someone has been knocking on your SSH port all night. Yet many administrators only open logs in a panic and do not know where to look. This guide gives you a practical map of Linux logs, the commands to filter them quickly, and the configuration that keeps them from filling your disk.
Where Linux logs live
Modern distributions use two systems side by side: the systemd journal, a structured binary log managed by journald, and classic text files under /var/log/, written by services directly or by rsyslog.
| Log | Debian / Ubuntu | RHEL family (AlmaLinux, Rocky) |
|---|---|---|
| General system messages | /var/log/syslog | /var/log/messages |
| Authentication, SSH, sudo | /var/log/auth.log | /var/log/secure |
| Kernel | /var/log/kern.log or journalctl -k | journalctl -k |
| Package management | /var/log/apt/history.log | /var/log/dnf.log |
| Nginx | /var/log/nginx/access.log, error.log | same paths |
| Apache | /var/log/apache2/ | /var/log/httpd/ |
| MySQL / MariaDB | /var/log/mysql/ | /var/log/mariadb/ |
Paths can vary with configuration, so check the service's config file if a log is not where you expect. Reading most of these requires root or membership in the adm group (Debian/Ubuntu) or systemd-journal group for the journal.
Reading the journal with journalctl
journalctl is powerful because it lets you filter by service, time and priority without grepping through huge files.
journalctl -u nginx # all logs for one service
journalctl -u nginx -f # follow live, like tail -f
journalctl -u php8.3-fpm --since "1 hour ago"
journalctl --since "2026-11-22 09:00" --until "2026-11-22 10:00"
journalctl -p err -b # errors and worse since the last boot
journalctl -b -1 # previous boot (needs persistent journal)
journalctl -k # kernel messages, e.g. OOM killer
journalctl -u ssh -n 50 --no-pager # last 50 lines; the unit is sshd on RHELPriority levels range from emerg (0) to debug (7); -p err shows err, crit, alert and emerg.
Make the journal persistent and bounded
On some systems the journal is stored only in memory and lost on reboot. To keep it, make sure /var/log/journal exists and set limits in /etc/systemd/journald.conf:
[Journal]
Storage=persistent
SystemMaxUse=500M
MaxRetentionSec=1monthApply with systemctl restart systemd-journald. Check usage with journalctl --disk-usage, and trim manually with journalctl --vacuum-size=300M.
Nginx access and error logs
The access log records every request: client IP, time, request line, status code, bytes sent, referrer and user agent. The error log records problems Nginx itself saw: upstream timeouts, permission errors, missing files and configuration issues.
Useful one-liners for the default combined log format:
# follow errors live
tail -f /var/log/nginx/error.log
# count status codes
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
# top 10 client IPs
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
# most requested URLs returning 404
awk '$9 == 404 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
# search older compressed logs too
zgrep " 502 " /var/log/nginx/access.log*A burst of 502 Bad Gateway usually means the backend (PHP-FPM, Node, Gunicorn) is down or overloaded; the matching line in error.log will mention connect() failed or upstream timed out. To measure slow requests, add $request_time and $upstream_response_time to a custom log_format.
On very busy sites, consider access_log ... buffer=32k flush=5s; to reduce disk writes, or disable access logs for static assets you do not need to analyze.
Keeping disks healthy with logrotate
Logs grow forever unless something rotates them. logrotate runs daily (via cron or a systemd timer) and renames, compresses and deletes old logs according to rules in /etc/logrotate.conf and /etc/logrotate.d/. Nginx packages ship a rule already; here is a typical one for your own application:
/var/www/app/storage/logs/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 www-data www-data
}Test without changing anything using logrotate -d /etc/logrotate.d/app, and force a run with logrotate -f. If an application keeps writing to a deleted file, use a postrotate script to reload it, or copytruncate as a last resort.
When disk space suddenly drops, run du -sh /var/log/* | sort -h to find the culprit. Logs are often the reason a VPS disk fills up, so regular rotation is not optional.
A troubleshooting routine
- Check service state:
systemctl status nginxshows the last log lines too. - Read recent errors:
journalctl -u <service> --since "15 min ago". - Check the application and web server error logs at the same timestamp.
- Look at the kernel log for out-of-memory kills or disk errors.
- Correlate with your alerts; our uptime monitoring guide helps you know exactly when an incident began.
For scheduled jobs, remember to send their output to log files as shown in our cron jobs guide.
FAQ
Why is my journal empty after a reboot?
The journal is probably volatile, stored under /run/log/journal in memory. Create /var/log/journal, set Storage=persistent in journald.conf, and restart systemd-journald. From then on, journalctl -b -1 will show logs from the previous boot.
Is it safe to delete files in /var/log?
Deleting rotated, compressed files such as *.gz is generally safe. Avoid deleting active log files that a process still has open, because the space is not freed until the process restarts. Truncate them instead with truncate -s 0 file.log, and fix rotation so it does not happen again.
How do I find failed SSH login attempts?
On Debian and Ubuntu, search /var/log/auth.log for "Failed password" or "Invalid user". On the RHEL family use /var/log/secure, or journalctl -u sshd. Many attempts from the same addresses suggest enabling key-only login and a tool such as fail2ban.