Console Login
Home / Blog / Server Administration / Stop the Swap: Configuring Nginx as a Reverse Proxy for Apache in 2010
Server Administration 9 views

Stop the Swap: Configuring Nginx as a Reverse Proxy for Apache in 2010

@

The 3:00 AM Pager Duty

It is a sound every sysadmin dreads: the Nagios alert. Your load average just hit 20.0, and the swap usage is climbing. You SSH in, run top, and see the culprit immediately. It is Apache. It is always Apache.

The traditional LAMP stack is robust, but it creates a process or thread for every single connection. When you get hit by the "Slashdot effect" or a traffic spike from a VG.no feature, Apache eats your RAM until the kernel OOM-killer steps in. It is messy.

You do not need to rewrite your entire application. You just need to change the front door. By placing Nginx in front of Apache as a reverse proxy, you can serve static content efficiently and only wake up the heavy Apache processes for dynamic PHP.

The Architecture: Nginx + Apache

We are not replacing Apache; we are shielding it. Nginx uses an asynchronous, event-driven architecture (specifically the epoll event notification mechanism on Linux). This allows a single process to handle thousands of concurrent connections with a tiny memory footprint.

Pro Tip: Do not just install yum install nginx from the default repositories if they are outdated. Add the EPEL repository or compile Nginx 0.8.53 from source to get the latest stable features.

Configuration Guide

Here is the battle-tested configuration we use at CoolVDS for clients running heavy Magento or Drupal sites on CentOS 5.5.

Step 1: Move Apache
First, edit your httpd.conf. Change the Listen port so Apache stops hogging port 80.

Listen 127.0.0.1:8080

Step 2: Configure Nginx Proxy
Create a new virtual host file in /etc/nginx/conf.d/. We need to tell Nginx to forward PHP requests to Apache while serving images and CSS directly.

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;
        access_log off;
        expires 30d;
    }

    # Pass the rest 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;
    }
}

The Storage Bottleneck: Why HDD Kills Performance

Configuration is only half the battle. When Nginx buffers responses or when Apache writes session files, you are hitting the disk. If you are running on a standard 7,200 RPM SATA drive, your I/O Wait (wa in top) will skyrocket during traffic spikes.

In 2010, storage is the single biggest bottleneck in hosting. This is why we built the CoolVDS platform differently. While others resell standard SATA VPS nodes, we are aggressive adopters of Enterprise SSDs. The random read/write speeds on SSDs are exponentially higher than mechanical disks.

When your database tries to perform a complex JOIN on a 2GB table:

  • SATA Drive: The drive head physically moves. Latency: 12ms+. Result: Your site hangs.
  • CoolVDS SSD: No moving parts. Latency: <0.1ms. Result: Instant page load.

Local Latency and Data Law

For our Norwegian clients, physics still applies. Hosting in the US or Germany adds 30-100ms of latency to every handshake. If your server is in Oslo, connected directly to NIX (Norwegian Internet Exchange), your local users get a snappy experience. Furthermore, adhering to the Personopplysningsloven (Personal Data Act) is simpler when your data physically resides within Norwegian borders.

Final Check

Before you restart services, test your Nginx syntax:

service nginx configtest

If it returns OK, flip the switch. Your RAM usage should drop, and your capacity for concurrent users should double. If you are still seeing I/O wait issues, it is not your config—it is your hardware. Stop fighting physics on spinning rust.

Need a test environment? Deploy a CentOS VDS with SSD storage on CoolVDS today. We are peered at NIX in Oslo for minimum latency.

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