4

Would nginx be a more suitable choice as a web server for high traffic websites?

The site we will be building is an e-commerce site, if that makes a difference.

I am really interested in the actual 'why' from a technical point of view either way. i.e., why would nginx be a better choice for this type of site from a technical standpoint, or the opposite, why it wouldn't?

Hari Harker
  • 702
  • 1
  • 12
  • 29
Martin
  • 815
  • 1
  • 7
  • 6
  • https://www.google.com/search?q=apache+vs+nginx – Danica Jun 23 '12 at 18:37
  • Thanks @Dougal - very good link :) - I am looking for advice in a specific use case of these servers though, not a general Apache v nginx. Thanks for your input though, you are a very knowledgeable chap and i appreciate you taking the time to give that link – Martin Jun 23 '12 at 18:40

1 Answers1

13

Martin,

In general, Nginx is better for high-traffic sites due to its event-driven architecture. Rather than handling each request in a distinct thread, it uses non-blocking I/O to service many requests in each thread.

The important aspect of this architecture is the reduced use of processes or threads. A thread can consume anywhere from 2MB to over 64MB of RAM. So when Apache serves a 10KB JPEG, it may actually be using a significant amount of RAM. It becomes worse if you have slow clients (e.g. smartphones) where the request may keep a thread busy for several seconds.

Many people find that running Nginx as a proxy in front of Apache to be an ideal middle ground. Nginx talks to the slow clients and can do so using a very small amount of RAM. When requests are forwarded to Apache, the request speed is limited by your local connectivity, not that of the remote user. This means that the network bottleneck will not keep the request (and it's memory-hogging thread) alive for any longer than necessary.

In short you get the low-resource benefits of Nginx coupled with the wide feature-set of Apache.

cliff.wells
  • 735
  • 7
  • 9
  • apache2-mpm-event. Haven't compared it yet but it's the same architecture for Apache and should be mentioned. – korkman Feb 10 '14 at 14:54
  • @cliff.wells by event-driven architecture, do you mean Nginx gets notified about the event when PHP gets a response ready? Also, I didn't understand the 2nd part, i.e. what do you mean by the wide feature-set of Apache and how does it make running Nginx as a proxy in front of Apache an ideal middle ground. – Sandeepan Nath Aug 26 '16 at 18:33
  • so php on nginx can give a performance equivalence of node js ? – Maaz Rehman Aug 09 '17 at 10:56
  • 1
    @MaazRehman that would also depend on the relative speeds of the languages in question. – cliff.wells Aug 14 '17 at 18:50
  • Oh . But as far as I know , Node js is asynchronous while php normally is not. Does that make comparing node js and php irrelevant ? – Maaz Rehman Aug 15 '17 at 07:46
  • Neither of those are really my area of expertise, but you can write asynchronous code in pretty much any language (including PHP, see http://reactphp.org/). – cliff.wells Aug 24 '17 at 04:22