Ubuntu EC2中Nginx无法解析/run/nginx.pid文件PID的问题
解决Nginx PID解析错误问题
问题现象
- 执行
systemctl status nginx时触发报错:nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument - 执行
ps aux -P | grep nginx得到进程信息:root 30696 0.0 0.1 141260 1632 ? Ss 16:00 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 33701 0.0 0.7 1234344 7116 ? S 16:00 0:00 nginx: worker process ubuntu 31115 0.0 0.1 12344 1128 pts/0 S+ 16:00 0:00 grep --color=auto ngin
Nginx配置文件内容
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings server { listen 443; ssl on; ssl_certificate /home/ubuntu/ssl/certificati/nginx_bundle_af1458631462.crt; ssl_certificate_key /home/ubuntu/ssl/my.domain.key; root /var/www/path; server_name my.domain.com; } ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
解决方案
1. 检查PID文件状态
先查看/run/nginx.pid的内容和权限:
cat /run/nginx.pid ls -l /run/nginx.pid
如果文件为空、包含非数字字符,或者权限不属于root用户,都会导致systemd解析失败。
2. 清理异常进程与PID文件
手动停止所有Nginx进程:
sudo kill -TERM 30696 sudo kill -TERM 33701
删除损坏的PID文件:
sudo rm /run/nginx.pid
3. 重新启动Nginx
让systemd重新启动服务,生成正确的PID文件:
sudo systemctl restart nginx
4. 验证修复结果
执行systemctl status nginx确认报错消失;再用ps aux -P | grep nginx检查进程状态,同时查看/run/nginx.pid确认内容为master进程的数字PID。
额外排查点
- 核对Nginx配置中
pid指令的路径,是否与/lib/systemd/system/nginx.service里的PIDFile参数一致。 - 确认
/run目录权限正常,Nginx进程具备写入该目录的权限。
内容的提问来源于stack exchange,提问作者ds_1994
相关产品推荐
相关产品推荐

