You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Traefik启用日志轮转?日志轮转是否为内置功能?

How to Set Up Log Rotation for Traefik (Including access.log)

Great question! Let's start with the key point straight from the docs: Traefik does NOT have built-in log rotation functionality. As the official notes state, it expects external tools like logrotate (a standard utility on most Linux systems) to handle log rotation and management.

Now, let's walk through exactly how to set this up for both Traefik's core logs and your access.log:


1. Using logrotate (For Traefik Running Directly on Host)

Step 1: Verify logrotate is installed

First, check if logrotate is already on your system:

logrotate --version

If it's missing, install it via your package manager:

  • Debian/Ubuntu: sudo apt install logrotate
  • RHEL/CentOS/Fedora: sudo dnf install logrotate

Step 2: Create a Traefik Log Rotation Config File

Create a dedicated config file for Traefik in the logrotate.d directory (this is where system-wide log rotation rules live):

sudo nano /etc/logrotate.d/traefik

Paste in the following configuration (adjust paths and settings to match your Traefik setup):

/var/log/traefik/*.log {
    daily          # Rotate logs every day
    missingok      # Don't throw an error if a log file is missing
    rotate 7       # Keep 7 days of rotated logs
    compress       # Compress old logs with gzip
    delaycompress  # Delay compression until the next rotation cycle
    notifempty     # Skip rotation if the log file is empty
    create 0640 root adm  # Create new log files with correct permissions
    postrotate
        # Send USR1 signal to Traefik to reopen log files (critical!)
        kill -USR1 $(cat /var/run/traefik.pid)
    endscript
}

Let's break down the important bits:

  • The postrotate block is essential: Traefik holds open file handles to its logs, so sending the USR1 signal tells it to close and reopen the log files. This ensures it writes to the new, rotated file instead of the old one that logrotate has renamed.
  • Adjust the path /var/log/traefik/*.log to match where your Traefik logs (including access.log) are stored.

Step 3: Test the Configuration

Run a dry run to make sure your config works without making changes:

sudo logrotate -d /etc/logrotate.d/traefik

If no errors appear, run the rotation manually once to verify:

sudo logrotate -f /etc/logrotate.d/traefik

2. Log Rotation for Traefik Running in Docker

If you're running Traefik in a container, you have two main options:

Option A: Mount Logs to Host and Use logrotate

If you've mounted Traefik's log directory from the host into the container (e.g., -v /var/log/traefik:/var/log/traefik in your docker run command), you can use the exact same logrotate setup as above. Just ensure the host path in the config matches your mount point.

Option B: Use Docker's Built-In Log Rotation

Docker has native support for log rotation when using the json-file log driver (the default). To enable it:

  1. Edit or create the Docker daemon config file:
sudo nano /etc/docker/daemon.json
  1. Add the following configuration (adjust max-size and max-file to your needs):
{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "10m",
        "max-file": "5"
    }
}
  • max-size: Maximum size of a single log file before rotation (e.g., 10m for 10MB)
  • max-file: Number of rotated log files to keep
  1. Restart Docker to apply the changes:
sudo systemctl restart docker

This will handle rotation for all Docker containers (including Traefik) using the json-file driver.


Key Recap

  • Traefik does not include built-in log rotation—you must use external tools like logrotate or Docker's native logging features.
  • For host-based Traefik deployments, logrotate is the most flexible solution, and the USR1 signal is critical to ensure Traefik switches to new log files after rotation.
  • For Docker deployments, either mount logs to the host and use logrotate, or leverage Docker's built-in rotation for simpler setup.

内容的提问来源于stack exchange,提问作者Robert Lachner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:52:15