Scaling Past Apache: The Nginx Reverse Proxy Guide
I see it every day. A perfectly good server melts down, not because the hardware is weak, but because the architecture is stuck in 2005. You are running Apache with mod_php. You get a spike in traffic from a generic banner ad on VG.no or Finn.no. Suddenly, your RAM usage spikes, swap thrashing begins, and the OOM-killer starts sniping processes.
The problem is the C10k problem (handling 10,000 concurrent connections). Apache’s prefork MPM is robust, but it creates a heavy process for every single connection—even the idle ones just waiting for a client to acknowledge a packet. It is unsustainable for high-traffic sites.
The solution isn't to buy a larger server yet. The solution is Nginx.
The Architecture: Nginx as the Bouncer
Think of Nginx as the bouncer and Apache as the bartender. The bartender (Apache) is great at mixing complex drinks (processing PHP), but terrible at crowd control. The bouncer (Nginx) can handle thousands of people at the door, check IDs, and only let valid orders through to the bar.
In this setup, we place Nginx on port 80 to handle all incoming connections. It serves static files (images, CSS, JS) instantly using virtually no RAM. It only passes dynamic requests (PHP) to Apache running on port 8080.
Step 1: The Installation (CentOS 5 / RHEL)
If you are on a CoolVDS Xen instance, you likely have the EPEL repositories enabled. If not, get them. We want Nginx 0.8.x (Stable).
yum install nginx
Step 2: Configuring the Proxy Pass
Open /etc/nginx/nginx.conf. We need to define the upstream backend. This tells Nginx where to send the heavy lifting.
upstream backend_hosts {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name example.no www.example.no;
# Serve static files directly - Fast!
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;
}
# Pass everything else to Apache
location / {
proxy_pass http://backend_hosts;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Sysadmin Note: Do not forget `proxy_set_header`. Without this, Apache will think every request is coming from `127.0.0.1`, and your access logs (and security blocking scripts like Fail2Ban) will be useless.
Why Hardware Matters: The I/O Bottleneck
Configuration can only save you so much. If your disk I/O is saturated, your `iowait` will climb, and Nginx will block waiting for files. This is where the underlying virtualization technology becomes critical.
Many budget hosts in Europe shove 50 customers onto a single SATA drive using OpenVZ. This creates a "noisy neighbor" effect. If one neighbor runs a backup script, your database query latency spikes to 500ms.
At CoolVDS, we strictly use Xen virtualization with dedicated allocation. We are also rolling out Enterprise SSD storage tiers. In 2010, SSDs are still a premium, but for database-heavy workloads, the random read/write speeds are 50x faster than traditional 15k RPM SAS drives. If you are running a high-traffic Magento store, SSDs are not a luxury; they are a requirement.
Latency and Legal Compliance in Norway
If your audience is Norwegian, hosting in Germany or the US adds unavoidable latency. A packet from Oslo to Texas and back takes roughly 120-140ms. From Oslo to our datacenter peering with NIX (Norwegian Internet Exchange)? Under 5ms.
Furthermore, we must consider the Personopplysningsloven (Personal Data Act). While Safe Harbor allows data transfer to the US, the Datatilsynet (Data Protection Authority) is becoming increasingly strict about how financial and health data is handled. Keeping your servers physically located in Norway simplifies your compliance posture significantly.
Tuning Buffers
Before you restart Nginx, tune your buffers to prevent writing temporary files to disk.
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
These settings allow Nginx to hold the response from Apache in memory before sending it to the client, freeing up the Apache process immediately.
The Verdict
Apache is not dead, but it should not be facing the public internet alone. By placing Nginx in front, you reduce memory footprint and increase concurrency. Combine this software stack with CoolVDS's low-latency network and Xen-based isolation, and you have a platform that can survive the "Slashdot effect" without breaking a sweat.
Need to benchmark this? Spin up a CoolVDS instance with SSD storage today. We offer a 100% uptime SLA because we know our architecture holds up.