Apache vs Nginx – Comparison and Performance Analysis

apache-2.2fastcgimod-phpnginx

I have been investigating the differences between Apache and Nginx recently and am confused about which I should choose.

I have done some searching but there is no definitive comparison between the two and I was wondering if someone here could give their views on the differences between the two.

My current knowledge allows me to understand that mod_php is faster and more secure than fastcgi however Apache is much worse when it comes to simultaneous connections and memory consumption.

My site is using a lot of long polling but has a non AJAX web base (i.e. Apache with long polling over the top).

My original solution to Apaches memory problems were to send the long polling through node.js and then get node.js to access Apache every 2 secs in which case Apache would not have an open connection but instead node.js would. I have come to the realisation this might not be good enough and am looking at different solutions. I am still interested as to whether my original idea would have worked.

So which is better for the modern web? Apache or Nginx?

Update: All the suggestions given were good and valid. I have gone with the original second idea which is to use a full Nginx server. I am satisfied that being a dedicated server I could not suffer security issues from fastcgi and since my long polling scripts need to be written in PHP I require a server that can deal with high load simultaneous connections and Apache just cannot do that no matter how much I change the structure it will still be memory hungry.

I have marked Martin F's answer since he gave such a clear and complete answer to my questions points that I feel he deserves the mark, however, all three answers were good and valid and will most definately look into using reverse proxy for another site I own since I just found something very very very kool that Nginx can do in proxying.

Thanks,

Best Answer

You seem to have a few misconceptions which I feel needs to be addressed.

First of all, mod_php is only marginally faster, all my tests have shown that the difference is so minuscule that it's not worth factoring in. I also doubt that the security aspect is relevant to you as you seem to be looking at a dedicated server and mod_php really only has an advantage in a shared environment - in fact, in a dedicated environment php-fpm will have the advantage as PHP and your web server now runs as different processes, and that's not even factoring in the awesome logging options in php-fpm such as slow log.

If the world was black and white I'd say go with a pure nginx setup and compile php with php-fpm. More realistically if you already have Apache working then make nginx a reverse proxy to apache and you might save a few hours of setup time and the difference in performance will be tiny.

But lets assume the world is black and white for a second, because this makes for far more awesome setups. You do nginx + php-fpm for your web server. To solve the uploads you use the upload module and upload progress module for nginx. This means that your web server accepts the upload and passes the file path onto PHP when it's done, so that the file doesn't need to be streamed between nginx and PHP via fastcgi protocol, sweet. (I have this in a live setup and it's working great, btw!)

For user downloading you use nginxs x-send-file-like feature called x-accel-redirect, essentially you do your authentication in PHP and set a header which nginx picks up on and starts transfer that file. PHP ends execution and your web server is handling the transfer, sweet! (Again, I have this in a live setup and it's working great)

For distributing files across servers or other long running operations we realize that PHP isn't really best suited for this, so we install gearman, which is a job server that can distribute jobs between workers on different servers, these workers can be written in any language. Therefore you can create a distribute worker and spawn 5 of them using a total of 200 KB of memory instead of the 100 MB PHP would use. Sweet. (I also have this running live, so it's all actually possible)

In case you haven't picked up on it yet, I think many of your problems aren't related to your web server at all, you just think that way because Apache forces it to be related to your web server due to it's structure, often there are far better tools for the job than PHP and PHP is a language which knows this and provides excellent options to off-loading work without ever leaving PHP.

I'd highly recommend nginx, but I also think you should look at other options for your other problems, if you have a scaling or performance problem then feel free to write me. I don't know if you can send messages through here but otherwise write me at martin@bbtn.us as I don't stalk server fault for anything not tagged with nginx. :)