无法重新启动nginx,bind()失败(98:地址已在使用中)


9

我知道此主题以各种不同的形式存在,但是我无法解决自己的问题。如果我运行service nginx restart,nginx失败,并显示以下日志:

2015/03/14 08:36:42 [emerg] 9400#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/03/14 08:36:42 [emerg] 9400#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/03/14 08:36:42 [emerg] 9400#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/03/14 08:36:42 [emerg] 9400#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/03/14 08:36:42 [emerg] 9400#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/03/14 08:36:42 [emerg] 9400#0: still could not bind()

我只有一个配置正在加载sites-enabled。当我跑步时:

$ grep -slir "listen 80"
$ sites-available/default

它表明default配置具有监听80,但这并不重要,因为它不在in中sites-enabled

$ ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)'
  PID  PPID %CPU    VSZ WCHAN  COMMAND
 9468     1  0.0  97188 sigsus nginx: master process /usr/sbin/nginx
 9471  9468  0.0  97328 ep_pol nginx: worker process
 9472  9468  0.0  97368 ep_pol nginx: worker process
 9693  9641  0.0   9448 pipe_w egrep --color=auto (nginx|PID)

netstat显示了正在使用的端口:

$ netstat -tulpn

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      1300/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      9468/nginx
tcp        0      0 0.0.0.0:2812            0.0.0.0:*               LISTEN      5980/monit
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1174/mysqld
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      1155/memcached
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9468/nginx
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1113/sshd
tcp6       0      0 :::25                   :::*                    LISTEN      1300/master
tcp6       0      0 :::9000                 :::*                    LISTEN      9106/hhvm
tcp6       0      0 :::22                   :::*                    LISTEN      1113/sshd
udp        0      0 127.0.0.1:11211         0.0.0.0:*                           1155/memcached

这是我完整的配置sites-enabled(其中只有一个):

server {
    listen                  443 ssl default_server;

    limit_conn              gulag 15;
    server_name             www.my-website.com;

    access_log              /home/my-website/logs/access.log;
    error_log               /home/my-website/logs/error.log info;

    root                    /home/my-website/web;
    index                   index.php index.html;

    ssl_certificate /etc/nginx/ssl/my-website.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/my-website.com.key;
    ssl_prefer_server_ciphers On;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;

    include                 /etc/nginx/includes/my-website_redirects_https.inc;
    include                 /etc/nginx/includes/file_cache.inc;
    include                 /etc/nginx/includes/wordpress.inc;
}

server {
    listen                  80;
    server_name             my-website.com www.my-website.com;
    return                  301 https://$server_name$request_uri;
}

server {
    listen                  443;
    server_name             my-website.com;
    return                  301 https://www.$server_name$request_uri;
}

server {
    server_name
        some-other-website1.com         www.some-other-website1.com
        some-other-website2.com         www.some-other-website2.com
        some-other-website3.com         www.some-other-website3.com
        some-other-website4.com         www.some-other-website4.com
        some-other-website5.com         www.some-other-website5.com

    rewrite ^(.*)$ https://www.my-website.com/some/page/ permanent;
}

server {
    server_name
        some-other-website6.com         www.some-other-website6.com
        some-other-website7.com         www.some-other-website7.com
        some-other-website8.com         www.some-other-website8.com

    rewrite ^(.*)$ https://www.my-website.com/some/other/page/ permanent;
}

server {
    server_name     subdomain.my-website.com;

    rewrite ^(.*)$ https://some.otherwebsite.com/ permanent;
}

server {
    server_name
        some-other-website9.com         www.some-other-website9.com
        some-other-website10.com         www.some-other-website10.com
        some-other-website11.com         www.some-other-website11.com

    rewrite ^(.*)$  https://www.my-website.com/ permanent;
}

我想知道是否由于我的配置而发生此bind()错误。有什么想法我可能做错了吗?我正在使用nginx / 1.4.6(Ubuntu)。任何帮助将不胜感激。


我目前正面临着同样的问题。曾经工作且没有更改的nginx配置现在无法成功重新加载,因为它说正在使用端口,但是nginx是在该端口上运行的唯一应用程序。您解决了问题吗?
米奇·肯特 Mitch Kent)

Answers:


13

尝试停止Nginx:

service nginx stop

检查nginx仍在运行:

ps ax | grep nginx

如果仍在运行,请终止该进程:

kill -p PID

检查nginx状态,将无法运行:

ps ax | grep nginx

然后重新启动:

service nginx start

在同样的问题之后,这对我有用。


对我来说,杀死PID之后,该过程将重新开始
Jason Liu

4

根据您的netstat输出,nginx本身仍在端口80上运行。

在尝试重新启动它之前,请使用验证您的配置nginx -t并修复错误。


4
我已经运行nginx -t,没有错误。这就是为什么我对无法重新启动感到困惑。
达伦2015年

尝试先停止nginx,然后再重新启动。
迈克尔·汉普顿

1
我遇到了同样的问题。我认为这是Nginx的错误。
ClojureMostly

2

我可能是唯一一个足以犯此错误的傻瓜,但是我不小心下载了错误的软件包(php而不是php-fpm),该软件包将Apache 2安装为依赖项。由于它在端口80上运行,因此Nginx无法。

可能不是一个很常见的错误,但是我想最主要的方法是检查端口80上是否有其他意外。


您不是第一个这样做的人。包裹名称选择不当;它确实应该被命名mod_php。但是,既然已经这么多年了,就必须在不破坏每个人的系统的情况下对其进行更改。
迈克尔·汉普顿

@MichaelHampton至少这是一个相当简单的解决方法(apt-get remove apache2 && apt-get autoremove
Redwolf Programs

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.