Console Login
Home / Blog / Server Administration / Nginx Reverse Proxy: Stop Apache from Eating Your RAM
Server Administration 8 views

Nginx Reverse Proxy: Stop Apache from Eating Your RAM

@

The Apache MaxClients Nightmare

We have all been there. It is 2:00 AM. Your monitoring system is screaming. You SSH in, run top, and see the horror: Load Average: 25.4. Your RAM is gone, swapped out to disk.

The culprit? Apache. Specifically, the Prefork MPM. Every time a user connects to download a 4KB logo, Apache spawns a heavy child process, consuming 20-30MB of RAM just to hand over a static file. If you have 2GB of RAM and limit MaxClients to 150 to stay safe, the 151st user gets a timeout. Your site isn't just slow; it is effectively down.

In 2011, throwing more hardware at this problem is expensive. The smart move isn't buying another server; it is changing your architecture. Enter Nginx.

The Event-Driven Savior: Nginx 1.0

With the recent release of Nginx 1.0 stable, we finally have a production-ready alternative to the threaded madness. Unlike Apache, Nginx uses an asynchronous, event-driven architecture. It doesn't spawn a process for every connection. One worker process can handle thousands of concurrent connections using very little memory.

By placing Nginx in front of Apache (as a reverse proxy), Nginx handles the heavy lifting—serving static files (images, CSS, JS) and managing client connections. It only talks to Apache when it needs PHP processing. This is the Nginx Proxy / Apache Backend setup.

Configuration: The "Heavy Lifter" Setup

Here is the battle-tested configuration we use at CoolVDS for high-traffic Norwegian clients. This assumes you have moved Apache to listen on port 8080.

server {
    listen       80;
    server_name  example.no www.example.no;

    # 1. Serve Static Files Directly (Skip Apache)
    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
        root   /var/www/html;
        expires 30d;
        access_log off;
    }

    # 2. Pass Dynamic Requests to Apache
    location / {
        proxy_pass         http://127.0.0.1:8080;
        proxy_redirect     off;
        
        # Essential headers for the backend to know the real IP
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

        # Buffer settings to handle backend timeouts gracefully
        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;
    }
}
Sysadmin Note: Do not forget to install mod_rpaf on your Apache backend. Without it, Apache will think all traffic is coming from 127.0.0.1, which renders your logs useless and breaks IP-based security.

Why Hardware Still Matters (Even with Nginx)

Software optimization works wonders, but it cannot fix bad I/O. Even Nginx will block if the disk subsystem is thrashing. This is where the underlying infrastructure becomes critical.

Many VPS providers in Europe are still over-selling consumer-grade SATA drives. When a "noisy neighbor" on the same physical host starts a backup, your disk I/O wait (iowait) spikes, and Nginx stalls waiting to read that static image.

At CoolVDS, we refuse to play that game. We use enterprise SAS RAID arrays and the latest SSD caching technology. Furthermore, our virtualization platform ensures strict resource isolation. If another user compiles a kernel, your CPU cycles remain yours. For a business targeting customers in Oslo or Bergen, latency matters.

Latency and The Norwegian Context

If your target audience is in Norway, hosting in Germany or the US adds milliseconds that translate to lost revenue. Routing traffic through NIX (Norwegian Internet Exchange) ensures the lowest possible ping times.

Additionally, consider compliance with the Personal Data Act (Personopplysningsloven) and Datatilsynet guidelines. Hosting data within the EEA (or specifically Norway) simplifies your legal standing regarding data export, ensuring you are on the right side of privacy regulations.

The Verdict

Stop letting Apache Prefork eat your budget. Implement Nginx as a reverse proxy today. It is the single most effective change you can make to improve concurrency without upgrading hardware.

However, if you are tired of wondering if your current host is overselling their CPU, it is time for a serious upgrade. Deploy a CoolVDS instance with pure resource isolation and high-speed storage. Your top command will thank you.

/// TAGS

/// RELATED POSTS

Surviving the Spike: High-Performance E-commerce Hosting Architecture for 2012

Is your Magento store ready for the holiday rush? We break down the Nginx, Varnish, and SSD tuning s...

Read More →

Automate or Die: Bulletproof Remote Backups with Rsync on CentOS 6

RAID is not a backup. Don't let a typo destroy your database. Learn how to set up automated, increme...

Read More →

Nginx as a Reverse Proxy: Stop Letting Apache Kill Your Server Load

Is your LAMP stack choking on traffic? Learn how to deploy Nginx as a high-performance reverse proxy...

Read More →

Apache vs Lighttpd in 2012: Squeezing Performance from Your Norway VPS

Is Apache's memory bloat killing your server? We benchmark the industry standard against the lightwe...

Read More →

Stop Guessing: Precision Server Monitoring with Munin & Nagios on CentOS 6

Is your server going down at 3 AM? Stop reactive fire-fighting. We detail the exact Nagios and Munin...

Read More →

The Sysadmin’s Guide to Bulletproof Automated Backups (2012 Edition)

RAID 10 is not a backup strategy. In this guide, we cover scripting rsync, rotating MySQL dumps, and...

Read More →
← Back to All Posts