Nginx 重啟發生 nginx.service: Failed to read PID from file nginx.pid: Invalid argument 解決辦法

Nginx 在某些情況下, 重啟發生如以下的錯誤訊息, 但是 Nginx 實際上是正常運作的
Aug 13 10:02:02 webserver systemd[1]: Stopped The NGINX HTTP and reverse proxy server.
Aug 13 10:02:02 webserver systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Aug 13 10:02:02 webserver nginx[28250]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Aug 13 10:02:02 webserver nginx[28250]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Aug 13 10:02:02 webserver systemd[1]: nginx.service: Failed to read PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument
Aug 13 10:02:02 webserver systemd[1]: Started The NGINX HTTP and reverse proxy server.


這個錯誤訊息很令人在意, 雖然 Nginx 有正確服務
nginx.service: Failed to read PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument

(nginx.pid 的路徑可能隨著你編譯安裝時的設定而不同)

在 Google 上搜尋了這項錯誤訊息的解決方法, 通常是在 nginx.service 檔裡加上啟動 Nginx 後的延遲, 例如:
ExecStartPost=/bin/sleep 0.1


原來是 Nginx 啟動尚未完成, systemd 就去讀取 nginx.pid, 於是造成錯誤訊息出現, 那麼來修改 nginx.service
nano /lib/systemd/system/nginx.service

ExecStartPost=/bin/sleep 0.1貼入 nginx.service 的"[Service]"區塊內
例如:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecStartPost=/bin/sleep 0.1
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target


編輯過 nginx.service 檔案需要重新載入
systemctl daemon-reload


嘗試重啟 Nginx
service nginx restart


可以看到 Nginx 不再吐錯誤訊息了
Aug 13 10:02:58 webserver systemd[1]: Starting The NGINX HTTP and reverse proxy server...
Aug 13 10:02:58 webserver nginx[28321]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Aug 13 10:02:58 webserver nginx[28321]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Aug 13 10:02:59 webserver systemd[1]: Started The NGINX HTTP and reverse proxy server.
2020-08-13 10:24 發佈
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?