Socialengine performance optimization : Nginx – Apache Combo

In our last two articles Optimize performance of your website and Iincrease performance and speed up socialengine using Memcached / APC /Xcache, we discussed about the tips to improve the performance of the socialEngine or any of custom website. In this article, we shall improve the performance further by using Nginx – Apache Combo pack.

We were using the apache + mod php for the socialEngine for one of our clients. Initially when there were less user load then website used to perform well but as user load increases with the popularity of the website, site become slower and sometimes it used to crashed without giving any warning. It was very difficult to found the exact issue for the crash. Initially we suspected that the mysql (MariaDB) can be the main culprit but after installing munin, it become clear that the issue is with Apache. We investigated further and found that apache’s preform MPM uses multiple child processes with one thread each and each process handles one connection at a time. So this resulted in more memory usage for apache as more and more users visited the website. This resulted in system crash and we used to reboot the system manually.  We tried to fine tune the prefork parameters but site was still falling apart.

We had no clue what should we do to stop the site crash. Users started complaining about page not opening or taking too much time to open the page. We thought about moving the static contents (css/js/images) to different server or even though of using Amazon s3. After looking at the articles on google, we tried NGINX as reverse proxy for our Apache’s server. In this mode, NGINX looks like just another Web server to users. Behind the scenes, though the reverse proxy is used to load balance Web requests among several back-end servers, or to provide caching for a slower back-end server. This result in very fast and stable Web Site with same hardware.

Apache performance with ngnix

There were spikes in the apache’s access during the starting of September month when only Apache was used.

Using NGINX as reverse proxy took the most of the load from apache server which resulted in fast and stable website. Earlier accesses/min used to reach ~5K and resulted in more apache’s processes thus more memory usage. Site is stable now as NGNIX handles static contents (js/css/images) while apache needs to process only php scripts.

Apache used to use 150 processes in month of September and resulted into site crash

Memory usages reduced drastically after ngnix acting as reverse proxy

NGINX can deliver 10 times the performance on the same hardware. NGINX is very fast and uses far less resources than Apache. It does this by being event-based. That means it doesn’t spawn new processes or threads for each Web page request. So even as the load increases, memory use remains predictable

The performance can be further increased by using php-fpm or fast-cgi in apache instead of using mod-php or use ngnix+fast-cgi. We tried to follow these approaches but found some limitations with each approach with our requirements. We would love to know more approaches to improve performance of website so please don’t hesitate to comment.

It is possible to do performance tuning by yourself, or getting developers to do this. However, in our experience, the community owners are really more focused on making their community an engaging experience via functionality or graphics. Similarly, most developers can write PHP code, but very few have the experience of various tools for improving performance. One of the solutions we thought was to bake this optimization right into the hosting, so we have teamed up with Acenet hosting to provide you hosting for SocialEngine and Phpfox with built-in PHP, web server and database performance enhancements. Custom optimization may be required when your community grows huge, but that is different for everyone and we will continue to provide it as a separate service.

References:

Socialengine performance optimization : Nginx – Apache Combo was last modified: April 11th, 2016 by Kapil Jain
  • msnyc10

    Wouldnt the same process be handled by a CDN?

    • Yes, its almost the same process which is handled by CDN,infact its complement to the CDN service but one have more control on the process. One can create cache copy of the specific pages on nginx as well.