Every website you visit, every API you call, and every file you download from the internet is served by a web server. Understanding how web servers work is foundational knowledge for any backend developer or systems engineer.
What is a Web Server?
A web server is software that:
- Listens for incoming network connections on a port (usually port 80 for HTTP, 443 for HTTPS)
- Accepts requests using the HTTP protocol
- Delivers the requested content (HTML, CSS, JavaScript, images, API responses) back to the client
+----------------+ HTTP Request +------------------+
| | -----------------------------> | |
| Client | | Web Server |
| (Browser) | <----------------------------- | |
+----------------+ HTTP Response +------------------+
|
v
+------------------+
| File System / |
| Application |
+------------------+
The client (browser, mobile app, or another service) sends a request, and the web server reads the request, retrieves the appropriate content, and sends back a response.
The HTTP Protocol
The HTTP (HyperText Transfer Protocol) is the foundation of data communication on the web. It defines how requests and responses are formatted.
HTTP Request Structure
GET /picture.jpg HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: image/jpeg
HTTP Response Structure
HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: 34512
[binary image data...]
Common HTTP Methods
| Method | Description |
|---|---|
| GET | Retrieve a resource |
| POST | Submit data to be processed |
| PUT | Replace a resource completely |
| PATCH | Partially update a resource |
| DELETE | Remove a resource |
Common HTTP Status Codes
| Code | Meaning |
|---|---|
200 OK | Request succeeded |
301 Moved Permanently | Resource has been permanently moved |
302 Found | Temporary redirect |
400 Bad Request | Client sent a malformed request |
401 Unauthorized | Authentication required |
403 Forbidden | Access denied |
404 Not Found | Resource does not exist |
500 Internal Server Error | Server-side error |
The Root Directory
A web server delivers files from a configured root directory (also called the document root). This is the folder on the server's file system that maps to the root URL (/).
URL Request: http://www.example.com/picture.jpg
|
v
Server maps to: /var/www/html/picture.jpg
Default Root Directories
| Web Server | Default Root Directory (Linux) | Windows |
|---|---|---|
| Apache | /var/www/html | C:\Apache24\htdocs |
| Nginx | /var/www/html | — |
| IIS | — | C:\inetpub\wwwroot |
The root directory is configurable in the web server's configuration files. You can change it to any path on the file system.
Virtual Hosts
A single web server can host multiple websites with different domain names using a feature called Virtual Hosts.
When a request arrives, the web server reads the Host header from the HTTP request and compares it against its virtual host configurations. It then serves files from the matching virtual host's root directory.
HTTP Request 1: Host: one.com
--> Web Server checks virtual hosts
--> Matches one.com
--> Serves files from /var/www/website_one
HTTP Request 2: Host: two.com
--> Web Server checks virtual hosts
--> Matches two.com
--> Serves files from /var/www/website_two
HTTP Request 3: Host: unknown.com
--> Web Server checks virtual hosts
--> No match found
--> Serves the default website
Apache Virtual Host Configuration Example
<VirtualHost *:80>
ServerName one.com
DocumentRoot /var/www/website_one
ErrorLog /var/log/apache2/one.com-error.log
</VirtualHost>
<VirtualHost *:80>
ServerName two.com
DocumentRoot /var/www/website_two
ErrorLog /var/log/apache2/two.com-error.log
</VirtualHost>
Nginx Server Block Example
server {
listen 80;
server_name one.com;
root /var/www/website_one;
}
server {
listen 80;
server_name two.com;
root /var/www/website_two;
}
There is no limit to the number of virtual hosts you can configure on a single server.
Common Web Server Software
| Software | Description | Best For |
|---|---|---|
| Apache HTTP Server | The most widely deployed web server, highly configurable via .htaccess | Shared hosting, PHP applications |
| Nginx | High-performance, event-driven architecture, excellent as reverse proxy | High-traffic sites, reverse proxy, load balancing |
| IIS (Internet Information Services) | Microsoft's web server, integrated with Windows Server | Windows/.NET environments |
| Node.js (http module) | JavaScript-based HTTP server, often used with Express | Node.js applications, APIs |
| Caddy | Modern server with automatic HTTPS | Simple deployments, development |
Static vs Dynamic Content
Web servers handle two types of content differently:
+--------------------------------------------------+
| Web Server |
| |
| Static Content Dynamic Content |
| (served directly) (processed first) |
| |
| HTML files PHP scripts |
| CSS files Python (WSGI/ASGI) |
| Images Node.js applications |
| JavaScript files Ruby on Rails |
+--------------------------------------------------+
Static content is served directly from the file system with no processing.
Dynamic content is processed by an application server (PHP-FPM, Gunicorn, Node.js) and the web server acts as a reverse proxy, forwarding requests to the application and relaying responses back to the client.
Reverse Proxy Architecture
In production environments, web servers like Nginx are often used as reverse proxies in front of application servers:
Client
|
v
Nginx (reverse proxy)
|
+-- /api/* --> Node.js app on port 3000
|
+-- /static --> /var/www/static (static files)
|
+-- /* --> Python/Django app on port 8000
Benefits of this architecture:
- SSL termination: Nginx handles HTTPS, backend speaks plain HTTP
- Load balancing: distribute requests across multiple app instances
- Caching: serve cached responses without hitting the application
- Security: hide backend services from direct internet exposure
- Compression: gzip compress responses before sending
HTTPS and SSL/TLS
Modern web servers must serve content over HTTPS (HTTP Secure). HTTPS encrypts the connection between client and server using TLS (Transport Layer Security).
Client Web Server
| |
| TLS Handshake |
| ---------------------------------> |
| <--------------------------------- |
| Certificate + Public Key |
| |
| Encrypted Communication |
| <================================> |
To enable HTTPS you need:
- A TLS certificate (from a Certificate Authority like Let's Encrypt)
- Configure the web server to use port 443 and load the certificate
# Nginx HTTPS configuration
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/html;
}
Web Server Performance Concepts
| Concept | Description |
|---|---|
| Concurrency | How many simultaneous connections the server can handle |
| Throughput | Requests per second the server can process |
| Latency | Time from request to first byte of response |
| Caching | Storing responses to avoid repeated processing |
| Compression | Reducing response size with gzip or Brotli |
| Keep-Alive | Reusing TCP connections across multiple requests |
Final Thoughts
Web servers are the entry point for all web traffic. Whether you are serving static files, running a dynamic application, or building a microservices architecture, the web server is the component that listens, routes, and responds to every single request.
Understanding how root directories, virtual hosts, reverse proxies, and HTTPS work gives you the foundation to configure, optimize, and troubleshoot any web server environment.
The web server is the front door of everything you build.