Web Server Examples

Working configurations for real-world patterns

Learn by Example

These examples demonstrate practical web server configurations you can study, copy, and adapt for your projects. Each example includes configurations for multiple server platforms when applicable.

Configuration examples show Nginx and Apache syntax side-by-side. Code examples use Node.js for programmatic server implementation.

Virtual Hosts Configuration

Single Site

Basic configuration for serving a single website.

Nginx Apache

Multiple Sites

Serve multiple domains from a single server.

virtual hosts server blocks

Subdomains

Handle subdomains with wildcard and specific matches.

wildcard server_name

Reverse Proxy Configuration

Basic Reverse Proxy

Forward requests to a backend application server.

proxy_pass ProxyPass

Load Balancing

Distribute traffic across multiple backend servers.

upstream round-robin

WebSocket Proxy

Proxy WebSocket connections with proper upgrade handling.

Upgrade Connection

Header Forwarding

Pass client information to backend servers.

X-Forwarded-For X-Real-IP

TLS/HTTPS Configuration

Basic HTTPS

Enable TLS with certificate and private key.

ssl_certificate SSLEngine

HTTP to HTTPS Redirect

Force all traffic to use HTTPS.

301 redirect HSTS

Let's Encrypt Setup

Free TLS certificates with automatic renewal.

Certbot ACME

Modern TLS Config

Secure cipher suites and protocol configuration.

TLS 1.3 cipher suites

Static File Serving Configuration

Basic Static Serving

Serve files from a directory with proper MIME types.

root DocumentRoot

Caching Headers

Configure Cache-Control and ETag for browser caching.

Cache-Control expires

Compression

Enable gzip and Brotli compression for text files.

gzip brotli

SPA Fallback

Route all requests to index.html for single-page apps.

try_files FallbackResource

Node.js Servers Code

Basic HTTP Server

Minimal Node.js server with request handling.

http.createServer

Static File Server

Serve files with streams, MIME types, and caching.

fs.createReadStream

Request Routing

Route by method and path without frameworks.

URL parsing routing

Graceful Shutdown

Handle process signals and drain connections properly.

SIGTERM server.close