Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache or nginx ? I like to understand the basic working flow of Nginx , its advantage and disadvantage

Tags:

nginx

apache

Pros & cons over Apache or nginx and how they work internally in order to maximize the resource utilization

Can I use Apache & Nginx together ? If I use only Nginx then what problem I can face ?

like image 279
Prabhaker shukla Avatar asked Jun 21 '14 06:06

Prabhaker shukla


1 Answers

Apache has some disadvantages, especially when it is used with the PHP module.

Apache's process model is such that each connection uses a separate process. Each process carries all the overhead of PHP and any other modules you may have loaded with it. An Apache process might run a PHP script or serve static content for one request. If the PHP has a memory leak (which does happen sometimes), the process continues to grow in size. Also, when KeepAlive is enabled, which is usually recommended, that process stays alive for a few seconds after the connection, consuming a "slot" that another client might be able to use and helping the server to reach its MaxClients sooner.

Nginx is an alternative webserver that normally uses the Linux "epoll" API to process requests in a non-blocking mode. This means that one single process can handle many simultaneous connections. Epoll is an efficient way to tell the single process which connection(s) it needs to deal with and which can wait. Nginx has a goal of solving the "C10k" problem - how to have 10,000 concurrent connections.

This naturally goes hand in hand with php-fpm, the FastCGI Process Manager. Nginx itself does not have PHP built-in. When it receives a request for a PHP script, it makes a call out to php-fpm to run the script, which then returns the result to nginx, which returns it to the client.

This all uses a lot less memory than a similar Apache+mod_php configuration.

There are a couple more huge advantages of php-fpm over mod_php:

  • It uses different "pools", each of which can run as a separate Linux user. This provides a simple and effective way of isolating websites (for example, if they are run by different customers who should not read each other's code) without the overhead or nastiness of suexec or suphp.
  • It has a slow log feature where it can dump a PHP stack trace of any script that has been running for greater than X seconds. This can help diagnose slow code issues.

Php-fpm can be run with Apache, and in fact this allows you to take advantage of Apache's more efficient Worker MPM (or Event in Apache 2.4). However, my experience is that configuring it in Apache is significantly more complex than configuring it in nginx, and even with Worker, it still is not quite as efficient with nginx.

Disadvantages of moving to nginx - not many, but things to keep in mind:

  • It does not support .htaccess files. I think this is a good thing personally as .htaccess files must be parsed by Apache for every request, which can cause significant overhead.
  • Configuration files need to be re-written. If you have many complex site configurations, this could take some doing. For simple cases it is not usually a big deal.

Feature Of Nginx

  • Nginx is fast because it does not need to create a new process for each new request.
  • HTTP proxy and Web server features
  • Ability to handle more than 10,000 simultaneous connections with a low memory footprint (~2.5 MB per 10k inactive HTTP keep-alive connections)
  • Handling of static files, index files, and auto-indexing
  • Reverse proxy with caching
  • Load balancing with in-band health checks
  • Fault tolerance
  • Nginx uses very little memory, especially for static Web pages..
  • FastCGI, SCGI, uWSGI support with caching
  • Name- and IP address-based virtual servers
  • IPv6-compatible
  • SPDY protocol support
  • FLV and MP4 streaming
  • Web page access authentication
  • gzip compression and decompression
  • URL rewriting having its own rewrite engine
  • Custom logging with on-the-fly gzip compression
  • Response rate and concurrent requests limiting
  • Bandwidth throttling
  • Server Side Includes
  • IP address-based geolocation
  • User tracking
  • WebDAV
  • XSLT data processing
  • Embedded Perl scripting
  • Nginx is highly scalable, and performance is not dependent on hardware.

With only Nginx, you lose a whole bunch of apache-specific features such as all the mod_dav stuff. You lose a lot of modules, effectively

Conclusion

The best use for nginx is in front of Apache if you need Apache modules. Use it as a load-balancer if you might, between multiple Apache instances, and you suddenly have a mixed set-up that is rather

like image 145
Vikas Kumar Avatar answered Nov 02 '22 23:11

Vikas Kumar