30

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,

Sammaye
  • 679
  • 1
  • 8
  • 16

3 Answers3

28

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. :)

Martin Fjordvald
  • 7,589
  • 1
  • 28
  • 35
  • 1
    Damn that really cleared things up :) thanks that was the explanation I was looking for. I think I'm pretty sold on learning Nginx now since Apache would choke and die using my site. Fortunately it looks really quite easy to move it across. I mean people on node.js have said write most things in that and only poll your php session class if you really need to (which I do unless theres a way of detecting when users close their windows :P). Yea I'm running on a massive server farm so knowing that there is no security threat helps tonnes, thanks for the great explanation :) – Sammaye Jul 05 '10 at 18:59
  • I've been looking at this: http://www.joeandmotorboat.com/2008/02/28/apache-vs-nginx-web-server-performance-deathmatch/ and this: http://blog.webfaction.com/a-little-holiday-present and it has really got me kiddying at the thought of being able to long poll to my hearts content really. Not a single memory problem unlike Apache :) – Sammaye Jul 05 '10 at 19:02
  • Wait so your saying I could make a multithread worker in java and php can work around it perfectly? I mean the biggest problem I see is the server since I get massive memory problems with Apache using long polling, which is common...ofc fixed by Nginx. – Sammaye Jul 05 '10 at 21:41
  • 1
    Essentially yes, I have file distribution workers written in C, using the gearman extension for PHP I send a distribution job to the gearman job server and it sends it out to a worker, which can be written in whatever language; PHP, Java, C, etc. This worker then does it's job and reports status back to gearman, which reports back to PHP. (unless a background job was chosen, in which case PHP ends without waiting for it) – Martin Fjordvald Jul 05 '10 at 23:43
  • 2
    I know this is an old posting but I have to say, this is one of the most informative posts I've found on the subject of nginx vs apache. Thans Martin, +1. – Akoi Meexx Dec 21 '12 at 19:29
5

I would suggest running nginx as a reverse proxy. It'll handle all of your static & cached files (where it's considerably quicker than Apache / less memory overhead) and then forward all requests for dynamic content to Apache.

  • Yes this is what most people seem to be doing atm I'll definately have to look into this :) – Sammaye Jul 05 '10 at 09:13
  • 1
    Remember to install mod_rpaf for Apache so that you can pass through client IP addresses for logging purposes (otherwise Apache logs will show all requests as being from 127.0.0.1), add the following into nginx config: proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; – Greg Annandale Jul 05 '10 at 09:29
  • I do wonder one thing before I try this tonight. If I'm routing through Nginx to Apache which serves my PHP does that mean that long polling would still have the same problems as Apache or does calling it in reverse proxy make Apache act differently in some way? – Sammaye Jul 05 '10 at 11:55
1

I'm not so sure that mod_php is faster than its alternatives, where did you read this? I've done some lab testing with nginx+php-fpm, and from what I've measured it beats every other setup.

Take a look at this setup: http://interfacelab.com/nginx-php-fpm-apc-awesome/

I've set it up nearly the same, except that I use PHP packages from http://www.dotdeb.org/ - wich includes a php-fpm package and a init script ready to use. I don't use memecache or syck.

pauska
  • 19,532
  • 4
  • 55
  • 75
  • http://stackoverflow.com/questions/78108/is-there-a-difference-between-apache-module-vs-cgi-concerning-security got it from here and I have checked the php manual and it does say that php_mod provides significant advantage over cgi versions. Your setup looks good. It looks very easy too. I'll look into it :) – Sammaye Jul 05 '10 at 09:49
  • 2
    It states that CGI is much slower than a builtin module - not FastCGI :) – pauska Jul 05 '10 at 09:52
  • Though slow processes can crash this server quite easily. That is my main concern I use slow processes such as file uploading and folder creation in php including downloading and file retrieving through it all. – Sammaye Jul 05 '10 at 09:57
  • 1
    Well - if you're worrying about PHP processess dying (or timing out) then FastCGI (or PHP-FPM) is the way to go. It can kill off dead child processess without interrupting other activities. – pauska Jul 05 '10 at 10:30
  • I'm guessing if I don't want the to server to crash everytime I run a slow service I would make a larger pool of workers right? – Sammaye Jul 05 '10 at 10:51
  • 1
    Yes. Or, well, it depends. How many (maximum) number of slow requests will you process at the same time? Set max PHP FPM threads to that, plus the number of "fast" cgi's you want avaible. I've heard about people running 200 PHP-FPM childs on a server with 4GB RAM, so I wouldnt worry too much about this if I were you. The next version of PHP (5.3.3) will include PHP-FPM by standard, where there also is an adpative engine included - it will scale according to how many requests you have pending. – pauska Jul 05 '10 at 11:18
  • 1
    I would be easily running over multiple servers (maybe upto 10) but if I can fit 200 requests that can be long polled on a 4GB server this should almost half the 20 server I would need running Apache. hmmmm...I will need to test this out tonight – Sammaye Jul 05 '10 at 12:44
  • @pauska. The link is broken. – daviewales May 22 '13 at 11:11
  • @pauska, as the links is still broken, can you please summarise the information from it if you still remember it and/or try to find an alternative location for it? – Stefan Marinov Mar 04 '14 at 12:03