できない.dev

nginx「bind() to 0.0.0.0:80 failed (98: Address already in use)」の原因と直し方

nginx の Address already in use は、そのポートを使っている別のプロセスを止めれば直る。
動いている nginx をもう一度起動しても出るので、設定を変えたときは nginx -s reload で読み直す。

公開: 更新:

症状できない · exit 1
nginx
nginx: [emerg] bind() to 0.0.0.0:80 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:80 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:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
解決後できた · exit 0
nginx

2026-11-20 に nginx:1.27 で実際に打って取った出力。検証環境の詳細

要約

nginx を起動すると、次の行が 500 ミリ秒おきに繰り返され、最後に起動をあきらめる。

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

bind() to 0.0.0.0:80 failed (98: Address already in use) は、listen に書いたアドレスとポートを、すでに別のプロセスが使っているときのエラーである。
そのポートを使っているプロセスを確かめて止めれば直る。
相手が動いている nginx 自身なら、起動し直さずに nginx -s reload を送る。

実行例

nginx:1.27 で 80 番を別のプロセスが握っていると、nginx は 5 回リトライして終了コード 1 で終わった。
ss -ltnp で握っているプロセスを特定して止めると起動でき、動作中にもう一度 nginx を打つと同じエラーになった。

$ nginx
nginx: [emerg] bind() to 0.0.0.0:80 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:80 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:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
$ echo $?
1
$ ss -ltnp "sport = :80"
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      5            0.0.0.0:80        0.0.0.0:*    users:(("perl",pid=273,fd=3))
$ nginx
$ echo $?
0
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/
200
$ nginx
nginx: [emerg] bind() to 0.0.0.0:80 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:80 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:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
$ echo $?
1
$ nginx -t && nginx -s reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2026/11/19 19:13:50 [notice] 285#285: signal process started
$ echo $?
0

— 2026-11-20 時点の出力

検証環境2026-11-20 検証
検証日
実行環境
nginx:1.27 Debian GNU/Linux 12 (bookworm)

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. ポートを使っているプロセスを確かめる

sudo ss -ltnp 'sport = :80'

出力の一例として users:(("nginx",pid=1,fd=6)) のように、待ち受けているプロセスの名前と PID が出る。
nginx 以外なら、そのサービスを止めるか、nginx の listen を別のポートに変える。
ss が無い環境では、sudo lsof -i :80 でも同じことが分かる。

2. 動いている nginx には reload を送る

nginx -t && nginx -s reload

設定を変えたあとは、起動し直さずに reload を送る(Beginner’s Guide(新しいタブで開く))。
上のイメージでは signal process started と出て、終了コード 0 で終わった。
systemd で動かしているなら sudo systemctl reload nginx を使う。

3. IPv4 と IPv6 を別々に待ち受ける

server {
    listen 80;
    listen [::]:80;
}

ipv6only の既定は on で、listen [::]:80; は IPv6 だけを受け持つ(listen(新しいタブで開く))。
3 の設定から ipv6only=off を消すと、nginx は起動し、curl localhost に応答した。

よくある原因

  1. 別のプロセスが同じポートを使っている。
    Apache や開発用のサーバーが 80 番を取っていると、nginx は起動できない。
    nginx 1.31.8 の公式イメージで、同じネットワークの中で python -m http.server 80 を動かしてから nginx を打つと、上のエラーになった。
  2. nginx を二重に起動している。
    nginx が動いている状態で nginx を打つと、新しい nginx が同じポートを取りにいって失敗する。
    同じイメージで試すと、5 回試したあとに still could not bind() が出て、終了コードは 1 だった。
  3. listen の書き方で、同じポートを二重に取っている。
    listen 80; と listen [::]:80 ipv6only=off; を同じ設定に書くと、IPv6 のソケットが IPv4 の 80 番も受け持とうとする。
    このときは bind() to [::]:80 failed だけが出た。

nginx -t は listen が実際に取れるかまでは確かめない。
3 の設定でも test is successful と出たあとに、起動で失敗した。