Skip to main content

Web servers and networking · Intermediate · 22 min

Debug a slow or failing website with curl

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · curl 8.18 · Nginx 1.30

You will plant three faults in a practice Nginx site, then use curl 8 to find each one by reading verbose output, following a redirect loop, timing every phase of a request and checking a certificate against its name, and fix them.

On this page

When a site is slow or broken, a browser tells you very little: a spinner, a "too many redirects" page, or a certificate warning. curl shows you every stage of the request instead, so you can say which one failed: name lookup, connection, TLS handshake, the server's own work, or a redirect rule. This tutorial is about that connection-level work. Calling and testing an API with curl is covered in /tutorials/test-an-api-with-curl.

You build a small practice site first and plant three faults in it: a redirect loop, a slow backend and a certificate issued for the wrong name. Then you find each one the way you would on a real site, and fix it. The site uses the name shop.example.test. The .test top-level domain is reserved and never resolves on the internet, which forces you to use the tools that point curl at a server without DNS.

Nginx comes from the nginx.org repository, as in /tutorials/install-nginx-on-ubuntu-26-04. Every command runs on the server itself.

Step 1 · Install the tools and write a misbehaving backend

Install curl, OpenSSL (for the certificates) and Python 3 (for the practice backend). Ubuntu Server usually has all three, and apt leaves them alone if so.

Bash
sudo apt update
sudo apt install -y curl openssl python3
curl --version | head -n 1

The first line names curl 8.18.0 or a later 8.x. Everything here works on any curl from 7.71 onwards.

Make a folder for the practice files and write the backend. It has two deliberate faults: /report takes two seconds to answer, and /flaky returns 503 Service Unavailable twice before it starts working. It also has no handler for HEAD requests, which matters in a later step.

Bash
mkdir -p ~/curl-lab
cat > ~/curl-lab/backend.py <<'PY'
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import time


class Handler(BaseHTTPRequestHandler):
    flaky_calls = 0

    def do_GET(self):
        if self.path == "/report":
            time.sleep(2)  # the planted slow query
            self.reply(200, "report ready\n")
        elif self.path == "/flaky":
            Handler.flaky_calls += 1
            if Handler.flaky_calls < 3:
                self.reply(503, "busy, try again\n")
            else:
                self.reply(200, "flaky worked on call %d\n" % Handler.flaky_calls)
        else:
            self.reply(404, "not found\n")

    def reply(self, status, text):
        body = text.encode()
        self.send_response(status)
        self.send_header("Content-Type", "text/plain")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)


ThreadingHTTPServer(("127.0.0.1", 9001), Handler).serve_forever()
PY

Start it in your second terminal and leave it running. It listens on 127.0.0.1 only, so nothing outside the server can reach it.

Bash
python3 ~/curl-lab/backend.py

Check it worked

Back in the first terminal, ask the backend for a path it does not know:

Bash
curl -sS http://127.0.0.1:9001/nothing-here

It prints not found, and the second terminal logs one line for the request.

Step 2 · Plant the faults in Nginx

Create a local certificate authority (CA) and a site certificate signed by it. The certificate is issued for old.example.test, not shop.example.test: that is the third fault. The keys are created inside umask 077, so they are private to you from the moment they exist.

Bash
cd ~/curl-lab
(umask 077; openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out ca.key)
openssl req -x509 -new -key ca.key -sha256 -days 30 -subj "/CN=Curl Lab CA" -out ca.pem
(umask 077; openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out site.key)
printf 'subjectAltName=DNS:old.example.test\n' > old.ext
openssl req -new -key site.key -subj "/CN=old.example.test" -out old.csr
openssl x509 -req -in old.csr -CA ca.pem -CAkey ca.key -CAcreateserial -days 30 -sha256 -extfile old.ext -out site.pem

Copy the certificate and key to where Nginx reads them. install -m 600 creates the key file readable by root only.

Bash
sudo install -d -m 755 /etc/nginx/curl-lab
sudo install -m 644 ~/curl-lab/site.pem /etc/nginx/curl-lab/site.pem
sudo install -m 600 ~/curl-lab/site.key /etc/nginx/curl-lab/site.key

Put the redirect rules in their own small file, so you can fix them later without touching anything else. /account sends visitors to /login, and /login sends them straight back: the first fault.

Bash
sudo tee /etc/nginx/curl-lab/redirects.conf > /dev/null <<'CONF'
location = /account { return 301 /login; }
location = /login   { return 302 /account; }
CONF

Now the site itself: plain HTTP on port 80 with a health check, the redirects and two paths passed to the backend, and HTTPS on port 443 with the wrong-name certificate. The file name starts with zz- so it sorts last in conf.d. Nginx treats the first block it reads for a port as that port's default, so a name that sorts last never takes over requests meant for your other sites.

