Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

nginx: bind() to 0.0.0.0:80 failed (98: Address already in use)

Port 80 belongs to something else: an old nginx master, Apache, or a published container. Locate the listener, then fix the duplicate listen lines.

~5 min readLinux
#nginx#linux#networking#systemd
Bold white headline reading PORT CONFLICT with an nginx socket crate blocked at an amber port gate beside a cyan rival listener.

Introduction & Quick Summary

nginx will not start, or will not reload:

code
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()

98 is the Linux errno EADDRINUSE: the socket the configuration asks for is already owned by another process, or by another socket in the same process. The message repeats per listen line, so a config with both IPv4 and IPv6 addresses prints two lines and then summarises.

Quick answer:

bash
sudo ss -ltnp 'sport = :80' # who owns the port, protocol, address sudo lsof -iTCP:80 -sTCP:LISTEN sudo ss -ltnp 'sport = :443'

Kill or stop that owner, or move nginx to another port — but decide which one you actually want, because the owner is often a second copy of nginx that survived a failed restart.

The four processes that most often hold port 80 on a Linux host: an nginx master left over from a manual nginx -s stop that did not finish, Apache or httpd, docker-proxy because a container published -p 80:80, and a Caddy or Node process started by a developer. On a fresh cloud image it is usually Apache, which the distribution enabled by default while nginx was installed alongside it.

Core Mechanics, Signatures & Reproducible Setup

nginx binds one listening socket per listen directive after dropping privileges, but before writing its PID file. A bind failure aborts the whole startup, so a single conflicting line prevents every other port from being served.

Address semantics matter more than they look:

