312

突然间我收到以下 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   - 

我难住了。我该如何调试?

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

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

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
4

21 回答 21

340

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

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

于 2013-05-28T17:27:13.493 回答
222

[::]: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

于 2013-02-26T23:52:47.663 回答
208

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

代替

sudo systemctl restart nginx

我不得不使用:

sudo pkill -f nginx & wait $!
sudo systemctl start nginx
于 2018-08-03T03:22:56.863 回答
45

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

我只需要删除/etc/nginx/sites-available/default. 然后它起作用了。

此删除将仅删除符号链接,作为备份,您可以在 /etc/nginx/sites-enabled/default

我的会议在/etc/nginx/default.

于 2013-02-24T12:47:22.490 回答
28

我也遇到了同样的错误。

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

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

有用!

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

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

  1. 找到 apache2 ports.conf文件

    sudo /etc/apache2/ports.conf
    
  2. 然后更改端口80,我将其设置为70

  3. 保存文件

  4. 重新启动系统

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

于 2013-05-29T05:37:04.920 回答
26

尝试执行此命令

sudo fuser -k 443/tcp
service nginx restart

fuser命令将找到进程 id(PID) 并且-k标志将终止启用 nginx 重新启动的进程。

于 2018-03-29T04:49:23.957 回答
14

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

grep -r listen /etc/nginx/*

两个文件在同一个端口监听:

/etc/nginx/conf.d/default.conf:           listen 80;  
/etc/nginx/sites-enabled/default.conf:    listen 80;
于 2019-01-16T17:54:43.217 回答
9

我通过运行解决了

sudo killall apache2

sudo fuser -k 443/tcp

最后

sudo service nginx start
于 2021-03-19T11:06:38.383 回答
7

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

参考: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()
于 2018-06-11T15:12:19.733 回答
4

我使用 supervisor 在 Docker 容器上并排运行 Nginx 和 Gunicorn。

这是用于 supervisor 的配置:

[supervisord]
nodaemon=true

[program:gunicorn]
command = /project/start.sh
user = www-data

[program:nginx]
command=/usr/sbin/nginx

问题是我如何默认启动 Ngnix,它在前台运行。这使得主管重试运行另一个 Nginx 实例。

您可以通过添加-g 'daemon off;'到命令行或daemon off;配置文件顶部来解决问题,Nginx 停留在前台,主管停止尝试运行另一个实例。

于 2019-07-24T10:44:42.030 回答
2

首先在 /etc/apache2/ports.conf 中将 apache 监听端口 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,静态内容由 nginx 服务器提供服务。

对于像缓存这样的高级配置:

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

于 2018-06-01T12:15:27.763 回答
2

我知道这是旧的,但还要确保你的 docker 容器都没有在端口 80 上。这是我的问题。

于 2020-12-31T17:34:36.437 回答
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这意味着有多个 nginx 块文件包含[::]:80.

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

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

于 2018-10-31T23:34:09.953 回答
1

就我而言,Apache、Apache2 或 Nginx 中的一项服务已经在运行,因此我无法启动另一项服务。

于 2018-12-22T09:31:25.303 回答
1

就我而言,罪魁祸首原来是一个服务器块,其中包含:

        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在我的配置中的其他任何地方都没有使用。

于 2019-06-29T11:05:33.607 回答
1

我在 AWS Lightsail 上遇到了这个错误,使用了上面的最佳答案

listen [::]:80;

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

然后单击我的 AWS 帐户中的“重启”按钮,我有主服务器 Apache 和 Nginx 作为代理。

于 2020-10-01T19:53:52.417 回答
1

多个服务可以在同一个端口上侦听。这个问题通常来自于在同一台机器上混合 Apache 和 NGINX。

去检查:

sudo netstat -plant | grep 80

停止 Apache 并重启 NGINX:

sudo systemctl stop apache2 && sudo systemctl restart nginx && sudo systemctl status nginx

于 2021-08-06T05:34:39.230 回答
0

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

于 2017-01-14T21:21:36.667 回答
0

继续@lfender6445 和@SAURABH 的回答——

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

vagrant ssh 进入盒子并运行以下命令以禁用 Apache2 在访客盒子启动时启动:

sudo update-rc.d -f apache2 remove

退出 ssh,vagrant 停止,vagrant up。问题解决了。

于 2018-12-03T18:58:30.050 回答
0

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

于 2018-12-04T22:45:16.313 回答
0

我有同样的问题,但我看到 Nginx 监听了端口 80:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      9730/nginx 

但是当我尝试重新启动它时,我遇到了错误:

    service nginx restart
Stopping nginx:                                            [FAILED]
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] still could not bind()

我的问题在配置文件中,我设置了 PID 文件,并且似乎系统无法正确捕获它:

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

当我删除它时,它起作用了。

于 2020-05-19T21:25:47.177 回答