Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

curl cannot connect to port 80 or 443 on a server running nginx

Errorcurl: (7) Failed to connect to example.com port 80

Short 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.

  • Applies to nginx 1.26 and 1.28, curl 8.x, Ubuntu 24.04 with UFW
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.1
  2. 2Match what you see, then jump to its fix

    1. 1ss prints nothing for the port, and the local curl also fails with exit code 7nothing is listening on that portFix
    2. 2ss 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. 3Local curl works, ss shows nginx on 0.0.0.0, remote curl failsThe firewall blocks the port from outsideFix

What does the error look like?

  • curl: (7) Failed to connect to example.com port 80
  • CURLE_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 nginx

Confirmed by curl.se

Did this fix it?

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

Did this fix it?

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 verbose

2Fix

Allow the web ports through UFW.

sudo ufw allow http
sudo ufw allow https

3Verify

A request from another machine connects.

curl -I http://example.com

Confirmed by ubuntu.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.