What Happens When a Web Server Receives a Request
You type an address into your browser, press Enter, and a page appears a moment later. In that moment, a surprising amount of coordinated work takes place across several machines and software layers. Understanding the journey helps when something breaks, because you can ask which step failed.
This walkthrough follows a single request from the browser to the server and back, with a focus on what the server does once the request arrives.
Step One: Finding the Server
Browsers and servers speak in numeric addresses, but people use names. Before anything can be sent, the browser needs to translate the name into an IP address. This is the job of the Domain Name System, or DNS.
The browser first checks its own cache, then asks the operating system, which asks a resolver, usually run by your network provider or a public service. The resolver may in turn ask other servers until it finds the answer. Results are cached for a period set by the domain owner, so most lookups are quick.
Step Two: Opening a Connection
With an address in hand, the browser opens a TCP connection to the server, normally on port 80 for plain HTTP or port 443 for HTTPS. TCP sets up a reliable channel through a short exchange of messages known as a handshake. It guarantees that data arrives in order and that lost pieces are resent.
For HTTPS, a TLS handshake follows. The server presents a certificate to prove its identity, and the two sides agree on encryption keys. After this, everything they exchange is encrypted, so anyone watching the network sees only scrambled bytes.
Newer versions of HTTP can run over QUIC, which combines connection setup and encryption to save time, but the concepts are the same.
Step Three: Sending the Request
The browser now sends an HTTP request. It is plain structured text, or a binary equivalent in newer versions, with a few parts.
GET /articles/hello HTTP/1.1
Host: example.com
Accept: text/html
Cookie: session=abc123
The first line gives the method, the path and the protocol version. Headers follow, carrying details such as the host name, accepted content types and cookies. For methods like POST, a body comes after the headers, containing form data or JSON.
Step Four: The Server Accepts and Parses
On the server machine, a program is listening on the port. When data arrives, the operating system hands the new connection to that program. This might be a general-purpose web server, a reverse proxy or the application server itself.
The software reads the bytes, checks that they form a valid HTTP request and splits them into method, path, headers and body. Malformed or oversized requests are rejected here with an error such as 400.
Often a front-facing web server handles the connection details and passes the request along to application code, either by forwarding it over the network or through a local socket. This lets the front layer deal with encryption, compression and static files while the application focuses on logic.
Step Five: Routing to the Right Code
Next, the server decides who should handle the request. It might match the path against a set of rules. A path ending in .css or .png is served directly from disk. A path like /articles/hello is passed to the application, where a router maps it to a specific function.
Along the way, middleware may run. These are small steps applied to every request, such as logging, checking a session cookie, enforcing limits on request rates or decoding a JSON body.
Step Six: Doing the Work
Now the application does the real job. A handler might read a parameter from the URL, query a database for the matching record, call another internal service, and combine the results with a template to produce HTML or JSON.
This stage is usually where most of the time goes. Slow database queries, calls to remote services and heavy computation all show up here, which is why profiling usually starts with the handler.
Step Seven: Building the Response
The application returns a response made of a status code, headers and a body.
- Status code: 200 means success, 301 or 302 mean redirect, 404 means not found, and 500 means the server hit an error.
- Headers: content type, length, caching rules and any cookies to set.
- Body: the HTML page, JSON data or file contents.
The web server may compress the body, add security headers and write a line to the access log recording the request path, status and duration.
Step Eight: Delivery and Cleanup
The response travels back through the same encrypted connection. The browser reads the headers, decodes the body and begins rendering. If the page references images, scripts or stylesheets, the browser fires off more requests, often reusing the existing connection to avoid repeating the handshake.
The server may keep the connection open for a short time, expecting further requests, and then close it. Resources tied to the request, such as memory and database handles, are released.
Where Things Go Wrong
Knowing the steps maps symptoms to causes.
- A "site cannot be reached" error often points to DNS or a refused connection.
- A certificate warning points to the TLS step.
- A 404 suggests routing found nothing to match.
- A 500 means the application code failed.
- A request that hangs usually means slow work in the handler, such as a stuck query.
Frequently Asked Questions
What is the difference between a web server and an application server?
A web server mainly handles connections, static files and forwarding, while an application server runs your program code. In practice the roles overlap, and many setups place a web server in front of an application server.
Why does HTTPS add a delay at the start?
The TLS handshake requires a few extra messages to verify the certificate and agree on keys. Reusing connections and newer protocol versions reduce this cost, so it is usually small after the first request.
What does a status code of 500 mean?
It means the server understood the request but failed while processing it, typically because of a bug or an unavailable dependency. The server logs usually contain the details that the browser does not show.