Nginx logs can grow quickly on a busy website, fill a server’s disk, or retain information you no longer need. You can clear them safely, but the right method depends on how Nginx is configured and whether it is writing to files, a container log system, or a centralized logging service. Before removing anything, confirm which logs you want to clear and consider whether you need to keep a copy for troubleshooting, security, or compliance.
This guide explains how to find Nginx log files, empty them without disrupting logging, handle deleted-but-still-open files, and prevent the same problem from returning. The commands assume a Linux server and may need adjustment for your distribution or hosting setup.
What Nginx Logs Can You Clear?
Nginx commonly writes two types of logs. The access log records requests handled by the server, such as requested URLs, response codes, and client information. The error log records warnings and errors, including problems with upstream servers, permissions, and configuration. A typical installation stores them at:
/var/log/nginx/access.log
/var/log/nginx/error.log
Those paths are common, not guaranteed. A server may use different filenames, separate logs for individual websites, or send logs to a container runtime or external service. Clearing the wrong file may have no effect—or could remove logs used by another application.
Find the Active Nginx Log Paths
Start by checking the Nginx configuration. The access_log and error_log directives specify where logs are written. Search the main configuration and included files with:
sudo nginx -T 2>/dev/null | grep -E 'access_log|error_log'
The -T option tests and prints the full configuration, including files pulled in through include directives. The command above filters that output to show log directives. You can also inspect common configuration locations, such as /etc/nginx/nginx.conf and files under /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/.
If you see a relative log path, it is interpreted in relation to Nginx’s prefix directory. If logging is set to off, or the directive points to syslog:, the logs may not be ordinary files. Check your hosting platform’s documentation before trying to clear them.
Safest Quick Method: Empty the Log Files
To clear a regular Nginx log file while keeping the file in place, truncate it to zero bytes. For example:
sudo truncate -s 0 /var/log/nginx/access.log
sudo truncate -s 0 /var/log/nginx/error.log
Replace those paths with the active paths you found in your configuration. Truncating preserves the file itself, including its ownership and permissions, while removing its current contents. Nginx can continue writing new entries to the same file, so you generally do not need to stop the service or restart it.
You can check the result with:
sudo ls -lh /var/log/nginx/
sudo tail -n 20 /var/log/nginx/access.log
The first command shows file sizes. The second displays recent access-log entries; after you clear the log, new requests will begin appearing as they arrive.
To clear all matching Nginx log files, first review the exact files that would be affected. Avoid using broad commands such as sudo rm -f /var/log/nginx/*. That may delete rotated logs, files belonging to other services, or files you intended to keep. Truncate only the specific active logs you have identified.
Should You Delete the Log File Instead?
Usually, no. Removing an active log file with rm does not necessarily make Nginx stop writing to it. A running process can keep the deleted file open, and the disk space may remain in use until that process closes its file handle. Meanwhile, new log entries may continue going to the invisible, deleted file instead of a newly created file.
If you have already deleted an active log and the disk space did not return, check for deleted files still held open by a process:
sudo lsof +L1
Look for Nginx processes and deleted log files in the output. To make Nginx reopen its log files, use the reopen command described below. For future cleanup, truncating the active file or using log rotation is generally safer than deleting it.
Reopen Nginx Logs When Needed
After log rotation—or if an active log was deleted—Nginx may need a signal telling it to reopen its log files. On a typical installation, run:
sudo nginx -s reopen
This asks Nginx to reopen its logs without stopping the service. It is different from a full restart. If the command cannot find the Nginx PID file, check your installation’s PID-file configuration or use the appropriate service-manager command. On many systemd-based systems, this is another option:
sudo systemctl kill -s USR1 nginx
The USR1 signal tells the Nginx master process to reopen log files. Service names and permissions can differ, so confirm the correct service name on your server. Do not send signals to a process ID you have not verified.
For a simple truncation of an existing log file, reopening is usually unnecessary. The file remains the same file, and Nginx can keep writing to it. Reopening matters most when files have been moved, replaced, or deleted.
Use Logrotate for Automatic Nginx Log Cleanup
Manually clearing logs is a short-term fix. For ongoing management, use logrotate, a standard Linux utility that rotates old logs, creates fresh files, and optionally compresses or deletes older copies. Many Nginx installations already include a logrotate configuration, commonly at:
/etc/logrotate.d/nginx
Inspect that file before creating or changing a rotation rule. A typical configuration may resemble this:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -s /run/nginx.pid ] && kill -USR1 "$(cat /run/nginx.pid)"
endscript
}
This example rotates logs daily, keeps a limited number of older copies, compresses eligible files, and asks Nginx to reopen its logs after rotation. It is only an example: the correct owner, group, permissions, PID-file path, retention period, and schedule depend on your system. Do not paste it over an existing configuration without checking how your server is set up.
You can test a logrotate configuration without making changes by using debug mode:
sudo logrotate -d /etc/logrotate.d/nginx
Forcing rotation with logrotate -f can be useful in a controlled test, but it changes files immediately. Review the configuration and make sure you have enough disk space before using it. If you only want to remove the current contents right now, truncating the active log is more direct.
Clearing Nginx Logs in Docker or Kubernetes
Nginx running in a container may send access and error logs to standard output and standard error instead of writing to files. In that case, commands such as truncate /var/log/nginx/access.log may not apply. Container logs are usually collected and managed by Docker, Kubernetes, or a logging driver.
For Docker, inspect how the container is configured and check the runtime’s log-management settings. Avoid manually deleting files under Docker’s internal storage directories while the daemon is running; doing so can cause inconsistent logging or interfere with the container runtime. Configure log rotation through the Docker daemon or the relevant container settings instead.
In Kubernetes, view logs with kubectl logs, while retention and collection are generally controlled by the cluster’s node-level logging system or an external logging platform. Follow your platform’s procedure for retention or deletion rather than trying to edit a container’s log file directly.
Before You Clear Nginx Logs
-
Confirm the paths. Use the active Nginx configuration rather than assuming every server uses
/var/log/nginx/. - Check the contents first. Recent errors may help diagnose an outage, failed deployment, or suspicious activity.
- Save a copy if required. Back up logs that may be needed for an investigation, audit, or retention policy.
-
Use the right permissions. Administrative access is commonly required, which is why the examples use
sudo. - Check available disk space. If the disk is full, clearing a large log can help, but also investigate why the file grew unexpectedly.
- Protect sensitive data. Access logs can contain IP addresses, URLs, and other information that may be sensitive under your organization’s policies.
Common Problems After Clearing Logs
If the file appears empty but grows again immediately, that is usually normal: Nginx is recording new requests. If the file stays empty despite traffic, verify that the path is active, logging is enabled, and the Nginx worker processes have permission to write to it. Check the error log and validate configuration changes with sudo nginx -t.
If disk usage does not drop after deleting a log, the file may still be open by a running process. Use sudo lsof +L1 to look for deleted open files, then have the relevant service reopen its logs. If the space still appears unavailable, check whether another log, database, container, or temporary file is consuming it.
If you are unsure whether a log is safe to remove, do not delete it yet. Identify which process owns it, where it is configured, and whether a retention requirement applies. A little investigation is safer than removing evidence needed to fix a production problem.
Conclusion
The simplest way to clear a standard Nginx log is to confirm its configured path and truncate the active file with sudo truncate -s 0. This removes existing entries while preserving the file Nginx is using. Avoid deleting active logs unless you understand how the service will reopen them, and use logrotate or your container platform’s logging controls to manage growth over time. Always check your server’s configuration and retention requirements before clearing Nginx logs.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.