Bash
sudo tee /etc/nginx/conf.d/zz-curl-lab.conf > /dev/null <<'CONF'
server {
    listen 80;
    server_name shop.example.test;
    default_type text/plain;

    location = /health { return 200 "ok\n"; }

    include /etc/nginx/curl-lab/redirects.conf;

    location /report { proxy_pass http://127.0.0.1:9001; }
    location /flaky  { proxy_pass http://127.0.0.1:9001; }
}

server {
    listen 443 ssl;
    server_name shop.example.test;
    default_type text/plain;

    ssl_certificate     /etc/nginx/curl-lab/site.pem;
    ssl_certificate_key /etc/nginx/curl-lab/site.key;

    location = / { return 200 "shop home over https\n"; }
}
CONF
sudo nginx -t

nginx -t ends with syntax is ok and test is successful. Load the new configuration:

Bash
sudo systemctl reload nginx

Check it worked

Bash
curl -sS -H 'Host: shop.example.test' http://127.0.0.1/health

It prints ok. The Host header told Nginx which site you meant, since you connected by IP address.

Step 3 · Point curl at a server before DNS does

Setting the Host header works for plain HTTP, but it leaves the URL saying 127.0.0.1, and for HTTPS that breaks the certificate check (you will see why in the TLS step). The better tool is --resolve, which tells curl "for this name and port, use this address" and changes nothing else:

Bash
curl -sS --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/health

The format is name:port:address. The URL, the Host header, the TLS server name and the certificate check all use shop.example.test, exactly as they would once DNS points at the server.

--connect-to does the same job one level down. It maps a name and port to a different name and port, so you can send a request for one host to another machine, or to another port, while the request itself stays unchanged:

Bash
curl -sS --connect-to shop.example.test:80:127.0.0.1:80 http://shop.example.test/health

Use --resolve when you know the new server's IP address. Use --connect-to when the target is itself a name (a load balancer such as lb-2.internal) or listens on another port.

The everyday use is a migration. Before you switch DNS to a new server, run your checks against it from anywhere. Replace www.example.com and 203.0.113.10 with your site's name and the new server's address:

Bash
curl -sSI --resolve www.example.com:443:203.0.113.10 https://www.example.com/
curl -sSI --connect-to www.example.com:443:203.0.113.10:443 https://www.example.com/

Check it worked

Both lab commands printed ok, with no change to /etc/hosts and no DNS record.

Step 4 · Read a request and its response line by line

Ask for the health check with -v:

Bash
curl -v --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/health

The output looks something like this. The first character of each line tells you who said it:

Text
* Added shop.example.test:80:127.0.0.1 to DNS cache
* Connected to shop.example.test (127.0.0.1) port 80
> GET /health HTTP/1.1
> Host: shop.example.test
> User-Agent: curl/8.18.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.30.8
< Content-Type: text/plain
< Content-Length: 3
<
ok
  • * lines are curl's own notes: which address it used and whether the connection opened. If the output stops at Trying ... and then times out, the problem is the network or a firewall, not the web server.
  • > lines are what curl sent. Check the method, the path and the Host header here.
  • < lines are what came back. The status line comes first, then the headers, then an empty < line that ends the headers.
  • Anything without a prefix is the body.

-v writes to standard error, and the body goes to standard output. Add -o /dev/null to see only the conversation.

When you only want the headers, there are two options, and they do not ask the server the same question. -i sends a normal GET and prints the response headers above the body. -I sends a HEAD request, which asks for headers only:

Bash
curl -sS -i --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/report
curl -sS -I --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/report

The first takes two seconds and returns 200 OK with report ready. The second comes back at once with 501 Not Implemented, because the backend never learnt to answer HEAD. Plenty of real applications are the same, and some caches and frameworks treat HEAD differently from GET. When -I disagrees with what a browser sees, repeat the request with -i, which is what a browser sends.

Check it worked

You can point to the > line that carries Host: shop.example.test, and you have seen /report give two different status codes depending on the method.

Step 5 · Find and fix the redirect loop

A visitor who opens /account gets a "too many redirects" error. -L makes curl follow redirects, and --max-redirs stops it after a few hops instead of curl's default of 50:

Bash
curl -sS -I -L --max-redirs 4 --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/account

You see the response headers of each hop, alternating between 301 Moved Permanently with Location: http://shop.example.test/login and 302 Moved Temporarily with Location: http://shop.example.test/account. The run ends with something like curl: (47) Maximum (4) redirects followed, and curl exits with code 47.

Two URLs that point at each other are the whole diagnosis. On a real site, the cause is often a rule that depends on something curl lets you vary: an HTTPS redirect behind a proxy that talks plain HTTP to the server, a www rule in both the app and the web server, or a login page that needs a cookie. Repeat the command with -H 'X-Forwarded-Proto: https' or -b 'session=...' to see which one ends the loop.

Nginx wrote full URLs in Location even though the rules say /login, because its absolute_redirect setting is on by default and builds the URL from the request's Host header.

Here the fault is the /login rule. Make it serve the login page instead of redirecting. This rewrites the whole small file:

Bash
sudo tee /etc/nginx/curl-lab/redirects.conf > /dev/null <<'CONF'
location = /account { return 301 /login; }
location = /login   { return 200 "login form\n"; }
CONF
sudo nginx -t
Bash
sudo systemctl reload nginx

Check it worked

Bash
curl -sS -L -o /dev/null -w '%{num_redirects} redirect, ended at %{url_effective}\n' --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/account

It prints 1 redirect, ended at http://shop.example.test/login. Drop -o /dev/null -w ... and you see login form.

Step 6 · Time each phase to find the slow one

-w prints values about the transfer after it finishes. Keep a template in a file, so you can reuse it on any site. Each value is the time in seconds from the start of the request until that phase ended:

Bash
cat > ~/curl-lab/timing.txt <<'EOF'
   dns lookup: %{time_namelookup}s
  tcp connect: %{time_connect}s
tls handshake: %{time_appconnect}s
 request sent: %{time_pretransfer}s
   first byte: %{time_starttransfer}s
        total: %{time_total}s
       status: %{http_code}
EOF

Time the health check, then the report page:

Bash
curl -sS -o /dev/null -w @$HOME/curl-lab/timing.txt --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/health
curl -sS -o /dev/null -w @$HOME/curl-lab/timing.txt --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/report

For /health every line is a few thousandths of a second. For /report, everything up to request sent is just as quick, then first byte jumps to about 2.0s. tls handshake is 0 because this is plain HTTP.

Read the gaps between lines, not the lines themselves:

  • A large dns lookup means slow or failing DNS. Try another resolver, or check the record's TTL.
  • A large gap from dns lookup to tcp connect is network distance or packet loss. A connection that never completes is a firewall or a server that is down.
  • A large gap to tls handshake points at a far-away TLS endpoint or an overloaded one.
  • A large gap from request sent to first byte is the server working: the application, a database query or a slow upstream. That is the fault here.
  • A large gap from first byte to total is the size of the response and the speed of the link.

So the network is fine and Nginx is fine. The backend is slow, and the fix belongs in the application, not the server configuration.

If many of your users are on mobile data in, say, Lagos or Nairobi, run the same template from a machine in that region as well. Connection and TLS times that look fine from a European data centre can be much larger there, and the fix for that is a server or CDN closer to them, not faster code.

To see where the wait sits inside the conversation, record a full trace with a timestamp on every line. --trace-ascii writes everything curl sends and receives to a file:

Bash
curl -sS -o /dev/null --trace-time --trace-ascii ~/curl-lab/report-trace.txt --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/report
grep -E 'Send header|Recv header' ~/curl-lab/report-trace.txt

The timestamp on the last => Send header line and on the first <= Recv header line are about two seconds apart: the request was sent and nothing came back until the backend finished.

A trace holds every header and body byte, including cookies and Authorization headers. Remove those before you paste a trace into a ticket or a chat.

Check it worked

Your /report timing shows a first byte near 2.0s while every earlier phase is close to zero. One last check shows how a client sees this when it runs out of patience. Give curl one second:

Bash
curl -sS --max-time 1 --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/report

It fails with something like curl: (28) Operation timed out after 1001 milliseconds with 0 bytes received. Exit code 28 always means a timeout, so scripts can tell it apart from a refused connection (code 7).

Step 7 · Find and fix the wrong-name certificate

Request the HTTPS site:

Bash
curl -sS --resolve shop.example.test:443:127.0.0.1 https://shop.example.test/

It fails with exit code 60 and something like SSL certificate problem: unable to get local issuer certificate. curl does not know your lab CA, so it cannot tell who signed the certificate.

The tempting fix is -k (--insecure):

Bash
curl -sS -k --resolve shop.example.test:443:127.0.0.1 https://shop.example.test/

It prints shop home over https, and that is the problem. -k turns off both checks curl makes: who signed the certificate, and whether it was issued for the name you asked for. It just hid the second fault from you. Anyone between you and the server could answer with their own certificate and -k would accept it. Never leave it in a script, a health check or a deployment pipeline.

The right tool is --cacert, which tells curl to trust one specific CA for this request and keeps every check switched on:

Bash
curl -sS --cacert ~/curl-lab/ca.pem --resolve shop.example.test:443:127.0.0.1 https://shop.example.test/

Now the signature checks out and curl reaches the next problem: still exit code 60, with something like SSL: no alternative certificate subject name matches target host name 'shop.example.test'. Look at the names the server is really presenting:

Bash
openssl s_client -connect 127.0.0.1:443 -servername shop.example.test < /dev/null 2> /dev/null | openssl x509 -noout -subject -ext subjectAltName

It shows subject=CN=old.example.test and DNS:old.example.test. The certificate belongs to another site. Issue one for the right name, with the same key, and install it:

Bash
cd ~/curl-lab
printf 'subjectAltName=DNS:shop.example.test\n' > shop.ext
openssl req -new -key site.key -subj "/CN=shop.example.test" -out shop.csr
openssl x509 -req -in shop.csr -CA ca.pem -CAkey ca.key -CAcreateserial -days 30 -sha256 -extfile shop.ext -out site.pem
sudo install -m 644 ~/curl-lab/site.pem /etc/nginx/curl-lab/site.pem
sudo nginx -t
Bash
sudo systemctl reload nginx

On a public site the same two errors usually have different causes: "unable to get local issuer certificate" most often means the server is not sending its intermediate certificate, and a name mismatch means the certificate does not list a name such as www. /tutorials/fix-tls-certificate-chain-and-hostname-errors works through both.

Check it worked

Bash
curl -sS --cacert ~/curl-lab/ca.pem --resolve shop.example.test:443:127.0.0.1 https://shop.example.test/

It prints shop home over https, with every check on. Now see why --resolve beats a Host header for HTTPS:

Bash
curl -sS --cacert ~/curl-lab/ca.pem -H 'Host: shop.example.test' https://127.0.0.1/

It fails again with exit code 60, even with the correct certificate in place. The Host header travels inside the encrypted connection, but the certificate check happens before that, against the name in the URL: 127.0.0.1. curl also sends no server name (SNI) during the handshake when the URL holds an IP address, so a server with several HTTPS sites cannot tell which certificate you want. --resolve keeps the real name in the URL and avoids both problems.

Step 8 · Ride out brief failures with retries

Ask for the flaky page once:

Bash
curl -sS -i --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/flaky

It returns HTTP/1.1 503 Service Unavailable and busy, try again. Without --fail, curl still exits with code 0, because it did get an answer. Now ask with retries:

Bash
curl -sS --retry 3 --retry-delay 1 --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/flaky

curl prints a warning along the lines of Will retry in 1 second. 3 retries left., waits, tries again, and the output ends with flaky worked on call 3.

What --retry does and does not do:

  • It retries only on errors that are likely to pass: a timeout, or an HTTP 408, 429, 500, 502, 503 or 504. A 404 or a certificate error is never retried. Add --retry-all-errors only when you are sure every failure is worth repeating.
  • Without --retry-delay, curl waits one second and doubles the wait each time. If the server sends a Retry-After header, curl waits as long as it says.
  • Retrying a POST that charges a card or sends a message can do it twice. Use retries for reads, health checks and downloads.

A slow or flaky backend shows up in the timing first and in the retries second. In a monitoring script, combine the two: --max-time so a hung server cannot hang the script, --retry for a single blip, and --fail so a final error status gives a non-zero exit code.

Check it worked

The retry command ended with flaky worked on call 3. Run it again and it succeeds on the first try, because the backend only misbehaves for its first two calls.

Step 9 · Remove the practice site

Stop the backend with Ctrl+C in the second terminal.

The next commands delete the lab's Nginx files, its certificates and the ~/curl-lab folder, including the CA key. They touch nothing else.

Bash
sudo rm /etc/nginx/conf.d/zz-curl-lab.conf
sudo rm -r /etc/nginx/curl-lab
sudo nginx -t
Bash
sudo systemctl reload nginx
Bash
rm -r ~/curl-lab

Check it worked

Bash
curl -s -o /dev/null -w '%{http_code}\n' --resolve shop.example.test:80:127.0.0.1 http://shop.example.test/health

The request no longer returns the lab's ok. You get whatever your default site answers for an unknown name, such as 404, or 000 if that site closes the connection.

Next steps

  • Test API endpoints, JSON bodies and authorization headers with curl: /tutorials/test-an-api-with-curl.
  • Follow one request through DNS, TCP, TLS and HTTP to see what each timing phase covers: /tutorials/how-a-request-reaches-your-server.
  • Fix missing intermediates and hostname mismatches on a public site: /tutorials/fix-tls-certificate-chain-and-hostname-errors.
  • Run two sites on one server and check each before DNS points at it: /tutorials/host-multiple-sites-with-nginx-server-blocks.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.