Fix nginx "duplicate listen options for 0.0.0.0:80"
nginx "duplicate listen options for 0.0.0.0:80": two listen lines on one address:port both set socket options like reuseport or backlog. Keep them on one.
nginx: [emerg] duplicate listen options for 0.0.0.0:80 in /etc/nginx/conf.d/site-b.conf:2nginx: configuration file /etc/nginx/nginx.conf test failedTwo or more server blocks share the address and port (0.0.0.0:80), and **more than one of their listen lines carries socket options such as reuseport or backlog=. Those options may be given only once per port. Collect every option you need on one listen line** and remove them from the others. The error fires even when the option names or values differ (for example reuseport on one line and backlog=4096 on another), so simply deleting one side silently drops an option that only that side had. Compare the lines first.
The official nginx documentation says these parameters "can be specified in any listen directive, but only once for a given address:port pair". The error text itself is not in the docs; it is duplicate listen options for %V in the source (src/http/ngx_http.c).
[1] Find which files collide. The file and line in the message (site-b.conf:2) are the later one. Search the .conf files for the option names. nginx -T is not usable while the config test is failing.
$ grep -Rn --include="*.conf" -E "reuseport|backlog|bind|so_keepalive" /etc/nginx//etc/nginx/conf.d/site-a.conf:2: listen 80 reuseport;/etc/nginx/conf.d/site-b.conf:2: listen 80 backlog=4096;site-a has reuseport and site-b has backlog=4096. Different options still collide because the port is the same, which is what happens when site configs are written separately. Without --include, binaries under modules/ match too and flood the output. Use capital -R: lowercase -r skips symlinks in sites-enabled/.
[2] Check which parameters count. In the source, the error fires when two listen lines both set a socket option. Results from this reproduction:
listen in two servers | Result |
|---|---|
two listen 80 backlog=511; | error |
two listen 80 reuseport; | error |
two listen 80 so_keepalive=on; | error |
two listen 80 bind; | error |
listen 80 reuseport; and listen 80; | passes |
two listen 80; | passes |
listen 80 reuseport; and listen [::]:80 reuseport; | passes (different address) |
listen 80 backlog=511 default_server; and listen 80; | passes |
default_server, ssl and http2 do not take part in this check (they are not in the source's list that triggers it, and in the test two default_server lines gave a different error). By the source, setfib, fastopen, rcvbuf, sndbuf, accept_filter, deferred, ipv6only and multipath belong to the same group, but only backlog, reuseport, so_keepalive and bind were run here.
[3] Merge the options each site used onto one line. If the options are identical, keeping one side is enough, but when they differ, as here, first decide whether both reuseport and backlog=4096 are needed. If the same option has different values (for example backlog=511 and backlog=4096), pick one value deliberately. Put the merged line on the site that should own the port (the first one, or the one with default_server) and remove all options from the other listen lines. Back up both files before editing.
$ mkdir -p /var/backups/nginx$ cp /etc/nginx/conf.d/site-a.conf /etc/nginx/conf.d/site-b.conf /var/backups/nginx/The listen lines after the edit:
$ grep -Rn --include="*.conf" "listen" /etc/nginx/conf.d//etc/nginx/conf.d/site-a.conf:2: listen 80 reuseport backlog=4096;/etc/nginx/conf.d/site-b.conf:2: listen 80;[4] Test again, then start nginx and check that both hosts answer. nginx -t only checks and does not apply anything: if nginx is stopped, start it with nginx; if it is running, use nginx -s reload.
$ nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful$ nginx$ curl -s -H 'Host: a.example.com' http://127.0.0.1/a$ curl -s -H 'Host: b.example.com' http://127.0.0.1/bBoth hosts answered normally after the start (this test started nginx from a stopped state). Under systemd, apply it with sudo systemctl reload nginx. Whether a reload applies options that change the listening socket itself, such as reuseport, was not verified, so schedule a restart if it matters.
Repeating the same line inside one server is a different error. Testing server { listen 80; listen 80; } makes nginx -t print this line (the middle one of the three error lines):
nginx: [emerg] a duplicate listen 0.0.0.0:80 in /etc/nginx/nginx.conf:3Delete the repeated line; it has nothing to do with the options. For two default_server lines see a duplicate default server; for overlapping names that only warn, see conflicting server name … ignored.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.