A simplified Nginx-Apache combo with Wordpress support

Mike Green
Jun 28, 2010 · 11 min read

Which is faster — Varnish or Nginx?

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 26
model name      : Intel(R) Xeon(R) CPU           E5504  @ 2.00GHz
stepping        : 5
cpu MHz         : 1995.000
cache size      : 4096 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf pni ssse3 cx16 sse4_1 sse4_2 popcnt hypervisor lahf_lm
bogomips        : 3990.00
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:
Concurrency Level:      100
Time taken for tests:   29.548 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      25009500 bytes
HTML transferred:       3901482 bytes
Requests per second:    3384.27 [#/sec] (mean)
Time per request:       29.548 [ms] (mean)
Time per request:       0.295 [ms] (mean, across all concurrent requests)
Transfer rate:          826.55 [Kbytes/sec] receivedConnection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   12  39.1     12    1960
Processing:     9   18  49.6     14    2036
Waiting:        1   15  45.9     12    1966
Total:         14   29  65.9     26    2159
With Varnish in front of Apache, things really started to look good:Concurrency Level:      100
Time taken for tests:   13.489 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      28315282 bytes
HTML transferred:       1100594 bytes
Requests per second:    7413.64 [#/sec] (mean)
Time per request:       13.489 [ms] (mean)
Time per request:       0.135 [ms] (mean, across all concurrent requests)
Transfer rate:          2049.99 [Kbytes/sec] receivedConnection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6   2.2      6      71
Processing:     2    7   1.9      7      70
Waiting:        1    6   2.0      5      66
Total:          3   13   3.1     13      81
At 2.48x more than what Apache can send out on its own, that's a mighty impressive improvement and Varnish deserves kudos. But at 1 GBytes of RAM for caching, would it really be more efficient and quicker than Nginx? The following results tell ...Concurrency Level:      100
Time taken for tests:   9.438 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      27706648 bytes
HTML transferred:       5201248 bytes
Requests per second:    10595.55 [#/sec] (mean)
Time per request:       9.438 [ms] (mean)
Time per request:       0.094 [ms] (mean, across all concurrent requests)
Transfer rate:          2866.87 [Kbytes/sec] receivedConnection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   1.0      4      56
Processing:     2    6   9.7      5     253
Waiting:        0    5   9.7      5     253
Total:          5    9   9.7      9     257

A little Varnish quirk on Ubuntu

ulimit -n 65535

The Configuration

apt-get install nginx libapache2-mod-php5 memcached php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json

Update Nginx

# apt-get install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base
wget http://www.nginx.org/download/nginx-0.7.67.tar.gz
tar -xf nginx-0.7.67.tar.gz
cd nginx-0.7.67
./configure
 — user=www-data
 — group=www-data
 — sbin-path=/usr/sbin
 — conf-path=/etc/nginx/nginx.conf
 — error-log-path=/var/log/nginx/error.log
 — pid-path=/var/run/nginx.pid
 — lock-path=/var/lock/nginx.lock
 — http-log-path=/var/log/nginx/access.log
 — http-client-body-temp-path=/var/lib/nginx/body
 — http-proxy-temp-path=/var/lib/nginx/proxy
 — http-fastcgi-temp-path=/var/lib/nginx/fastcgi
 — with-debug
 — with-http_stub_status_module
 — with-http_flv_module
 — with-http_ssl_module
 — with-http_dav_module
 — with-http_gzip_static_module
 — with-http_realip_module
 — with-mail
 — with-mail_ssl_module
 — with-ipv6
make && make install

Configuring PHP and Apache

NameVirtualHost *:8080
Listen 127.0.0.1:8080
<VirtualHost *:8080>
… etc …
</VirtualHost>

Configuring Nginx

mkdir /etc/nginx/includes
mkdir -p /var/cache/nginx/tmp
mkdir /var/cache/nginx/cached
chown -R www-data:www-data /var/cache/nginx
user www-data;
worker_processes 4;
worker_rlimit_nofile 16384;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
 worker_connections 2000;
}
http {
 include /etc/nginx/mime.types;
 default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 75 20;
gzip on;
 gzip_vary on;
 gzip_comp_level 3;
 gzip_min_length 4096;
 gzip_proxied any;
 gzip_types text/plain text/css application/x-javascript text/xml
 application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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;
proxy_max_temp_file_size 56m;
proxy_temp_path /var/cache/nginx/tmp;
proxy_cache_key $scheme$host$request_uri;
proxy_cache_path /var/cache/nginx/cached levels=2:2 keys_zone=global:64m inactive=60m max_size=1G;
proxy_cache_valid 200 302 30m;
proxy_cache_valid 301 1h;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_pass_header Set-Cookie;

Includes

if ($http_cookie ~* “comment_author_|wordpress_(?!test_cookie)|wp-postpass_”) {
 set $no_cache 1;
}
if ($http_user_agent ~* “(2.0 MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo Wii|Nitro|Nokia|Opera Mini|Palm|PlayStation Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows CE|WinWAP|YahooSeeker/M1A1-R2D2|NF-Browser|iPhone|iPod|Android|BlackBerry9530|G-TU915 Obigo|LGE VX|webOS|Nokia5800)” ) {
 set $no_cache 1;
}
proxy_no_cache $no_cache;
# Enable caching:
proxy_cache global;
# Default:
location / {
 proxy_pass http://127.0.0.1:8080;
}
# Rarely changed items can remain cached longer:
location ~* .(jpg|jpeg|png|gif|ico|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
 proxy_cache_valid 200 3h;
 proxy_pass http://127.0.0.1:8080;
}
# Deny access to .ht* files:
location ~ /.ht {
 deny all;
}

A default site

server {
 listen 80;
 server_name _;
root /var/www/sites/default/public;
 index index.html index.htm;
access_log /var/www/sites/default/logs/access.log;
 error_log /var/www/sites/default/logs/nginx.error.log;
# Includes:
 include /etc/nginx/includes/wordpress.inc;
 include /etc/nginx/includes/default_proxy.inc;
}

WordPress

Performance

Concurrency Level: 100
Time taken for tests: 6.694 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 2822197200 bytes
HTML transferred: 2806393092 bytes
Requests per second: 7469.02 [#/sec] (mean)
Time per request: 13.389 [ms] (mean)
Time per request: 0.134 [ms] (mean, across all concurrent requests)
Transfer rate: 411700.31 [Kbytes/sec] received
Connection Times (ms)
 min mean[+/-sd] median max
Connect: 0 2 0.4 2 16
Processing: 3 11 0.8 11 27
Waiting: 1 3 1.2 3 25
Total: 6 13 0.8 13 32

Myatu’s

Myatu’s Tech Blog, from the site that has been wasting bits and bytes daily, since 2008.

Mike Green

Written by

I keep servers happy, and they keep me happy.

Myatu’s

Myatu’s

Myatu’s Tech Blog, from the site that has been wasting bits and bytes daily, since 2008.