Post view

Nginx vs. Apache Web Server

If you do some searching related to the title of this post you will find endless reviews and topics surrounding Nginx vs. Apache. Many seem to be a little biased towards one or the other for some reason. I guess people have their favorites. I have used both, and don't use one or the other extensively.

What's right for you really depends on what your website needs are, and how much time you have available to learn one or the other.

There is an unlimited supply of help online these days. However, for the beginner you will probably have an easier time with Apache.

I decided to run a few basic tests myself to see if there were any noticeable differences.

I installed a fresh operating system along with a standard base Boonex Dolphin Install with each.

The first test was on CentOS 6.5, and it was basically Apache 2.2x vs. Nginx 1.0.x.

CentOS 6.5 32 bit
Apache 2.2.15-27
PHP 5.3.3-28
MySQL 5.1.73
- index.php renders: 0.08 to 0.125 with fcgi/suexec
- 9.5 requests per second to index.php
- 900 requests per second to license.txt

CentOS 6.5 32 bit
Nginx 1.0.15
PHP 5.3.3-28
MySQL 5.1.73
- index.php renders: 0.07 to 0.85
- 12.3 requests per second to index.php
- 6400+ requests per second to license.txt

The next one was on Ubuntu 12.04.4, which was Apache 2.2.x vs. Nginx 1.1.x.

Ubuntu 12.04.4 32 bit
Apache 2.2.22
PHP 5.3.10
MySQL 5.5.37
- index.php renders: 0.024 to 0.035 with fcgi/suexec
- 12 requests per second to index.php
- 1350 requests per second to license.txt

Ubuntu 12.04.4 32 bit
Nginx 1.1.19
PHP 5.3.10
MySQL 5.5.37
- index.php renders: 0.025 to 0.035
- 40 requests per second to index.php
- 6600 requests per second to license.txt

The last test was on Ubuntu 14.04, and was Apache 2.4.x vs. Nginx 1.4.x.

Ubuntu 14.04 32 bit
Apache 2.4.7
PHP 5.5.9
MySQL 5.5.37
- index.php renders: 0.018 to 0.035 with fcgi/suexec
- 34 requests per second to index.php
- 660 requests per second to license.txt

Ubuntu 14.04 32 bit
Nginx 1.4.6
PHP 5.5.9
MySQL 5.5.37
- index.php renders: 0.018 to 0.025 (consistently 0.018-0.020)
- 5800+ requests per second to index.php (This one may not be accurate.)
- 5000+ requests per second to license.txt


Dolphin Render Time Screen Shot:

Render Time













Nginx Requests Per Second Screen Shot:

Nginx requests












Test Machine:
A KVM powered VPS with 1 vCPU (2.4 Ghz), 1 GB RAM, SSD HD, 1 GB port (sharred).
*RMS (Red5) / Java was running during all tests, but it wasn't in use.

Cache / Accelerators tested:

  • Varnish Cache / Web Accelerator
  • File Cache
  • Memcache
  • APC Cache
  • XCache

File cache outperformed all other cache mechanisms. In other words, when Varnish, Memcache, APC Cache, or XCache were installed and enabled in Dolphin Administration the page render time increased (i.e. it took longer to render with these than it did with standard file cache).

Example render times:
0.018 = 18 milliseconds
0.08 = 80 milliseconds
0.25 = 250 milliseconds or 1/4 of a second
0.75 = 750 milliseconds or 3/4 of a second
1.0 = 1000 milliseconds i.e. 1 full second

I should point out that render time is not the same as page load time. Render time is how long a server takes to process and output to a format that a web browser can understand. Load time is how long it takes to download the actual content like images, css, js, etc. Obviously smaller figures would be faster for each.

A render time under 0.25 would most likely be fcgi or mod_php. A render time of 0.25 and above would probably be CGI/suPHP, which is very slow, but in use by a lot of shared hosting companies.

Apache 2.4 with MySQL 5.5 was faster than Apache 2.2 with MySQL 5.1 at page render speed when comparing CentOS to Ubuntu. However, when comparing the two Ubuntu tests it was minimal.

Nginx and Apache 2.4 were nearly identical at page render speed, but Nginx consistently hit the 0.020 (20 millisecond) mark.

Basically Nginx won't make your website load noticeably faster than Apache under normal circumstances, but it will handle more requests per second (8-10 times as many per second in some cases). And, it uses a fraction of the amount of memory and cpu power that Apache requires. In other words, the time it takes to render would be nearly the same, but it would be able to handle more traffic / visitors arriving at the same time.

The notable differences in the tests above besides the operating system and web server is the MySQL version. Neither were tuned for performance. So it's possible that you could squeeze a little more out of each if you tweaked the configuration file (my.cnf).

Swithing to Nginx:
If one were considering Nginx it would be wise to install and use it from the start. Converting to it later can be time-consuming especially if you had multiple websites you were hosting because you would need update the configuration file to reflect your websites. Also it would take a considerable amount of time to convert Apache rewrite rules to Nginx rewrite rules. And, that is just to name a few.

Code used to test index.php render time:

Added to the top of index.php:

function utime (){
    $time = explode( " ", microtime());
    $usec = (double)$time[0];
    $sec = (double)$time[1];
    return $sec + $usec;

$start = utime();

Added to the bottom of index.php:

$end = utime();
$run = $end - $start;
echo "Page created in: " .
   substr($run, 0, 5) . " seconds.";

Apache2-Utils was used for the requests per second tests.

With the following commands:
ab -n 1000 -c 20 http://domain or ip/index.php
ab -n 1000 -c 20 http://domain or ip/license.txt

ab -n 10000 -c 100 http://domain or ip/index.php
ab -n 10000 -c 100 http://domain or ip/license.txt

And assorted variations of the above.

Like I mentioned earlier I do not prefer one or the other. However, I like Apache's module support. Nginx is a nice lightweight snappy web server, but it can be a real pain to get the configuration files setup and working the way you want. There are advantages and disadvantages with both and to me it's a toss up.

4239 Awesome Views
Post info
Sponsored Links
Yahoo and MSN (16 posts)
Alexa (3 posts)
Articles (1 posts)
Boonex (79 posts)
Boonex DolphinU (1 posts)
Dedicated Servers (2 posts)
Dolphin Pro (3 posts)
Google (25 posts)
Google, Yahoo and MSN (11 posts)
Hosting (3 posts)
Microsoft (4 posts)
My Ramblings (12 posts)
Resources (6 posts)
Servers (1 posts)
Tech News (26 posts)
Thoughts (1 posts)
Updates (1 posts)
Wordpress (1 posts)