Thursday, 15 October 2015

504 Gateway Timeout Error in Nginx Server

When we are dealing with large Drupal / Wordpress setup in Nginx Server with limited resources, then we will be facing the “504 Gateway Time-out” error.

There are 2 ways to control / fix this problem.
  • By Increasing PHP Execution Time from Nginx level.
  • Minimize the Code execution time by optimizing server calls (This is with respect to programming).
In this Article, i would like to show First point of Increasing PHP Execution Time from Nginx Level.
We can follow below steps to increase the php timeout value. By default it's max execution time will be 30s. Now we will increase that to 500 (These seconds depends on your usage). 

In General of Apache server, if we increase max_execution_time in php.ini file and restart php server will work. but in Nginx, we have to do changes in 2 more places and restart both PHP-FPM & Nginx.

  • Change max_execution_time in php.ini file.
    • Connect to Linux -> Nginx Server Command Prompt
    • Open PHP.INI file -> vim /etc/php5/fpm/php.ini
    • Change Max Execution Time to 500 seconds -> max_execution_time = 500 
  •  Change request_terminate_timeout in PHP-FPM
    • This change needs to be done only if we have un commented request_terminate_timeout line before. As it is commented by default, it takes value of max_execution_time from php.ini
      • Open www conf file - vim /etc/php5/fpm/pool.d/www.conf
      • Change Request Terminate Timeout value to 500 -> request_terminate_timeout = 500
  • Change fastcgi_read_timeout value in Nginx Config
    • If we want to increase timeout value only for one site, follow below steps.
      • vim /etc/nginx/sites-available/
      • location ~ \.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_pass  unix:/var/run/php5-fpm.sock;
                fastcgi_read_timeout 300;
    • If we want to increase time limit for all sites in our server, we can edit main nginx.conf file itself
      • vim /etc/nginx/nginx.conf
      • Add 'fastcgi_read_timeout' in http{} section
      • http { 
           ---Other Lines of code --  
           fastcgi_read_timeout 500; 
           ---Other Lines of code -- 
  • Reload PHP-FPM and Nginx
    • Reload PHP-FPM -> service php5-fpm reload 
    • Reload Nginx -> service nginx reload

No comments:

Post a Comment

Thank you so much for providing your valuable feedback. I will will look into them and update my skills & technologies accordingly.