Console Login
Home / Blog / Server Administration / Escaping the Apache Trap: High-Performance Nginx Reverse Proxy Configuration
Server Administration 8 views

Escaping the Apache Trap: High-Performance Nginx Reverse Proxy Configuration

@

Stop Letting Apache Eat Your RAM: The Case for Nginx

It starts with a few slow page loads. Then, the load average on your server creeps past 10.0. Suddenly, you’re SSH-ing into a sluggish box only to find httpd processes consuming 95% of your memory. If you are still serving static content directly through Apache's prefork MPM (Multi-Processing Module) in 2011, you are essentially DDoSing yourself.

The reality of the modern web—even here in Norway—is concurrency. The old "one process per connection" model doesn't scale when you have hundreds of users keeping keep-alive connections open. This is known as the C10k problem.

The solution isn't throwing more RAM at the problem. The solution is event-driven architecture. Enter Nginx.

The Architecture: Nginx as the Bouncer

Think of Nginx not as a replacement for your application logic, but as a highly efficient bouncer. It stands at the front door (Port 80), handles the heavy lifting of handshakes and static files (images, CSS, JS), and only lets the serious requests through to your backend (Apache/PHP or Python).

I recently migrated a high-traffic news portal based in Oslo from a pure Apache setup to an Nginx reverse proxy architecture. The load dropped from 15.0 to 0.8 instantly. Here is exactly how we did it.

1. Installing Nginx on CentOS 5

First, get the EPEL repositories if you haven't already. The stock repos are often outdated.

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum install nginx

2. The Reverse Proxy Configuration

We are going to tell Nginx to listen on port 80 and forward PHP requests to Apache, which we will move to port 8080. This keeps the heavy Apache processes free from dealing with slow clients.

Edit /etc/nginx/nginx.conf (or your site-specific conf inside /etc/nginx/conf.d/):

server {
    listen       80;
    server_name  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 backend
    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;
        
        # Timeouts to prevent hanging connections
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
    }
}
Pro Tip: Don't forget to install mod_rpaf on your Apache backend. Without it, Apache will think all traffic is coming from 127.0.0.1 (localhost), which renders your access logs useless and breaks IP-based restrictions.

Why Underlying Hardware Matters

Configuration is only half the battle. Nginx is incredibly efficient at I/O, using the epoll system call on Linux to manage thousands of connections. However, if your underlying disk subsystem is thrashing, your iowait will skyrocket regardless of your config.

This is where many VPS providers fail. They oversell their nodes using OpenVZ, meaning you are fighting for disk access with 50 other noisy neighbors. If one of them starts a backup, your site stalls.

At CoolVDS, we rely on Xen virtualization and high-performance RAID arrays. Xen provides strict resource isolation. When you configure Nginx to buffer requests to disk on a CoolVDS instance, you are getting guaranteed throughput. For database-heavy sites, our premium instances utilize enterprise-grade SSDs (Solid State Drives), which destroy the seek-latency of traditional SAS drives.

Local Latency: The Norwegian Advantage

If your target audience is in Norway, hosting in Germany or the US adds 30-150ms of latency to every handshake. With the TCP 3-way handshake, that delay triples.

By hosting on CoolVDS infrastructure connected directly to the NIX (Norwegian Internet Exchange), you reduce that latency to single-digit milliseconds for local users. Furthermore, adhering to the Personopplysningsloven (Personal Data Act) is significantly easier when your server physically resides within Norwegian jurisdiction, satisfying the requirements of the Datatilsynet.

Testing the Setup

Before you switch DNS, use curl to verify the headers are passing correctly:

curl -I -H "Host: www.example.no" http://127.0.0.1

You should see Server: nginx/0.8.54 instead of Apache. Once verified, restart your services:

/etc/init.d/httpd restart
/etc/init.d/nginx start

Don't let legacy configurations kill your growth. The shift from LAMP to LEMP (or Nginx proxying) is the industry standard for 2011.

Ready to handle real traffic? Stop sharing resources with strangers. Deploy a Xen-powered instance on CoolVDS today and see what true isolation does for your load times.

/// 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