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
1 2 3 4 5 6 | pm.max_children – the maximum number of children that can be alive pm.start_servers – the number of children created on startup. pm.min_spare_servers – the minimum number of children idle pm.max_spare_servers – the maximum number of children in ‘idle’ |
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
1 2 3 4 5 | worker_processes ## Set this to the number of cores your machine has worker_rlimit_nofile ## Set this to a very high value 10000+ worker_rlimit_nofile ## Number of file descriptors nginx can create , Set this toa very high value, there is no harm doing so. |
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
1 2 3 | listen = “/tmp/php-fpm.sock” |
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
1 2 3 4 | slowlog = /var/log/php5/slow.log request_slowlog_timeout = 5s ## Default is 0 means off |
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 .