Nginx (pronounced “engine-x”) is a high-performance web server, reverse proxy, and load balancer known for its low memory footprint and ability to handle tens of thousands of concurrent connections. Created by Igor Sysoev in 2004, Nginx powers a large portion of the busiest websites on the internet.
What Nginx does
Nginx serves multiple roles, often simultaneously:
- Web server: Serves static files (HTML, CSS, JS, images) directly with minimal overhead.
- Reverse proxy: Sits in front of application servers (Node.js, Python, Ruby), forwarding requests and returning responses. This lets you handle TLS termination, caching, and load balancing at the proxy layer.
- Load balancer: Distributes traffic across multiple upstream servers using round-robin, least connections, IP hash, or other algorithms.
- TLS termination: Handles HTTPS encryption/decryption so your application servers don’t have to.
Architecture
Apache creates a thread or process per connection. Nginx uses an event-driven, asynchronous architecture with worker processes, each handling thousands of connections in a single thread using epoll (Linux) or kqueue (BSD/macOS). This is why Nginx excels at serving many simultaneous connections with minimal memory.
Basic configuration
Nginx configuration uses a block-based syntax in /etc/nginx/nginx.conf:
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
location /api/ {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
This serves static files from the root and proxies /api/ requests to a backend application.
Common use cases
- Static site hosting: Serve an Astro, Next.js, or Hugo build directly
- API gateway: Route
/api/v1/to one service,/api/v2/to another - SSL front-end: Terminate HTTPS at Nginx, proxy plain HTTP to internal services
- Rate limiting: Protect backend services from traffic spikes with
limit_req - Caching: Cache upstream responses to reduce backend load
- WebSocket proxy: Proxy persistent WebSocket connections to application servers
Nginx vs. Apache
Apache is more flexible with .htaccess files (per-directory config) and dynamic module loading. Nginx is faster for high-concurrency workloads and static file serving. Many setups use Nginx as a reverse proxy in front of Apache, combining both strengths.
Tips
- Always test configuration changes with
nginx -tbefore reloading - Use
includedirectives to organize configs into separate files - Set proper
worker_processes(usuallyautoto match CPU cores) - Enable gzip compression for text-based responses
Generate Nginx configurations with the Nginx Config Generator or check SSL certificate details with the SSL Certificate Decoder.