Last updated
What is nginx?
nginx (pronounced "engine-x") is a high-performance web server, reverse proxy, and load balancer. Originally created to solve the C10K problem (handling 10,000 concurrent connections), nginx has become one of the most popular web servers in the world, powering over 30% of all websites.
Unlike traditional web servers that create a new process or thread for each connection, nginx uses an asynchronous, event-driven architecture that can handle thousands of concurrent connections with minimal memory footprint. This makes it ideal for high-traffic websites, microservices architectures, and API gateways.
Common nginx Use Cases
- Reverse Proxy: Forward requests to backend application servers
- Load Balancer: Distribute traffic across multiple backend servers
- SSL Termination: Handle HTTPS encryption/decryption
- Static Content: Serve static files efficiently
- Caching: Cache responses to reduce backend load
- API Gateway: Route and manage API requests
How to Use the nginx Config Generator
Step 1: Select Configuration Type
Choose the type of nginx configuration you need. Reverse proxy is most common for forwarding requests to backend applications. SSL/TLS is for HTTPS setup. Load balancer distributes traffic across multiple servers.
Step 2: Configure Server Settings
Enter your domain name and the port nginx should listen on (typically 80 for HTTP or 443 for HTTPS). For the backend, specify where nginx should forward requests (e.g., localhost:3000 for a Node.js app).
Step 3: Generate and Deploy
Click "Generate Config" to create your configuration. Review the output, then download it. Place the file in /etc/nginx/sites-available/ on your server, create a symlink to sites-enabled, and reload nginx with `sudo nginx -s reload`.
Common Use Cases
1. Node.js Application Reverse Proxy
Use nginx as a reverse proxy in front of your Node.js application. nginx handles SSL termination, static files, and forwards dynamic requests to your Node.js server running on a local port.
2. Microservices API Gateway
Configure nginx to route requests to different microservices based on URL paths. This provides a single entry point for your microservices architecture while distributing requests to appropriate backend services.
3. High-Traffic Website Load Balancing
Distribute incoming traffic across multiple backend servers to handle high load. nginx can use round-robin, least connections, or IP hash algorithms to balance requests efficiently.
4. Static Website with CDN
Serve static HTML, CSS, and JavaScript files directly from nginx with optimal caching headers. nginx's efficient static file serving can handle thousands of concurrent connections with minimal resources.
5. WebSocket Proxy
Proxy WebSocket connections from clients to backend WebSocket servers. nginx handles the HTTP upgrade handshake and maintains long-lived connections efficiently.
nginx Configuration Examples
Example 1: Basic Reverse Proxy
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Example 2: SSL/TLS Configuration
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://localhost:3000;
}
}
Example 3: Load Balancer
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
Example 4: Static Files with Caching
server {
listen 80;
server_name example.com;
root /var/www/html;
location ~* \.(jpg|jpeg|png|gif|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
Example 5: WebSocket Proxy
location /ws {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}