Console Login

Stop Apache Thrashing: The Definitive Guide to Nginx Reverse Proxies

Stop Apache Thrashing: The Definitive Guide to Nginx Reverse Proxies

It starts with a few extra seconds of load time. Then, top shows your load average creeping past 5.0. Finally, the dreaded OOM killer steps in, and your MySQL process vanishes. If you are running a standard LAMP stack (Linux, Apache, MySQL, PHP) on a high-traffic site, you know this nightmare scenario intimately.

The problem isn't your code; it's the architecture. Apache 2.2 with mod_php is brilliant, but it relies on a process-based model (Prefork) where every connected client consumes a heavy thread. When you hit the "Slashdot Effect," you run out of RAM long before you run out of CPU.

There is a better way. By placing Nginx in front of Apache as a reverse proxy, you can handle thousands of concurrent connections with a tiny memory footprint. Here is how we implement this architecture to salvage crashing servers.

The Architecture: Nginx + Apache

In this setup, Nginx listens on port 80. It handles all the heavy lifting of connection handling and serves static assets (images, CSS, JS) directly from the disk. It only forwards requests for dynamic content (PHP) to Apache, which we bind to port 8080 on the loopback interface.

Pro Tip: We've seen memory usage drop by 60% simply by offloading static files to Nginx. Apache threads are expensive; don't waste them sending JPEGs.

Step 1: Install Nginx on CentOS 5

Nginx isn't in the default CentOS repositories yet. You'll need to add the EPEL repository or compile from source if you need specific modules. For stability, we recommend the 0.7.x stable branch.

cat > /etc/yum.repos.d/nginx.repo << EOF [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/5/noarch/ gpgcheck=0 enabled=1 EOF yum install nginx

Step 2: Configure Apache

We need to move Apache off port 80. Edit your httpd.conf:

# /etc/httpd/conf/httpd.conf Listen 127.0.0.1:8080 KeepAlive Off

Critical: Turn KeepAlive Off in Apache. Nginx handles the keepalives with the client. Apache should process the PHP request and close the connection immediately to free up the slot.

Step 3: The Nginx Reverse Proxy Config

Here is a battle-tested configuration block. This goes into your nginx.conf inside the server block.

server { listen 80; server_name example.com www.example.com; root /var/www/html; # Serve static files directly location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { access_log off; log_not_found off; expires 30d; } # 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; } }

Handling the Real IP

Since Apache is receiving connections from Nginx (127.0.0.1), your access logs will look wrong. Install mod_rpaf (Reverse Proxy Add Forward) on Apache so it sees the actual client IP passed in the X-Real-IP header. This is mandatory if you use IP-based blocking or geo-targeting.

Why Infrastructure Matters

Configuration is only half the battle. We recently migrated a client hosting a high-traffic forum. Their config was perfect, but the server was crawling. The culprit? I/O Wait.

When Nginx serves static files and writes logs, and MySQL writes to the InnoDB buffer pool, standard SATA drives choke. Seek times kill performance.

At CoolVDS, we don't oversell our nodes. We use enterprise-grade 15k RPM SAS RAID 10 arrays. While other hosts stack you on slow 7.2k drives, our storage subsystem offers the IOPs necessary to keep Nginx feeding data as fast as the network can take it.

The Norwegian Advantage

For developers targeting the Nordic market, latency is the silent killer. Hosting in the US or Germany adds 30-100ms of round-trip time. By placing your VPS in our Oslo datacenter, you are directly peering at NIX (Norwegian Internet Exchange). Your content reaches Norwegian users in single-digit milliseconds.

Furthermore, keeping your data within Norway ensures strict adherence to the Personopplysningsloven (Personal Data Act). With the scrutiny of Datatilsynet increasing, knowing exactly where your server rack physically sits is a compliance necessity, not a luxury.

Final Thoughts

Nginx is not a magic bullet, but combined with the right hardware, it changes the game. It allows you to squeeze 3x the traffic out of the same RAM allocation compared to a standalone Apache setup.

Don't let legacy architecture hold your project back. If you are ready to stop debugging timeouts and start scaling, it is time to upgrade.

Ready for raw performance? Deploy a CoolVDS instance with 15k SAS storage in Oslo today.