通过 systemd 单元启动时 nginx 超时

通过 systemd 单元启动时 nginx 超时

我的 nginx 通过/usr/sbin/nginx -c /etc/nginx/nginx.conf命令行启动得很好,但是当我尝试使用以下命令启动它时:

# systemctl start nginx

然后它会挂起直到超时。

我的 systemd 单元如下所示:

[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /var/run/nginx/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

这是启动 nginx 的尝试:

root@efk:~# systemctl start nginx                                                                                                  
Job for nginx.service failed because a timeout was exceeded.                                                                       
See "systemctl status nginx.service" and "journalctl -xe" for details.                                                             
root@efk:~# systemctl status nginx.service                                                                                         
? nginx.service - A high performance web server and a reverse proxy server                                                         
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)                                             
   Active: failed (Result: timeout) since Mon 2021-10-04 00:32:44 CEST; 1min 25s ago                                               
     Docs: man:nginx(8)                                                                                                            
  Process: 14413 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)                        
  Process: 14403 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)               
 Main PID: 466 (code=exited, status=0/SUCCESS)                                                                                     
                                                                                                                                   
Oct 04 00:31:14 efk systemd[1]: Starting A high performance web server and a reverse proxy server...                               
Oct 04 00:31:14 efk systemd[1]: nginx.service: Can't open PID file /var/run/nginx/nginx.pid (yet?) after start: No such file or dir
Oct 04 00:32:44 efk systemd[1]: nginx.service: Start operation timed out. Terminating.                                             
Oct 04 00:32:44 efk systemd[1]: nginx.service: Killing process 14414 (nginx) with signal SIGKILL.                                  
Oct 04 00:32:44 efk systemd[1]: nginx.service: Killing process 14416 (nginx) with signal SIGKILL.                                  
Oct 04 00:32:44 efk systemd[1]: nginx.service: Killing process 14417 (nginx) with signal SIGKILL.                                  
Oct 04 00:32:44 efk systemd[1]: nginx.service: Failed with result 'timeout'.                                                       
Oct 04 00:32:44 efk systemd[1]: Failed to start A high performance web server and a reverse proxy server.                          
root@efk:~# journalctl -xe                                                                                                         
Oct 04 00:32:44 efk systemd[1]: nginx.service: Start operation timed out. Terminating.
Oct 04 00:32:44 efk systemd[1]: nginx.service: Killing process 14414 (nginx) with signal SIGKILL.
Oct 04 00:32:44 efk systemd[1]: nginx.service: Killing process 14416 (nginx) with signal SIGKILL.
Oct 04 00:32:44 efk systemd[1]: nginx.service: Killing process 14417 (nginx) with signal SIGKILL.
Oct 04 00:32:44 efk systemd[1]: nginx.service: Failed with result 'timeout'.
Oct 04 00:32:44 efk systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit nginx.service has failed.
-- 
-- The result is RESULT.

但是当它开始时:

# /usr/sbin/nginx -c /etc/nginx/nginx.conf

效果很好。

nginx 是:

root@efk:~# /usr/sbin/nginx -V
nginx version: nginx/1.21.3 (nginx-quic)
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
built with OpenSSL 1.1.1 (compatible; BoringSSL) (running with BoringSSL)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/run/lock/subsys/nginx --build=nginx-quic --with-debug --add-module=../njs/nginx --user=www-data --group=www-data --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_quic_module --with-stream_quic_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_slice_module --with-http_stub_status_module --without-select_module --without-poll_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-openssl=/home/mtodorov/build/boringssl --with-cc-opt='-g -O2 -fPIE -fstack-protector-all -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -I /home/mtodorov/build/boringssl/.openssl/include/' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -L /home/mtodorov/build/boringssl/.openssl/lib/'
root@efk:~# 

所以路径 /var/run/nginx/nginx.pid 是编译进来的,所以我不知道为什么服务器在从 systemd 单元运行时找不到它?

任何想法?

马文

答案1

问题很愚蠢。尽管编译了配置,nginx 使用的是/var/run/nginx.pid,而不是/var/run/nginx/nginx.pid(与 apache2 不同)。

在 nginx.service 单元中更改这一点可以解决问题,现在该systemctl start nginx指令可以无缝运行。

马文

答案2

我在 ChromeOS 上启动 nginx 时遇到了类似的问题。不过,修复方法几乎相同,只是/etc/init.d/nginx我更改PID=/run/nginx.pidPID=/run/nginx/nginx.pid.

systemctl start nginx改变后工作。我不完全确定为什么 apt 将它放在 init.d 中而不是添加 systemd 服务,但它确实有效。

相关内容