Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 30 ยท First Steps

What a Web Server Does

Learn what a web server does: the HTTP request/response cycle, ports and sockets, static vs dynamic content and why Nginx or Apache sits in front of your app.

Every time you open a web page, your browser sends a short text message to another computer and gets a short text message back. The program on that other computer that listens for those messages is a web server. Nginx and Apache are the two most widely used ones, and almost every site you visit passes through one of them or through something built on the same ideas.

This course teaches you to configure both. Before touching a configuration file, it helps to understand the job itself: what arrives, what leaves, who does the work and why you would put a dedicated web server in front of your own application code. The real Nginx and Apache cannot run inside this page, so in every lesson the real commands and configuration appear in read-only blocks, and the live examples are small stand-ins written in JavaScript that reproduce the logic. In this lesson the stand-ins are real HTTP servers written with Node.js, so everything you see on the wire is genuine. Only the product is replaced.

The request and the response

HTTP is a conversation made of pairs: one request, one response. A request has a request line (method, target, protocol version), some headers and sometimes a body. A response has a status line, headers and a body.

HTTP
GET /blog/hello.html HTTP/1.1
Host: www.example.com
Accept: text/html

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 1456

<!doctype html> ...

Two details matter for everything that follows. First, the Host header says which site the client wants. One server can host hundreds of sites on the same address, and Host is how it tells them apart (you will configure this in Server Blocks and Choosing a Server). Second, the status code tells the client what happened: 2xx success, 3xx go somewhere else, 4xx you made a mistake, 5xx the server failed.

sequenceDiagram
    participant B as Browser
    participant W as Web server
    participant A as Application
    B->>W: GET /blog/hello.html (Host: www.example.com)
    alt a file exists for this path
        W->>W: read the file from disk
    else dynamic page
        W->>A: pass the request on
        A-->>W: generated HTML
    end
    W-->>B: 200 OK + headers + body

The web server answers from disk when it can and hands the request to an application when it must.

Our first stand-in is a complete HTTP server in a few lines of Node.js. It listens on the loopback address, and then the same program acts as the client with fetch. It prints the request line the server received and the response the client got:

Node.js A tiny HTTP server and its own client

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice req.url is only /hello?name=Ada: the path and query string. The host arrives separately in a header. A real web server reads the same fields before deciding what to do.

Ports, sockets and listening

A computer can run many network programs at once. An IP address finds the machine, and a port (a number from 1 to 65535) finds the program. A server binds a socket to an address and port and then listens. Browsers use default ports so you never type them: 80 for plain HTTP and 443 for HTTPS. So http://example.com/ really means http://example.com:80/.

On Linux, ports below 1024 are privileged, which is why a web server's first process usually starts as root, opens ports 80 and 443 and then hands the actual work to unprivileged worker processes. It is also why only one program can listen on a given address and port at a time. If you start a second server on port 80, it fails with "address already in use". Two different servers on the same machine, such as Nginx and Apache, need different ports (we use that on purpose in Nginx in Front of Apache). For more on sockets from the command line, see Networking Basics from the Shell.

The address you bind matters too:

Bind address Who can connect
127.0.0.1 (loopback) Only programs on the same machine
0.0.0.0 (all IPv4 interfaces) Anyone who can reach the machine
[::] (all IPv6 interfaces) Same, for IPv6
One specific address Only traffic arriving on that address

Our stand-ins always bind 127.0.0.1 and ask for port 0, which means "give me any free port". That is the safe way to run a demo server.

Static content and dynamic content

Most of what a site sends is static: files that already exist and are sent as they are, such as images, CSS, JavaScript, fonts, downloads and pre-built HTML. Serving a static file is a pure disk-to-network job. The operating system can even copy bytes from the file cache to the network socket without your program touching them (the sendfile mechanism you will meet in Static Files, Caching and MIME Types).

Dynamic content is produced for each request by running code: a PHP page, a Python view, a Node.js route. The code may query a database, check who is logged in and build a page that is different for each visitor. Dynamic responses cost far more CPU and memory than static ones.

A web server like Nginx or Apache is superb at the static part, and it knows how to forward the dynamic part to the application that understands it. That split is the most important idea in this course.

What a web server adds over your application

Frameworks such as Express, Flask or Laravel can listen on a port themselves, so why not expose them directly? You can, for development. In production, a web server in front earns its place:

What sits in front of your app

One front door Ports 80 and 443 for many sites and apps.
TLS Certificates and HTTPS handled in one place.
Static files Served fast, without waking the app.
Protection Limits, timeouts and slow-client buffering.
1 / 2

The second stand-in builds the pattern in miniature. An "app server" produces dynamic JSON and counts every request it receives. A "front server" answers from a small table of static files, forwards /api/ requests to the app, and returns 404 for everything else. At the end we ask the app how many requests actually reached it:

Node.js A front server in front of an app server

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Five requests came in and only two of them cost the application anything. Multiply that by millions of image and CSS requests and you see why the split exists. This stand-in is honest about its limits: real Nginx or Apache also streams large files from disk, keeps connections alive, handles thousands of connections at once and speaks TLS, none of which this 40-line program does.

Server processes and workers

A real web server is more than one loop. Typically a master (or parent) process reads the configuration, opens the listening sockets and supervises several worker processes that accept connections and serve requests. Reloading the configuration means starting new workers that read the new settings while the old ones finish their current requests. How workers are organized is the main difference between Nginx and Apache, covered in Nginx vs Apache: Architecture and Choosing.

Where the configuration comes in

Everything in this course answers questions like these, in a configuration language you will learn:

  • Which address and port do I listen on, and for which host names?
  • Which directory holds the files for this site, and which file is the index?
  • Which URLs go to the application and which are static files?
  • Which headers do I add, which requests do I refuse or rate-limit?
  • Where do the logs go, and how do I reload safely?
NGINX
server {
    listen 80;
    server_name www.example.com;
    root /var/www/example;

    location /api/ {
        proxy_pass http://127.0.0.1:3000;
    }
}

That is a real Nginx fragment (read-only here): a site on port 80 that serves files from /var/www/example and forwards /api/ to an app on port 3000. It is the same shape as our second stand-in. By the end of the course you will read and write configurations like this with confidence.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which part of an HTTP request tells a server that hosts many sites which one the client wants?

    The Host header carries the site name. Many sites can share one address and port, and the server uses Host to choose between them.
  2. 2. Why can't two programs listen on the same address and port at once?

    A listening socket owns its address and port. A second bind fails with 'address already in use', so servers on one machine need different ports or addresses.
  3. 3. Which request is the best fit for the web server itself rather than the application?

    A static file needs no code to run. The web server can send it straight from disk, so the application never has to wake up.
  4. 4. What does binding a demo server to 127.0.0.1 mean?

    127.0.0.1 is the loopback address. Traffic to it never leaves the machine, which makes it the safe choice for local experiments and for backends behind a proxy.

Summary

  • A web server reads HTTP requests (method, path, headers, optional body) and writes responses (status, headers, body).
  • A server binds a socket to an address and port; 80 is the HTTP default and 443 the HTTPS default, and only one program can own an address and port.
  • Static files are served straight from disk; dynamic pages come from application code, which the web server forwards requests to.
  • A web server in front adds TLS, one front door for many sites, fast static serving, protection, load balancing, logging and caching.
  • Real servers use a master process and workers; how those are organized differs between Nginx and Apache.
  • In this course the real products' commands and config are read-only, while runnable stand-ins show how the logic works.

Next: Installing and Finding Your Way Around