Nginx
High-performance web server, reverse proxy, and load balancer. Handles many concurrent connections efficiently using asynchronous event-driven architecture. Designed to solve the C10K problem and is the standard choice for Docker environments.
Web Server
Nginx
Overview
Nginx is a high-performance web server, reverse proxy, and load balancer that employs an asynchronous event-driven architecture. Developed by Igor Sysoev from Russia, it was designed to solve the C10K problem (efficiently handling 10,000 concurrent connections). Despite being lightweight, it boasts high processing performance and is adopted by numerous websites worldwide.
Details
Nginx was first released in 2004 and quickly spread as a high-performance alternative to Apache HTTP Server. Currently, it holds a 33.8% share of the global web server market, maintaining the top position.
Key Technical Features
- Asynchronous Event-Driven Architecture: Handles massive concurrent connections with minimal memory usage
- Master-Worker Process Model: Balances stability and efficiency
- Modular Design: Only necessary features can be incorporated
- HTTP/3 Support: Supports the latest HTTP protocol for further acceleration
Use Cases
- Static content delivery
- Reverse proxy server
- Load balancer
- API gateway
- Proxy for microservices environments
Advantages and Disadvantages
Advantages
- High Performance: Efficiently handles massive concurrent connections
- Lightweight: Operates with minimal memory usage
- Simple Configuration: Intuitive and easy-to-understand configuration files
- Rich Features: Single solution for web server, proxy, and load balancer
- High Stability: Suitable for long-term continuous operation
- Active Community: Abundant documentation and support
Disadvantages
- Dynamic Content Processing: External processes required for PHP and other dynamic content
- Module Availability: Not as rich as Apache for some specialized features
- Learning Curve: Apache users need to learn different configuration methods
Reference Pages
Configuration Examples
Basic Web Server Configuration
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
SSL/TLS HTTPS Configuration
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
root /var/www/html;
index index.html;
}
}
Reverse Proxy Configuration
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Load Balancing Configuration
upstream web_servers {
least_conn;
server web1.example.com:80 weight=3;
server web2.example.com:80 weight=2;
server web3.example.com:80 weight=1;
}
server {
listen 80;
server_name loadbalancer.example.com;
location / {
proxy_pass http://web_servers;
}
}
Caching Configuration
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive=60m use_temp_path=off;
server {
listen 80;
server_name cache.example.com;
location / {
proxy_cache my_cache;
proxy_pass http://backend;
proxy_cache_valid 200 10m;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
}
}
HTTP/2 Configuration
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
location / {
root /var/www/html;
}
}