What is Nginx?
What is Nginx?
Nginx is an open-source web server created in 2004 to solve the C10K problem — handling 10,000 concurrent connections efficiently. Where older servers like Apache spawned a new thread or process per request, Nginx uses an event-driven, non-blocking architecture that handles thousands of connections in a single thread.
This makes Nginx extremely fast and memory-efficient — exactly what you need in front of a production app.
Two Roles Nginx Plays
1. Web Server (Static File Server)
Nginx can serve static files (HTML, CSS, JS, images) directly from disk — fast, with no application code involved.
Request: GET /logo.png
Nginx reads the file from /var/www/html/logo.png and sends it directly.
This is how you'd serve a React build or a static site.
2. Reverse Proxy
A reverse proxy accepts requests from clients and forwards them to a backend server (your Node.js, Python, or any other app). The client never talks to your app directly.
Client → Nginx (port 80/443) → Node.js app (port 3000, localhost only)
Benefits of proxying through Nginx:
- Your app doesn't need to handle SSL
- Nginx can serve static files itself (fast), only sending dynamic requests to Node
- You can run multiple apps on one server, routing by domain name
- Nginx adds a layer of protection — your app's port isn't exposed to the internet
Nginx vs. Apache
| Nginx | Apache | |
|---|---|---|
| Architecture | Event-driven, async | Thread/process-per-request |
| Concurrency | Excellent | Good |
| Memory usage | Low | Higher under load |
| Config style | Declarative blocks | Directive-based (.htaccess) |
| Static files | Very fast | Fast |
| Dynamic content | Proxies to app server | Can run PHP natively (mod_php) |
For modern Node.js or Python backends, Nginx is the standard choice.
Configuration Structure
Nginx config lives at /etc/nginx/nginx.conf, but in practice you put site-specific config in /etc/nginx/sites-available/ and symlink it to /etc/nginx/sites-enabled/.
The core building block is the server block — equivalent to a virtual host:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
This configuration:
- Listens on port 80
- Accepts requests for
example.com - Forwards all traffic to a Node.js app on port 3000
Installing Nginx
On Ubuntu/Debian:
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx # start on boot
Verify it's running by visiting your server's IP in a browser — you should see the Nginx welcome page.
Key commands:
sudo nginx -t # test config for syntax errors
sudo systemctl reload nginx # apply config changes without dropping connections
sudo systemctl restart nginx # full restart
sudo tail -f /var/log/nginx/error.log # watch error logs