nginx - nginx:[emerg] bind()到[::]:80失败(98:地址已经在使用中)

问题描述 投票:193回答:14

突然间,我得到了以下nginx错误

 * Restarting nginx
 * Stopping nginx nginx
   ...done.
 * Starting nginx nginx
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
   ...done.
   ...done.

如果我跑

lsof -i :80 or sudo fuser -k 80/tcp 

我一无所获。 80端口没有任何东西

然后我运行以下:

sudo netstat -pan | grep ":80"
tcp        0      0 127.0.0.1:8070          0.0.0.0:*               LISTEN      15056/uwsgi     
tcp        0      0 10.170.35.97:39567      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39564      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39584      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39566      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39571      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39580      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39562      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39582      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39586      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39575      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39579      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39560      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39587      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39591      10.158.58.13:8080       TIME_WAIT   -               
tcp        0      0 10.170.35.97:39589      10.158.58.13:8080       TIME_WAIT   - 

我很难过。

怎么调试?

我正在使用uwsgi

代理传递端口8070.uwsgi正在运行。 Nginx不是。我使用的是ubuntu 12.4

以下是我的nginx conf文件的相关部分

upstream uwsgi_frontend {
          server 127.0.0.1:8070;
        }
server {
listen 80;
        server_name 127.0.0.1;
        location = /favicon.ico {
                  log_not_found off;
                }



                location / {
                       include uwsgi_params;
                       uwsgi_buffering off;

                       uwsgi_pass 127.0.0.1:8070;
                 }
        }

这是我在ubuntu 12.04上安装nginx的方法

nginx=stable;add-apt-repository ppa:nginx/$nginx;
apt-get update
apt get install nginx-full
ubuntu nginx
14个回答
192
投票

[::]:80是一个ipv6地址。

如果您的nginx配置正在侦听端口80以及端口[::]:80,则可能导致此错误。

我在默认站点 - 可用文件中有以下内容:

listen 80;
listen [::]:80 default_server;

您可以通过将ipv6only=on添加到[::]:80来解决此问题,如下所示:

listen 80;
listen [::]:80 ipv6only=on default_server;

有关更多信息,请参阅:

http://forum.linode.com/viewtopic.php?t=8580

http://wiki.nginx.org/HttpCoreModule#listen


1
投票

我遇到了类似的问题。日志如下所示

2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: bind() to [::]:80 failed (98: Address already in use)
2018/10/31 12:54:20 [emerg] 128005#128005: still could not bind()
2018/10/31 12:54:23 [alert] 127997#127997: unlink() "/run/nginx.pid" failed (2: No such file or directory)
2018/10/31 22:40:48 [info] 36948#36948: Using 32768KiB of shared memory for push module in /etc/nginx/nginx.conf:68
2018/10/31 22:50:40 [emerg] 37638#37638: duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/default:18
2018/10/31 22:51:33 [info] 37787#37787: Using 32768KiB of shared memory for push module in /etc/nginx/nginx.conf:68

最后一个[emerg]显示duplicate listen options for [::]:80这意味着有多个包含[::]:80的nginx块文件。

我的解决方案是删除其中一个[::]:80设置

附:你可能有默认的块文件。我的建议是将此文件保留为端口80的默认服务器。并从其他块文件中删除[::]:80


1
投票

在我的情况下,Apache或Apache2或Nginx之一的服务已经运行,因此我无法启动其他服务。


0
投票

我有几个* .save文件(来自nano的紧急转储)来自我的网站中不同的NGINX配置文件 - 可用的目录。一旦我删除了这些.save文件,NGINX就重启了。我认为这些是无害的,因为没有相应的符号链接,但我想我错了。


0
投票

继续@ lfender6445和@SAURABH答案 -

我的问题还在于,在升级到Vagrant 2.2.2之后,当guest虚拟机启动时,Apache2作为Web服务器运行。在过去,我只有nginx作为Web服务器。

vagrant ssh进入框并运行以下命令以禁止Apache2在guest虚拟机框启动时启动:

sudo update-rc.d -f apache2 remove

退出ssh,流浪汉停止,流浪汉。问题解决了。


0
投票

如果在尝试上述任何解决方案后问题仍然存在,请重新启动服务器一次。它对我有用:)


0
投票

在我的案例中,罪魁祸首证明是一个包含以下内容的服务器块:

        listen  127.0.0.1:80;
        listen  [::1]:80 ipv6only=on;
        server_name  localhost;

在Linux上,侦听特定IP(例如[::1]:80)的套接字与侦听同一端口但任何IP(即[::]:80)的套接字冲突。通常,nginx将通过在这个场景后面使用单个套接字来透明地处理这个问题。但是,在listen指令上明确指定ipv6only(或某些其他选项)会强制nginx(尝试)为它创建一个单独的套接字,从而导致Address already in use错误。

