Web servers and networking · Beginner · 16 min
What happens when someone visits your site: DNS, TCP, TLS and HTTP
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Nginx 1.30.8 · curl 8.18
You will follow one HTTPS request to your own Nginx server phase by phase, and time the DNS lookup, TCP connection, TLS handshake and first byte with curl.
On this page
When someone types your address into a browser, four separate conversations happen before they see a single word. Their device asks a DNS resolver for your server's IP address. It opens a TCP connection to that address. For HTTPS, it runs a TLS handshake that agrees on keys and checks your certificate. Only then does it send the HTTP request and wait for your server to answer.
Each phase can be the slow or broken one, and each has its own fix. Knowing which phase you are in is the first thing to work out when a site is slow or fails to load.
In this tutorial you build a small lab on one machine: Nginx serving demo.example over HTTP and HTTPS with a self-signed certificate, and a tiny app behind it. You then walk one request through each phase with curl, and time every phase with curl's -w timers. The name demo.example is reserved for documentation, so it never clashes with a real site; curl's --resolve option points it at your own machine.
Step 1 · Build the lab: Nginx, a certificate and a small app
Install Nginx from nginx.org first, as in /tutorials/install-nginx-on-ubuntu-26-04.
Then install the other tools this tutorial uses. Ubuntu server ships most of them already; apt says so if they are there.
sudo apt-get install -y curl openssl python3Create a self-signed certificate for demo.example. The private key goes in a directory only root can read, so it is protected from the moment it is written. The certificate is public, so keep a copy in a demo folder where curl can read it.
mkdir -p ~/request-demo
sudo install -d -m 700 /etc/nginx/tls
sudo openssl req -x509 -newkey rsa:2048 -nodes -days 30 \
-subj "/CN=demo.example" \
-addext "subjectAltName=DNS:demo.example" \
-keyout /etc/nginx/tls/demo.key \
-out /etc/nginx/tls/demo.crt
sudo cat /etc/nginx/tls/demo.crt > ~/request-demo/demo.crtWrite the app. It waits 0.3 seconds, to stand in for real work such as a database query, and then reports what it received from Nginx:
cat > ~/request-demo/app.py <<'PY'
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
time.sleep(0.3)
lines = [
f"app saw Host: {self.headers.get('Host')}",
f"app saw X-Forwarded-For: {self.headers.get('X-Forwarded-For')}",
f"app saw X-Forwarded-Proto: {self.headers.get('X-Forwarded-Proto')}",
f"app saw the connection come from: {self.client_address[0]}",
]
body = ("\n".join(lines) + "\n").encode()
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
HTTPServer(("127.0.0.1", 9080), Handler).serve_forever()
PY
nohup python3 ~/request-demo/app.py > /dev/null 2>&1 &
sleep 1Now add a server block. It answers on port 80 and on port 443 with TLS, returns a short line at /, and passes anything under /app/ to the app:
sudo tee /etc/nginx/conf.d/demo.conf > /dev/null <<'CONF'
server {
listen 80;
listen 443 ssl;
server_name demo.example;
ssl_certificate /etc/nginx/tls/demo.crt;
ssl_certificate_key /etc/nginx/tls/demo.key;
location = / {
default_type text/plain;
return 200 "hello from demo.example over $scheme\n";
}
location /app/ {
proxy_pass http://127.0.0.1:9080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
CONF
sudo nginx -tThis block only answers requests for demo.example, so it leaves your other sites alone. Apply it:
sudo systemctl reload nginxCheck it worked
sudo nginx -t ends with test is successful. Ask the app directly, without Nginx in the way:
curl -sS http://127.0.0.1:9080/app/ | grep 'app saw Host'It prints app saw Host: 127.0.0.1:9080 after a short pause.
Step 2 · DNS: turn the name into an address
A browser cannot connect to a name, only to an IP address. It asks a DNS resolver, usually run by the visitor's ISP, mobile network or a public service, which looks the name up (or answers from its cache) and returns the address in your A or AAAA record. To see this for a real public name, look one up with dig (from the bind9-dnsutils package):
dig +short nginx.orgIt prints one or more IP addresses. Those addresses are what the browser connects to next; the name itself is not used again until the TLS and HTTP phases.
Your lab name has no public DNS record. --resolve demo.example:443:127.0.0.1 tells curl the answer in advance, for that name and port only, without touching any system files. Time the lookup phase:
curl -sS -o /dev/null --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
-w 'dns lookup took %{time_namelookup}s\n' https://demo.example/Check it worked
The line reads something like dns lookup took 0.000040s: tens of microseconds, because curl had the answer already. Against a real site on a cold cache, this phase can take tens or hundreds of milliseconds. If it is the slow phase, the fix is in DNS (the resolver, the record's TTL), not on your web server. /tutorials/dns-records-explained-a-cname-mx-txt covers the records themselves.
Step 3 · TCP: open a connection
With an address in hand, the client opens a TCP connection to port 80 (HTTP) or 443 (HTTPS). That takes a three-way handshake: the client sends SYN, the server answers SYN-ACK, the client sends ACK. That is one full round trip before any data moves, so the time it takes is roughly the network delay between visitor and server.
Connect over plain HTTP and time it:
curl -sS --resolve demo.example:80:127.0.0.1 \
-w 'tcp connected at %{time_connect}s\n' http://demo.example/Now try a port where nothing listens:
curl -sS --resolve demo.example:8443:127.0.0.1 https://demo.example:8443/ || echo "curl exit code: $?"Check it worked
The first command prints hello from demo.example over http and a connect time well under a millisecond, since the server is on the same machine. The second prints something like curl: (7) Failed to connect to demo.example port 8443, then curl exit code: 7. Exit code 7 means curl could not open the TCP connection: nothing is listening, or a firewall rejected it. A firewall that silently drops packets makes curl wait and then fail with exit code 28. On a mobile network the round trip can be 100 ms or more, which is why the number of round trips matters so much for visitors on prepaid mobile data in many parts of Africa and elsewhere.
To see the SYN, SYN-ACK and ACK packets themselves, follow /tutorials/watch-a-tcp-connection-with-tcpdump. When a real server refuses connections, /tutorials/find-what-is-listening-on-a-port-with-ss shows what, if anything, is listening on each port.
Step 4 · TLS: agree on keys and check the certificate
On port 443 the client starts a TLS handshake straight after TCP connects. It sends the name it wants (demo.example, in a field called SNI) so the server can choose the right certificate. The server sends its certificate, both sides agree on keys, and the client checks that the certificate is signed by an authority it trusts and that it names demo.example. With TLS 1.3 this costs one more round trip.
Your certificate is self-signed, so no browser or curl trusts it by default. Watch the check fail:
curl -sS --resolve demo.example:443:127.0.0.1 https://demo.example/ || echo "curl exit code: $?"Now give curl your certificate as the one to trust, and look at what was agreed:
curl -sS -v -o /dev/null --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
-w 'tcp connected at %{time_connect}s, tls done at %{time_appconnect}s, verify result %{ssl_verify_result}\n' \
https://demo.example/ 2>&1 | grep -E 'SSL connection|subject:|tls done'Check it worked
The first command fails with SSL certificate problem: self-signed certificate and curl exit code: 60. Exit code 60 means TCP worked but the certificate check did not. The second shows a line like SSL connection using TLSv1.3 / ..., the subject CN=demo.example, and verify result 0, which means the check passed. On the same line, the tls done time is later than the tcp connected time by the length of the handshake. On a live site, a certificate from Let's Encrypt or another public authority passes this check with no --cacert; /tutorials/understand-tls-certificates-and-chains-with-openssl explains the chain behind it.
Step 5 · HTTP: send the request and read the response
Inside the encrypted connection, the client sends an HTTP request: a method and path (GET /), a Host header naming the site, and other headers. The server answers with a status line (200 OK, 404 Not Found), response headers and the body. In curl's verbose output, lines starting with > are what curl sent and lines starting with < are what came back:
curl -sS -v --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
https://demo.example/ 2>&1 | grep -E '^[<>]|hello from'Ask for a path the server does not have:
curl -sS -o /dev/null --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
-w '%{http_code}\n' https://demo.example/missing | grep -x 404Nginx writes one line per request to its access log:
sudo tail -n 3 /var/log/nginx/access.logCheck it worked
The verbose output shows > GET / HTTP/1.1 and > Host: demo.example, then < HTTP/1.1 200 OK, a < Server: nginx/1.30.8 header and the body hello from demo.example over https. The second command prints 404. The access log shows "GET /missing HTTP/1.1" 404. An HTTP error status means DNS, TCP and TLS all worked: the problem is in the server's configuration or the app, so that is where to look.
Real sites usually add http2 on; to the server block. HTTP/2 sends many requests over one connection at once, but the phases before it are the same. /tutorials/compare-http-1-1-http-2-and-http-3 shows the differences side by side.
Step 6 · Keep-alive: reuse the connection
DNS, TCP and TLS cost several round trips, and a page needs many files. Keep-alive leaves the connection open after a response, so the next request skips straight to the HTTP phase. Nginx keeps an idle connection open for 75 seconds by default (keepalive_timeout).
Fetch two URLs in one curl command, which lets curl reuse its connection:
curl -sS -o /dev/null -o /dev/null --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
-w '%{url_effective} new connections: %{num_connects}\n' \
https://demo.example/ https://demo.example/missing | tee ~/request-demo/reuse.txt
grep -q 'missing new connections: 0' ~/request-demo/reuse.txtCheck it worked
The first URL reports new connections: 1 and the second new connections: 0: the second request paid for no TCP or TLS handshake at all. Two separate curl commands would each report 1, as a browser would if your server closed connections early. If your pages feel slow on high-latency networks, check that keep-alive is on and that the page does not pull files from many different domains, each of which needs its own lookup and handshakes.
Step 7 · Add a proxy hop and time every phase
Most requests pass through more than one machine. A CDN edge near the visitor may end their TLS connection and answer from its cache. A load balancer picks one of several servers. A reverse proxy such as Nginx sits in front of the app. Each hop that ends TLS opens its own connection to the next one, and the visitor's address would be lost if the proxy did not pass it along in X-Forwarded-For.
Your lab has one hop: Nginx in front of the app. Ask for a page the app serves:
curl -sS --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
https://demo.example/app/ | tee ~/request-demo/app.txt
grep -q 'X-Forwarded-Proto: https' ~/request-demo/app.txtNow save a timing format and time the whole request. Every timer counts from the start, so each number includes the phases before it:
cat > ~/request-demo/timing.txt <<'TXT'
dns lookup done: %{time_namelookup}s\n
tcp connected: %{time_connect}s\n
tls done: %{time_appconnect}s\n
first byte arrived: %{time_starttransfer}s\n
total: %{time_total}s\n
TXT
curl -sS -o /dev/null --cacert ~/request-demo/demo.crt \
--resolve demo.example:443:127.0.0.1 \
-w @"$HOME/request-demo/timing.txt" https://demo.example/app/Check it worked
The app reports Host: demo.example, X-Forwarded-For: 127.0.0.1, X-Forwarded-Proto: https, and a connection from 127.0.0.1: Nginx's own connection, not yours. In the timing, DNS, TCP and TLS finish within a few milliseconds, then first byte arrived jumps to a little over 0.3s. That gap between tls done and first byte arrived is the time your server and app spent working. When a real site is slow, the same five numbers tell you which phase to fix; /tutorials/debug-a-slow-or-failing-website-with-curl goes further with them.
To build this hop for a real app, follow /tutorials/nginx-reverse-proxy-for-a-node-app; to spread requests over several app servers, see /tutorials/load-balance-with-nginx-upstream.
Step 8 · Clean up
Stop the app and remove everything the lab created. Check the paths before you run this: it deletes the ~/request-demo folder and the /etc/nginx/tls directory.
pkill -f 'request-demo/app.py' || true
sudo rm -f /etc/nginx/conf.d/demo.conf
sudo rm -rf /etc/nginx/tls
rm -rf ~/request-demo
sudo nginx -tsudo systemctl reload nginxCheck it worked
curl -sS -k --resolve demo.example:443:127.0.0.1 https://demo.example/ || echo "curl exit code: $?"curl prints curl exit code: followed by 7 or 35, because nothing serves HTTPS on port 443 any more. If another site of yours already listens on 443, you get that site's response instead, which is fine: demo.example itself is gone. Your other Nginx sites are unchanged.
Next steps
- Set up the real thing on a public server, starting with
/tutorials/install-nginx-on-ubuntu-26-04. - Learn the DNS records that start every request in
/tutorials/dns-records-explained-a-cname-mx-txt. - Use these timers to find the slow phase on a live site with
/tutorials/debug-a-slow-or-failing-website-with-curl. - See how certificates are checked, link by link, in
/tutorials/understand-tls-certificates-and-chains-with-openssl.
Related tutorials
- Write Nginx location blocks that match what you expectYou will build a practice Nginx 1.30 server where every location block labels its responses, predict which block answers each of twelve URLs, check your answers with curl, and fix the alias, regex-order and proxy_pass mistakes that catch most people out.
- Write a server firewall with nftablesYou will write one /etc/nftables.conf with a default-drop input chain, rate-limited SSH, named address sets, counters and logging, and load it over SSH behind a timer that undoes it if you lose your connection.
- Watch a TCP connection open and close with tcpdumpYou will capture a curl request to a local web server with tcpdump on Ubuntu 26.04, read its handshake, data and teardown line by line, see the reset a closed port sends, and save, filter and rotate captures as pcap files.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials