Nginx – When should I switch to NGinx

apache-2.2nginx

I have a server with several domains and applications running, all through Apache. All is well at the moment but I have plans to develop some very performance-intensive web application (using C++ with CPPCMS), starting with my server for testing, maybe getting a separate server only for this application once it gets ready.

Any way, I've heard a lot about NGinx, that seems to be more performant than Apache, so I was asking myself if it was worth working with it for that new project. It's not clear in my mind because I don't know what kind of performance bottleneck NGinx does fix exactly.

I'm not a Apache power-user, I'm a poor linux admin and I don't develop web apps much (but I have notions). I'm mostly dedicated to writing software so the web server part is sometimes very obscure to me. Each time I have to configure a website through apach, I need a lot of time browsing in the doc to make sure I don't break everything.

That being said, I think I'm getting a lot better on this side but still need advice. I've seend some nginx configuration files around, and that seems much more understandable than the Apache ones, but maybe I'm wrong?

From informations I gathered, NGinx would be the best choice when you want load-balancing, so if you have your application spread on several machines, right?
As I'm thinking my application for scalling (and performance), it looks like it's what I need, but maybe I need to know more things about when it's interesting to move from Apache to NGinx. Is it worth switching to NGinx for all my current apps too? How much does it cost? (I mean, is it expensive on time to switch from one to the other?) Can I use Apache and NGinx both on the same machine without any problem?

Side note : Please don't urge me to use interpreted languages instead of C++, it's not related to the question. See the CPPCSM rationale page to see what kind of application can benefit fromt it. I perfectly understand the drawbacks (compared to apps in Ruby and Python, that I already use for less power-hungry webapps) and I'm fine with it.

Best Answer

Several points:

The major difference between Apache and Nginx or Lighttpd (that I personally very like) is architecture:

  1. Apache handles one connection per-process or per-thread (depending on mod-XYZ)
  2. Nginx and Lighttpd are single threaded handle multiple connections in single event loop.

As a result:

  1. Nginx and Lighttpd scales much better under high number of simultaneous connections, let's say with 1000 connections Apache would almost die as it would require 1000 processes in mod-prefork or 1000 threads in mod-worker.

    If you are planning to use Comet technologies where each connection requires long polling HTTP connection then Apache would not be acceptable as it does not scales well.

  2. Nginx and Lighttpd consume less memory as each connection require +- two sockets (HTTP and FastCGI), intermediate memory buffer and some state, while Apache would need entire thread including stack and other things.

From my personal experience in benchmarks I did Lighttpd (and I assume Nginx as well) is slightly faster with FastCGI backend then Apache but this is for low amount of connections.

Now another point is when you want to do some load-balancings using FastCGI connections.

In traditional architecture there is

                               |
                              HTTP
                               |
         Balancer (Nginx/Lighttpd/Cherooke/something-else)
            /      |           |            |      \
         HTTP    HTTP         HTTP        HTTP     HTTP
         /         |           |            |         \
 Apache+PHP   Apache+PHP   Apache+PHP   Apache+PHP   Apache+PHP   
  Server 2    Server 3     Server 4      Server 5     Server 6

Because Apache handles pools of processes each of them running mod-PHP (or other modes)

When you using CppCMS that handles pools on its own you can do something different:

                               |
                              HTTP
                               |
         Balancer (Nginx/Lighttpd/Cherooke/something-else)    
                           Server 1
            /      |           |            |      \
         FCGI    FCGI         FCGI        FCGI     FCGI
         /         |           |            |         \
    CppCMS App  CppCMS App  CppCMS App    CppCMS App  CppCMS App
     Server 2    Server 3     Server 4      Server 5     Server 6

So basically you do not need another indirection level because CppCMS handles process, thread and connection pool for you. While PHP/Ruby/Perl need some Apache mod-XYZ or handle its own FastCGI connector.

Related Topic