nginx.service:无法从文件 /run/nginx.pid 读取 PID:无效参数 ubuntu 16.04



我知道这是一个已知的错误,但我不知道如何解决这个问题,因为我做了很多谷歌搜索,然后最终回到堆栈以获得一些真正的解决方案:

以下是配置:

首先,我通过以下方式安装 nginx:

sudo apt-get install nginx
lsb_release -rd
Description:    Ubuntu 16.04.2 LTS
Release:        16.04
apt-cache policy nginx-core
nginx-core:
  Installed: 1.10.3-0ubuntu0.16.04.2
  Candidate: 1.10.3-0ubuntu0.16.04.2
  Version table:
 *** 1.10.3-0ubuntu0.16.04.2 500
        500 http://sg.mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 Packages
        500 http://sg.mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 Packages
        100 /var/lib/dpkg
/status
     1.9.15-0ubuntu1 500
        500 http://sg.mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 Packages

我的nginx服务配置是这样的:

nano /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/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 /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target

我的nginx.conf文件是这样的: nano/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
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
        ##
 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
        ##
        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_disable "msie6";
  ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##
        gzip on;
        gzip_disable "msie6";
        # 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 {
# 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;
当我启动nginx

服务器时,我的网页上出现502错误,nginx的状态是:

 sudo service nginx status
sudo: unable to resolve host iZt4ndf6hh48rtirr7qry9Z
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2017-10-15 16:34:10 CST; 4h 42min ago
  Process: 2682 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 2703 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 2700 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 2706 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2706 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─2707 nginx: worker process
Oct 15 16:34:10 iZt4ndf6hh48rtirr7qry9Z systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 15 16:34:10 iZt4ndf6hh48rtirr7qry9Z systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Oct 15 16:34:10 iZt4ndf6hh48rtirr7qry9Z systemd[1]: Started A high performance web server and a reverse proxy server.

现在我在谷歌上搜索并找到那个 pid 错误,但我试图解决该错误,但我无法这样做。

感谢您的阅读和您宝贵的时间。

这是由于 https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864 中讨论的争用条件而发生的。

如前所述,解决方法是在下面运行以创建覆盖文件

printf "[Service]nExecStartPost=/bin/sleep 0.1n" > /etc/systemd/system/nginx.service.d/override.conf
systemctl daemon-reload
systemctl start nginx

最新更新