Lesson 3 of 10 - Foundations: the server, the network and Git
How a request reaches your server
By Branko Petric - Updated - About 8 min - Free
The short answer
A request finds your server in steps. DNS turns a name into an IP address, the client connects to a port on that IP, and the server answers with an HTTP status code. When a site breaks, check each step in order with dig, curl -v and ss -tlnp to see where it stops.
Already know this? Skip to the 3 questionsIP addresses and ports
Every machine on a network has an IP address, such as 203.0.113.10. Think of it as the street address of a building. A port is the apartment number. One server can run many programs, and each one listens on its own port.
Some ports are standard:
22for SSH80for HTTP443for HTTPS5432for PostgreSQL
So 203.0.113.10:443 means "the program listening on port 443 at that address". If nothing listens there, the connection is refused.
127.0.0.1, also called localhost, always means "this machine". An app bound to 127.0.0.1:8080 can only be reached from the same server. An app bound to 0.0.0.0:8080 accepts connections on every network interface.
DNS: names to addresses
People type shop.example.com, not an IP. DNS (the Domain Name System) turns the name into an address. The most common record is an A record, which maps a name to an IPv4 address. A CNAME points one name at another name.
dig +short shop.example.com
# 203.0.113.10
If dig returns nothing or the wrong IP, the problem is DNS, and nothing on your server will fix it. DNS answers are also cached for a set time (the TTL), so a change can take a while to reach everyone.
HTTP status codes
Once connected, the client sends an HTTP request and the server answers with a status code. Learn the families, not every number:
2xxsuccess.200 OK.3xxredirect.301and302send you to another URL.4xxthe client asked for something wrong.401not logged in,403not allowed,404not found.5xxthe server failed.500the app crashed on this request,502the proxy got no valid answer from the app,504the app answered too slowly.
The family tells you where to look. A 4xx usually means the request or the permissions. A 5xx means something on the server side.
Tracing a request, step by step
Say users report that shop.example.com is broken. Walk the path in order.
# 1. Does the name point at the right server?
dig +short shop.example.com
# 2. What happens on the wire? -v shows the connection and headers
curl -v https://shop.example.com/
# * Connected to shop.example.com (203.0.113.10) port 443
# < HTTP/1.1 502 Bad Gateway
# < Server: nginx
# 3. On the server: what is listening, and which process?
sudo ss -tlnp
# LISTEN 0 511 0.0.0.0:443 0.0.0.0:* users:(("nginx",pid=812))
curl -v connected and got a reply from nginx, so DNS and port 443 are fine. The 502 says nginx could not get an answer from the app. ss -tlnp shows TCP (-t) listening sockets (-l) with numeric ports (-n) and the process (-p). Here nginx is on 443, but nothing is listening on 8080, where nginx sends traffic. The app is down. Next stop: systemctl status and journalctl for the app.
Each step either passes or tells you exactly where the request stopped.
Common beginner mistakes
- Treating every error as "the network". A
502or500is an HTTP answer, so the network worked. Read the status code before blaming DNS or the firewall. - Binding the app to 127.0.0.1 and testing from your laptop. It works on the server and fails from outside. Check the address in
ss -tlnp. - Changing DNS and expecting it to update at once. Old answers are cached until the TTL runs out. Check with
digbefore and after.
Check your understanding
3 questions. Retry as often as you like - nothing counts against you.
Question 1 of 3
Your app should listen on port 8080, but curl localhost:8080 says 'Connection refused'. What should you check?
Select all that apply - one or more answers can be right.
Want the bigger picture? This lesson maps to this step of the DevOps roadmap.