由于ipv6only=on无论如何都是默认的(从1.3.4开始),修复只是从该指令中删除该选项,并确保ipv6only未在我的配置中的任何其他位置使用。


193
投票

我通过运行sudo apachectl stop来解决这个问题 - 结果是apache在后台运行并阻止nginx在所需的端口上启动。

在ubuntu上运行sudo /etc/init.d/apache2 stop


33
投票

我发现了以前从未有过的问题。

我只是要删除/etc/nginx/sites-available/default。然后它奏效了。

我的conf在/etc/nginx/default


33
投票

我的情况不同,我不得不杀死运行Nginx重新启动它。

代替

sudo systemctl restart nginx

我不得不使用:

sudo pkill -f nginx
sudo systemctl start nginx

23
投票

我也得到了同样的错误。

nginx:[emerg] bind()到[::]:80失败(98:地址已经在使用中)

当我在浏览器中键入localhost时,我就得到了

有用!

这是此服务器的默认网页。

Web服务器软件正在运行,但尚未添加任何内容。而不是nginx欢迎页面,apache2在同一个端口上运行,

  1. 找到apache2 ports.conf文件 sudo /etc/apache2/ports.conf
  2. 更改80以外的其他端口,我将其设为70
  3. 保存文件
  4. 重启你的系统

它也适用于您,如果您在浏览器中键入localhost,您将获得nginx欢迎页面


5
投票

我在letsencrypt(certbot)和nginx中遇到了同样的问题,

ref:https://github.com/certbot/certbot/issues/5486

此错误还没有解决方案

所以,改变了一个更新的cron (续订后重新加载) (使用certbot的建议)

-- in /etc/cron.d/certbot
from
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew 
to
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --pre-hook "service nginx stop" --post-hook "service nginx start"

日志(短):

-- in /var/log/syslog
Jun 10 00:14:25 localhost systemd[1]: Starting Certbot...
Jun 10 00:14:38 localhost certbot[22222]: nginx: [error] open() "/run/nginx.pid$
Jun 10 00:14:41 localhost certbot[22222]: Hook command "nginx" returned error c$
Jun 10 00:14:41 localhost certbot[22222]: Error output from nginx:
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:443 $
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] bind() to 0.0.0.0:80 f$
Jun 10 00:14:41 localhost certbot[22222]: nginx: [emerg] still could not bind()
Jun 10 00:14:41 localhost systemd[1]: Started Certbot.


-- in /var/log/nginx/error.log
2018/06/10 00:14:27 [notice] 22233#22233: signal process started
2018/06/10 00:14:31 [notice] 22237#22237: signal process started
2018/06/10 00:14:33 [notice] 22240#22240: signal process started
2018/06/10 00:14:34 [notice] 22245#22245: signal process started
2018/06/10 00:14:38 [notice] 22255#22255: signal process started
2018/06/10 00:14:38 [error] 22255#22255: open() "/run/nginx.pid" failed (2: No $
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:443 failed (98: Addr$
2018/06/10 00:14:39 [emerg] 22261#22261: bind() to 0.0.0.0:80 failed (98: Addre$
2018/06/10 00:14:39 [emerg] 22261#22261: still could not bind()

4
投票

尝试执行此命令

sudo fuser -k 443/tcp
service nginx restart

2
投票

我的问题是我有重叠的监听指令。我已经设法通过运行找出重叠的指令

grep -r listen /etc/nginx/*

两个文件正在同一个端口上侦听:

/etc/nginx/conf.d/default.conf:           listen 80;  
/etc/nginx/sites-enabled/default.conf:    listen 80;

1
投票

首先在/etc/apache2/ports.conf include中更改apache listen端口80到8080 apache

Listen 1.2.3.4:80 to 1.2.3.4:8080
sudo service apache2 restart 

要么

sudo service httpd restart    // in case of centos

然后添加nginx作为反向代理服务器,将侦听apache端口

server {
 listen   1.2.3.4:80;
 server_name  some.com;

 access_log  /var/log/nginx/something-access.log;

 location / {
  proxy_pass http://localhost:8080;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }


location ~* ^.+\.(jpg|js|jpeg|png)$ {
   root /usr/share/nginx/html/;
}

location /404.html {
  root /usr/share/nginx/html/40x.html;
}

error_page 404 /404.html;
    location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
    location = /50x.html {
}

# put code for static content like js/css/images/fonts
}

更改后重新启动nginx服务器

sudo service nginx restart

现在所有流量都将由nginx服务器处理,并将所有动态请求发送到apache,静态conten由nginx服务器提供。

对于缓存等高级配置:

https://www.linode.com/docs/web-servers/nginx/slightly-more-advanced-configurations-for-nginx/#basic-nginx-caching

© www.soinside.com 2019 - 2024. All rights reserved.