How to configure log rotate for Gunicorn running as a systemd service within a venv behind an Nginx reverse proxy?

599 Views Asked by At

I can't figure out how to cleanly configure log rotation for my python app (served by Gunicorn 20.0.4 behind an Nginx reverse proxy). Official Gunicorn documentation states:

Send the USR1 signal to rotate logs if you are using the logrotate utility: kill -USR1 $(cat /var/run/gunicorn.pid)

I'm not sure what pid file I should target and where above logrotate directive should reside (postrotate or other).

SystemD service

# /etc/systemd/system/my_app.service
[Unit]
Description=My App
After=network.target
Wants=celery-my_app.service
Wants=celerybeat-my_app.service

[Service]
User=my_app_user
Group=nginx
Environment=FOO=bar
RuntimeDirectory=my_app
WorkingDirectory=/opt/my_app/my_project
ExecStart=/opt/my_app/my_project/venv/bin/gunicorn --workers 3 --log-level debug --error-logfile /var/log/my_app/my_app_error.log --access-logfile /var/log/my_app/my_app_access.log --capture-output --bind unix:/var/run/my_app/my_app.sock my_project.wsgi:application

[Install]
WantedBy=multi-user.target 

Logrotate configuration

/var/log/my_app/my_app_error.log /var/log/my_app/my_app_access.log {
    weekly
    missingok
    rotate 4
    delaycompress
    notifempty
    copytruncate
    postrotate
        # Gunicorn alleged pid file
        kill -USR1 $(cat /var/run/my_app.pid)
    endscript
}
  • Do I have to assume that Gunicorn pid file used for kill signal mentionned in documentation is /var/run/my_app.pid?
  • Are there additionnal directives to add to the logrotate configuration file below especially regarding my use case (venv, Gunicorn, Nginx, systemd) or caveats I shiuld be aware of to properly configure log rotation?
0

There are 0 best solutions below