Автоматический перезапуск nginx при изменении файла с сертификатом

В обычной ситуации перезапуск nginx должна делать та программа, которая обновила сертификат (обычно это certbot или dehydrated). Но есть и другой вариант – сделать триггер для systemd, срабатывающий при завершении записи в файл.

Файл /etc/systemd/system/nginx-reload.path:

[Unit]
Description=Reload nginx if certificate has been changed
Requires=nginx.service
After=nginx.service
 
[Path]
PathChanged=/etc/pve/local/pveproxy-ssl.pem
 
[Install]
WantedBy=multi-user.target

Файл /etc/systemd/system/nginx-reload.service:

[Unit]
Description=Reload nginx
 
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/nginx -s reload
 
[Install]
WantedBy=multi-user.target

Добавляем в автозагрузку и запускаем:

 systemctl daemon-reload
 systemctl enable nginx-reload.path
 systemctl start nginx-reload.path

Проверяем:

# touch /etc/pve/local/pveproxy-ssl.pem
# journalctl -u nginx-reload
 
Jul 22 18:34:48 myserver systemd[1]: Starting nginx-reload.service - Reload nginx...
Jul 22 18:34:48 myserver nginx[5338]: 2023/07/22 18:34:48 [notice] 5338#5338: signal process started
Jul 22 18:34:48 myserver systemd[1]: Finished nginx-reload.service - Reload nginx.