curl cannot connect to port 80 or 443 on a server running nginx
curl: (7) Failed to connect to example.com port 80Short answer
Exit code 7 means the TCP connection itself failed, before any HTTP happened. So nginx config rules like server_name are not involved yet. Check what is listening with ss, whether nginx listens on the address and IP version your client uses, and whether the firewall lets ports 80 and 443 in.
Fix it fast
1Find out which cause you have
On the server, list what is listening on 80 and 443, then try the same request from the server itself. If local works and remote fails, the firewall or the listen address is the cause.
sudo ss -ltnp | grep -E ':(80|443)\b' curl -sI http://127.0.0.12Match what you see, then jump to its fix
- 1
ss prints nothing for the port, and the local curl also fails with exit code 7nothing is listening on that portFix - 2
ss shows 127.0.0.1:80 or [::]:80 only, and remote clients on IPv4 failnginx listens only on one address or only on IPv6Fix - 3
Local curl works, ss shows nginx on 0.0.0.0, remote curl failsThe firewall blocks the port from outsideFix
- 1
What does the error look like?
curl: (7) Failed to connect to example.com port 80CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Cause 1 of 3
nothing is listening on that port
1Check
Exit code 7 is libcurl's failure to connect to the host or proxy. Check that nginx is running and has a listen directive for the port.
systemctl is-active nginx
grep -rn "listen" /etc/nginx/sites-enabled/ /etc/nginx/conf.d/2Fix
Start nginx (fix the config first if the test fails) and add a listen line for the missing port. For 443 the listen line needs the ssl parameter.
server {
listen 80;
listen 443 ssl;
}3Verify
ss now shows nginx on both ports.
sudo ss -ltnp | grep nginxConfirmed by curl.se
Cause 2 of 3
nginx listens only on one address or only on IPv6
1Check
listen with only a port accepts any address; listen 127.0.0.1 does not. A listen on [::] has ipv6only on by default, so it takes IPv6 only.
grep -rn "listen" /etc/nginx/
sudo ss -ltn | grep ':80'2Fix
Use a plain listen for IPv4 and add the [::] line for IPv6 beside it.
listen 80;
listen [::]:80;3Verify
Both 0.0.0.0:80 and [::]:80 appear.
sudo nginx -t && sudo systemctl reload nginx && sudo ss -ltn | grep ':80'Confirmed by nginx.org
Cause 3 of 3
The firewall blocks the port from outside
1Check
Look at the UFW rules on the server. A cloud security group or provider firewall is a second place to check.
sudo ufw status verbose2Fix
Allow the web ports through UFW.
sudo ufw allow http
sudo ufw allow https3Verify
A request from another machine connects.
curl -I http://example.comConfirmed by ubuntu.com
How do I stop it coming back?
- After any server change, test both ports from a second machine, not only from the server.
- List the firewall rules in your server notes, including the cloud provider's firewall.
- Keep listen lines explicit: one for IPv4 and one for IPv6.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
nginx will not start because port 80 is already taken
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Error 98 means another process already listens on the address and port that nginx wants.
2 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Your website on a Google Compute Engine VM times out on port 80
The site works with curl on the VM but not from outside
A timeout, as opposed to a refusal, usually means a firewall dropped the packets.
3 causes, each with a fixServers
Gunicorn rejects a request with "Error parsing headers: limit request headers fields size"
Error parsing headers: 'limit request headers fields'
Gunicorn caps the size of one header field at 8190 bytes and the number of headers at 100 by default.
2 causes, each with a fixServers
HAProxy answers 503 "No server is available to handle this request"
No server is available to handle this request.
HAProxy sends 503 when it has no server to give a request to.
4 causes, each with a fix