nginx
server { listen 80; # 0.0.0.0:80 (IPv4 wildcard) listen [::]:80; # [::]:80 with ipv6only=on by default listen 127.0.0.1:8080; # loopback only listen 80 default_server; # catch-all for unmatched Host }

listen 80 and listen [::]:80 are different sockets on Linux, even though a client can reach both. ipv6only=off on the IPv6 socket makes it accept IPv4-mapped connections too, which then conflicts with the explicit IPv4 listen and produces the second line of the error above.

Reproduce both cases on a spare port without touching production:

bash
sudo nginx -c /dev/null # control: starts with no config python3 -m http.server 8080 & # occupies 8080 echo 'server { listen 8080; }' | sudo tee /etc/nginx/conf.d/repro.conf sudo nginx -t && sudo nginx # bind() to 0.0.0.0:8080 failed (98) kill %1 && sudo nginx # succeeds once the port is free

nginx -t validates syntax and does not bind, which is why a configuration can pass -t and still fail at startup.

Key Comparison Table

The errno in the message is the fastest classifier:

MessageerrnoCauseFix
98: Address already in useEADDRINUSEanother listener owns the addressstop the owner or change the port
13: Permission deniedEACCESbinding a port below 1024 as non-rootgrant CAP_NET_BIND_SERVICE or use a high port
99: Cannot assign requested addressEADDRNOTAVAILthe address does not exist on this hostfix the listen address
97: Address family not supportedEAFNOSUPPORTIPv6 listen on an IPv4-only hostremove [::] lines
still could not bind()summaryseveral listen lines failedfix them all before restarting

Finding the owner:

ToolCommandNotes
ssss -ltnp 'sport = :80'shows process name and PID, needs root
lsoflsof -iTCP:80 -sTCP:LISTENworks on macOS too
fuserfuser -n tcp 80prints PIDs only, useful in scripts
systemctlsystemctl status nginx apache2 httpdshows which unit owns a master
dockerdocker ps --format '{{.Names}} {{.Ports}}'rows with 0.0.0.0:80-> are the cause

Where the conflict is configured, by deployment shape:

ShapeUsual occupantWhere to look
host with nginx + ApacheApache on 80, nginx on 8080/etc/apache2/ports.conf
nginx in Dockerhost nginx, or docker-proxydocker ps, compose ports:
Kubernetes ingresshost nginx from the host OShostPort in the DaemonSet
laptop, dev servera previous npm run devss, or lsof

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. A failed restart leaves two masters

systemctl restart nginx stops the old master and starts a new one. When the stop times out, the unit is marked failed but the old master keeps the sockets, and the start fails with EADDRINUSE:

bash
sudo systemctl status nginx --no-pager ps -eo pid,ppid,cmd | grep '[n]ginx' sudo systemctl stop nginx sudo pkill -TERM -f 'nginx: master' # only if the unit is genuinely stuck sleep 2 && sudo systemctl start nginx

Prefer sudo nginx -s reload for configuration changes: reload passes the existing sockets to new workers and never rebinds, so a live master cannot conflict with itself.

2. Docker published port

bash
docker ps --format 'table {{.Names}}\t{{.Ports}}' | grep ':80->' # web 0.0.0.0:80->80/tcp docker compose down # or change the host side: "127.0.0.1:8080:80"

The listener is docker-proxy, not the container process, so ss attributed the port to a name you do not recognise. Binding the published port to 127.0.0.1 keeps it reachable only through nginx, which is usually the intent.

3. The IPv6 listener that blocks IPv4

nginx
# both sockets, the recommended form listen 80; listen [::]:80; # one dual-stack socket; the IPv4 line must then be removed listen [::]:80 ipv6only=off;

Keeping ipv6only=off and listen 80 together produces exactly the pair of messages in the introduction: the IPv6 socket already covers IPv4 and the second bind fails.

4. Ports below 1024 without root

bash
sudo setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx # or, for containers, keep the default user and raise the floor sudo sysctl -w net.ipv4.ip_unprivileged_port_start=80

The setcap route is lost on every nginx upgrade, since the package replaces the binary. In a container, ip_unprivileged_port_start inside the net namespace is the portable option, and it is what Kubernetes does for you when a pod requests a low port.

5. default_server duplicates

Two server blocks with default_server on the same listen address fail with a different message:

nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/a.conf:3

That one names the file, so it is easier. It appears after copying a vhost file and forgetting to change the flag, and grep -rn default_server /etc/nginx finds both.

6. Reload versus restart semantics

A restart closes and reopens sockets; a reload keeps them. On a busy host, a reload during a deploy is safer even for a port change — but a port change is one of the few things a reload cannot apply, because the listening set changes. Confirm the live listening set after any change:

bash
sudo ss -ltnp | grep nginx sudo nginx -T | grep -n 'listen'

nginx -T prints the merged configuration including includes, which is the only way to see which file actually contributed a listen line.

Common Compilation/Runtime Mistakes & Version Gotchas

Killing by PID file. cat /var/run/nginx.pid | xargs kill fails silently when the PID file is stale and the number belongs to another process. Match on the command line (pgrep -a nginx) instead.

Assuming TIME_WAIT blocks the bind. Closed connections sit in TIME_WAIT for a minute, but nginx sets SO_REUSEADDR on listening sockets, so those sockets do not conflict. ss -tan state time-wait showing thousands of entries is normal and not the cause.

Editing a vhost and reloading when the failure is in another file. sites-enabled is usually a symlink farm; a stale link to a deleted file still loads. nginx -T plus the line number in the [emerg] message identifies the contributing file.

Version differences. nginx 1.25.1 moved the http2 parameter off listen into a standalone http2 on; directive, and configurations using listen 443 ssl http2; now emit a deprecation warning that fails -t under error_log ... warn. reuseport requires kernel 3.9 or later and is not enabled by default; adding it to every listen line lets several nginx instances bind the same port, which surprises anyone who expects the second one to fail. The [::] default changed from ipv6only=off to on in nginx 1.3.4, so advice written for older releases reverses the fix.

Verifying from the outside. Once nginx starts, the port is not proof the site is reachable: a firewall rule or a security group can still drop traffic. The CIDR calculator confirms whether a client address falls inside the allowed range, and the HTTP status code reference distinguishes a connection refusal from a 502 returned by a reverse proxy that is up but cannot reach its upstream.

Reference material: ngx_http_core_module listen directive and the nginx beginner's guide.

Official references

primary documentation

The Linux documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Linux guides

More in Linux

All Linux guides

The guides filed next to this one in Linux, in library order.