Nginx Configuration Guide: Reverse Proxy, SSL and Load Balancing
Configure Nginx as a reverse proxy, SSL termination, and load balancer. Learn server blocks, upstream definitions, and security settings in detail.
Ahmet Yılmaz
Senior Infrastructure Engineer
Over 34 percent of web traffic passes through Nginx, and this share grows every year. Nginx configuration knowledge is a cornerstone of modern web infrastructure management. This guide covers configuring Nginx as a reverse proxy, SSL termination point, and load balancer, along with server block management and security best practices.
What Is Nginx and Why Choose It?
Nginx (pronounced engine-x) is a high-performance web server and reverse proxy developed by Igor Sysoev in 2004. Its event-driven, asynchronous architecture handles tens of thousands of concurrent connections with low memory usage. Compared to Apache's thread-per-connection model, Nginx is significantly more efficient for static file serving and reverse proxy scenarios.
Nginx serves four main roles: web server (static file serving), reverse proxy (in front of application servers), load balancer (traffic distribution), and SSL/TLS termination point. Most production environments use it for multiple roles simultaneously.
💡 Tip: Always run nginx -t before applying configuration changes. This command detects errors in configuration files without restarting the server.
Server Block Configuration
Server blocks (equivalent to Apache's virtual hosts) let you host multiple websites on the same server. Each server block uses the server_name directive to specify which domain it responds to.
| Directive | Description | Example Value |
|---|---|---|
| listen | Port to listen on | 443 ssl http2 |
| server_name | Domain name | example.com www.example.com |
| root | Document root directory | /var/www/example |
| proxy_pass | Reverse proxy target | http://localhost:3000 |
| ssl_certificate | SSL certificate path | /etc/letsencrypt/live/... |
Reverse Proxy Configuration
A reverse proxy forwards client requests to backend application servers. Instead of exposing Node.js, Python, PHP-FPM, or Java applications directly to the internet, place them behind Nginx for security, performance, and flexibility gains.
For WebSocket connections, add proxy_http_version 1.1 and Upgrade headers. Proxy buffer settings affect performance with large responses. Check our Node.js production guide for detailed reverse proxy examples.
⚠️ Important: Always include proxy_set_header X-Real-IP $remote_addr and X-Forwarded-For headers in your reverse proxy configuration. Otherwise, your application server sees all requests as coming from 127.0.0.1.
SSL/TLS Configuration with Let's Encrypt
HTTPS is no longer optional — Google marks non-HTTPS sites as insecure. Get free SSL certificates with Let's Encrypt and configure automatic renewal with Certbot. Enable TLS 1.2 and 1.3 protocols and disable older versions. HSTS headers ensure browsers always connect via HTTPS. Our SSL/TLS certificate guide covers certificate types in detail.
Load Balancing for Traffic Distribution
Nginx's upstream block distributes traffic across multiple backend servers. Algorithm options include round-robin (default), least connections, IP hash, and random. Health check mechanisms automatically remove failed servers from the pool.
For session-persistent applications, use ip_hash to route the same user to the same backend. A better approach is storing session data in a centralized store like Redis. The Nginx load balancing documentation explains all algorithms in detail.
Frequently Asked Questions
Should I use Nginx or Apache?
Nginx excels at static file serving, reverse proxy, and high concurrent connection scenarios. Apache offers advantages with .htaccess support and its module ecosystem, especially in shared hosting environments. Modern projects generally prefer Nginx.
Do configuration changes require a server restart?
The nginx -s reload command reloads configuration without downtime. Existing connections are unaffected, and new connections use the updated configuration. A full restart (systemctl restart nginx) is rarely needed.
How many websites can Nginx host?
Nginx itself supports thousands of server blocks. The limiting factor is your server's RAM and CPU capacity. You can host hundreds of sites on a single Nginx instance by defining separate server blocks for each.
After mastering Nginx configuration, check our Linux command line guide and Docker Compose article.
Nginx-Optimized Cloud Servers
Hosted Cloud servers come with Nginx pre-configured. Set up your high-performance web infrastructure right away.
Explore Cloud Server PlansAhmet Yılmaz
Senior Infrastructure Engineer
With over 10 years of experience in cloud infrastructure and DevOps, Ahmet specializes in Kubernetes, Terraform, and high-availability architectures.
Comments coming soon