Stop Wasting RAM: The Nginx Reverse Proxy Architecture
It is 2009, and yet I still see sysadmins provisioning massive dedicated servers just to serve static images. It’s burning money. If you are running a high-traffic site—maybe a busy vBulletin forum or a heavy Magento store—relying solely on Apache with mod_php is a suicide pact for your available memory.
We've all seen the graph. Traffic spikes (the Slashdot effect), Apache spawns hundreds of child processes, each consuming 20-50MB of RAM, and suddenly your server hits swap. The load average climbs to 50. The site dies.
The solution isn't more hardware. It's smarter architecture. By placing Nginx (Engine-X) in front of Apache, you can handle thousands of concurrent connections with a tiny memory footprint. This setup is standard on all managed tiers at CoolVDS, but if you are managing your own root VPS, you need to configure this yourself.
The Architecture: Nginx + Apache
Apache is excellent at processing dynamic content (PHP, Python, Perl). It is terrible at holding open connections for slow clients (Keep-Alive). Nginx is event-driven and asynchronous. It doesn't spawn a process for every connection.
The Strategy:
- Nginx (Port 80): Handles all incoming traffic. Serves static files (JPG, CSS, JS) directly from disk. Buffers slow client connections.
- Apache (Port 8080): Only processes the heavy dynamic scripts passed to it by Nginx.
Configuration Guide (CentOS 5.3)
Assuming you have a CoolVDS instance running CentOS 5, here is the battle-tested configuration we use for high-performance nodes.
1. Reconfigure Apache
First, move Apache off port 80. Edit /etc/httpd/conf/httpd.conf:
Listen 127.0.0.1:8080
We bind to localhost so no one can bypass Nginx. Restart Apache:
/etc/init.d/httpd restart
2. Install Nginx
If you don't have the EPEL repository enabled, grab the source. We recommend the 0.7.x stable branch.
3. The Nginx Configuration
This is where the magic happens. Edit /etc/nginx/nginx.conf. Pay attention to the proxy_pass directives.
server {
listen 80;
server_name example.com www.example.com;
# Serve static files directly - no Apache overhead
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /var/www/html;
expires 30d;
break;
}
# Pass dynamic content to Apache
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
Pro Tip: Don't forgetmod_rpaffor Apache. Since Nginx is talking to Apache, Apache will think all traffic is coming from 127.0.0.1. Installmod_rpafso your access logs show the real visitor IP addresses.
Why Hardware Still Matters
Software optimization can save your CPU, but disk I/O is often the hidden bottleneck. Many budget VPS providers in Europe overload their host nodes, leading to "I/O Wait" that kills your database performance regardless of your Nginx config.
At CoolVDS, we don't oversell. Our nodes in Oslo use enterprise 15,000 RPM SAS RAID-10 arrays. While standard SATA drives cap out at around 70-80 IOPS, our SAS arrays deliver the random read/write speeds required for heavy MySQL queries.
Data Integrity and Law
Hosting physically in Norway offers more than just low latency to NIX (Norwegian Internet Exchange). With the strict enforcement of the Personopplysningsloven (Personal Data Act), ensuring your customer data remains on servers physically located within Norwegian jurisdiction is a significant trust factor for local businesses. Unlike US-based hosting which is subject to the Patriot Act, CoolVDS adheres to strict Norwegian privacy standards.
The CoolVDS Advantage
We use Xen HVM virtualization. Why does this matter? Unlike OpenVZ, where a neighbor can steal your memory, Xen provides true hardware isolation. Your RAM is yours. Combined with Nginx, this setup is virtually bulletproof.
Ready to drop your load average? Deploy a Xen VPS on our Oslo node today. If you need help tuning your my.cnf or compiling Nginx from source, our support team is just a ticket away.