Tuning Nginx php-fpm for high Traffic

With the release  of php7, PHP  is most preferred  for High traffic websites/applications. PHP7 brings explosive performance improvements, drastically reduced memory consumption, and a long list of new language features. Designed for handling high workloads it is the ultimate choice for web developers today.

Nginx is the most popular webserver used in high traffic environments, powering over 40% of busiest websites in the world. It can handle tens of thousands of simultaneous requests, with very low memory footprint. It can scale in all directions, from small VM server up to large cluster of servers.

Nginx with php-fpm, is widely used for handling high throughput workloads. For most use cases, default NGINX and PHP_FPM settings work well, but achieving optimal performance  requires a bit of tuning

Tuning nginx-phpfpm for high traffic websites/applications

1. Adjust the child  processes in php-fpm

 The number of child processes are set dynamically based on the following directives

By default these  values are set to low and you might not handle high traffic with these values. You have to increase these values considering the workloads and Server capacity.

2. Adjust the worker processes in Nginx

 Adjust the following directives related to nginx worker processes

3. Switch from TCP to UNIX domain sockets

If nginx and php-fpm are running on same server, make sure you are using Unix domain sockets instead of tcp for fasctcgi connection.  Following directive inside the php-fpm configuration

UNIX sockets have better performance the TCP because there’s less copying and fewer context switches.

4. Disable access logs

Log files on high traffic sites involve a lot of I/O that has to be synchronized across all threads. This can have a big impact on the performance. Make sure access logs are disabled while keeping error logs enabled

5.PHP Accelerator

Make sure you have enabled a PHP accelarator. Starting with PHP 5.5 the Zend Opcache is integrated and shipped with PHP . Make sure its is enabled and configured properly.

A huge performance gain is associated with this. You can check the status and usage of the cache by enabling any of the Opcache web UI.

6. Enable PHP_FPM slow logs

You can enable php-fpm slow logs by adding the following lines in the php-fpm pool configuration file

This will help you to troubleshoot the latency issues and findout the slow backends or slow external endpoints.

7. Compression

Compressing responses often significantly reduces the size of transmitted data . However, since compression happens at runtime it can also add some processing overhead. So watch the server CPU usage closely after enabling it .

Leave a Reply

Your email address will not be published. Required fields are marked *