🚀 What is Nginx?
Nginx (enginex) is an open-source software that serves as a high-performance web server, reverse proxy, load balancer, HTTP cache, and more. It was created in 2004 by Russian developer Igor Shishov to solve the C10K problem (handling 10,000 simultaneous connections) and has since become a core infrastructure, serving over 40% of the world’s web traffic.
—.
⚙️ Key Features & Benefits
-
Event-driven asynchronous architecture.
- How it works:
graph LR A[Client Request] --> B[Master Process] B --> C[Worker Process 1] B --> D[Worker Process 2] C --> E[Non-blocking I/O] D --> F[Non-blocking I/O]
- Instead of per-thread/per-process connections, a single process handles thousands of connections in a non-blocking manner → high concurrency with fewer resources.
- More efficient than Apache’s prefork MPM with 1/5 memory and more than 2x processing speed.
- How it works:
-
Multi-purpose middleware.
- Web Server: Super fast delivery of static files (HTML, images).
- Reverse proxy**:
location / { proxy_pass http://backend_server; # Forward requests to WAS (e.g., Node.js) }
- load balancer:
upstream myapp { server 10.0.0.1 weight=3; # weighted server 10.0.0.2; }
- End SSL/TLS: decrypt HTTPS and send plaintext to the backend.
-
Dynamic module system
- Allows selective loading of only the required functionality (e.g.
nginx-module-image-filter
).
- Allows selective loading of only the required functionality (e.g.
—.
🛠️ Main use cases
Uses | Description | Example Configuration Code |
---|---|---|
Serves static content | Serves images, CSS, JS | root /var/www/html; |
API Gateway | Microservice routing | location /api { proxy_pass http://api_servers; } |
Load balancing | Distribute traffic across multiple servers | upstream { least_conn; server app1:8080; } |
DDoS Defense | Limit the number of requests | limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s; |
—]
📝 Basic setup guide (based on Ubuntu)
-
**Install
sudo apt update sudo apt install nginx
-
**Core configuration file structure
/etc/nginx/nginx.conf
: Main configuration/etc/nginx/sites-available/
: Virtual host templates/etc/nginx/sites-enabled/
: Enabled hosts (symlink)
-
Simple Static Server Example.
server { listen 80; server_name example.com; root /var/www/example; # Static file paths location / { index index.html; } location ~* \.(jpg|png)$ { # image caching expires 30d; } }
-
Validate settings & restart **bash
sudo nginx -t # Test sudo systemctl restart nginx
—]
🔍 Utilizing Advanced Features
-
accelerate caching
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m; location / { proxy_cache my_cache; proxy_pass http://backend; }
- Store frequently accessed content in memory/disk to speed up response ⬆️
-
WebSocket proxy.
location /chat/ { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://websocket_backend; }
-
add_header X-Frame-Options DENY; # prevent clickjacking ssl_protocols TLSv1.2 TLSv1.3; # Block outdated protocols
—]
💡 Nginx vs Apache
Topics | Nginx | Apache | |
---|---|---|---|
Connection Model | Asynchronous Event-based | Thread/Process-based | |
Static File Performance | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
Dynamic content | Dependent on external processes (e.g. PHP-FPM) | Module embedded processing | |
Configuration files | Declarative structure | .htaccess complexity |
—.
🎯 Bottom line: Why Nginx?
- Fast and lightweight: Reduce hardware costs ⬇️
- Flexible: Optimized for microservices, cloud environments ☁️
- Secure: Resistant to high volumes of traffic and security threats 🛡️
> “Nginx is the hidden backbone of modern web infrastructure. It’s more than just a web server, it’s the core engine responsible for the performance and reliability of the entire system.”
Even beginners can get started by modifying nginx.conf
line by line! 🚀
➜ Official documentation | ➜ Configuration generator