🎉 Get 70% OFF all licenses this week! Shop Now
View Categories

Scan/Import stops working randomly in new VPS setup with following errors: 500/502/504/Gateway timeouts.

4 min read

Make sure you have added the Background Tasker cron job in your web panel cron tab settings. Read this topic to find how to setup tasker cron job. As it will fix scan halt and background processing automatically for you.

If you have added it successfully and still background process is still getting disconnected and halting, then follow below additional adjustments:

This issue is very common if you are using nginx proxy, apache with php-fpm/fastcgi, and have a lower timeout value and PHP fpm pool settings on php script run time when you scan a thousand emails list or handle many requests between the server. Based on the server configuration and usage, this issue can be resolved in several ways. You have to monitor and apply proper settings.

There are a few extra things you can try to fix the 504/502/Gateway timeout error. They would involve tweaking the server settings and increasing specific resource limits.

NEW SOLUTIONS #

A) Make sure you have assigned a proper HOST on the server and tested your scan@yourdomain.com email address webmail system by mail-tester.com and receives 10/10 without any error or warning on DNS or rDNS!

B) Make sure after saving the below edits in the config files, restart each package service from the web panel or via SSH.

These modifications can be reset to default if the web panel rebuilds a domain config.
Also, for the correct location to modify contact with each web panel developer/owner support / check their forums.

# PHP FPM POOL & INI CONFIG # #

Usual Location, look for .ini and .cnf files:

.cnf:
/etc/php/7.4/fpm/pool.d/yourdomain_file
/etc/opt/php/7.4/fpm/pool.d/yourdomain_file

.ini:
/etc/php/7.4/fpm/php.ini
/etc/opt/php/7.4/fpm/php.ini

*************
POOL Config:
*************
Bottom config sample based on a system with a 14-core CPU and 47GB of RAM:

pm = dynamic
pm.max_children = 140
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 35
pm.max_requests = 1000
request_terminate_timeout = 300

 

**************
PHP.ini Config
**************
max_execution_time = 300
max_input_time = 300
memory_limit = 30GB ; adjust based on 70% of your VPS RAM

# MYSQL My.cnf CONFIG # #

Usual location, but can look for [mysqld] in relative config :
/etc/my.cnf
/etc/mysql/my.cnf

Here is a detailed my.cnf file [mysqld] data config sample based on a system with a 14-core CPU and 47GB of RAM.

*****
add
*****
max_allowed_packet = 1024M ; adjust based on usage

 

****************************
and comment out if exists
****************************
#max_connections=200
#max_user_connections=50
#wait_timeout=10
#interactive_timeout=50
#long_query_time=5

# NGINX CONFIG # #

For your domain config file, if this value does not exist

Inside location, php add at the  end ‘fastcgi_read_timeout’

location ~ \.php$ {

    fastcgi_read_timeout 300s;
fastcgi_send_timeout 300s; }

# APACHE CONFIG #
#

For your domain config file, if this value does not exist
Inside VirtualHost, add at the  end ‘ProxyTimeout’

# Increase proxy timeout for long-running PHP scripts
ProxyTimeout 300

 

OLD SOLUTIONS #
Apache #

Apache users may increase the default timeout value in the httpd.conf file. For example:

# Timeout: The number of seconds before receives and sends time out.
Timeout 99999

Furthermore, increasing the max_execution_time limit in php.ini can also bring positive results:

max_execution_time = 99999;

Save the changes, restart apache, and re-check. The 504 gateway timeout error should be gone if the cause was insufficient request timeout value.

Nginx + FastCGI (php-fpm enabled): #

If your VPS utilizes Nginx, try increasing the following values in /etc/nginx/conf.d/timeout.conf

proxy_connect_timeout 99999; 
proxy_send_timeout 99999; 
proxy_read_timeout 99999; 
send_timeout 99999;

In php.ini set :

max_execution_time = 99999;

Change request_terminate_timeout parameter (commented by default) in /etc/php-fpm.d/www.conf (for Debian /etc/php5/fpm/pool.d/www.conf) file:

request_terminate_timeout = 99999;

Add fastcgi_read_timeout variable inside the ‘nginx’ virtual host configuration:

fastcgi_read_timeout 99999;

Add/increase the following values in the ‘http’ section of the /etc/nginx/nginx.conf file:

fastcgi_buffers 8 128k;
fastcgi_buffer_size 256k;

Restart both ‘apache’ and ‘nginx’.

==========================================

If above steps doesn’t work, try this one..

  1. Open your nginx.conf file located in /etc/nginx directory.
  2. Add this below piece of code under http { section:
    client_header_timeout 99999;
    client_body_timeout 99999;
    fastcgi_read_timeout 99999;
    client_max_body_size 32m;
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 128k;
    

    Note: If its already present, change the values according.

  3. Reload Nginx and php5-fpm.
    $ service nginx reload
    $ service php5-fpm reload
    

    If the error persists, consider increasing the values.

=========================================

PHP.ini #

Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):

max_execution_time = 99999;

Changes in PHP-FPM Try raising request_terminate_timeout setting in php.ini file (CentOS path is /etc/php-fpm.d):


request_terminate_timeout = 99999;
Changes in Nginx Config

Finally, add fastcgi_read_timeout variable inside our Nginx virtual host configuration:

location ~* \.php$ {
    include         fastcgi_params;
    fastcgi_index   index.php;
    fastcgi_read_timeout 99999;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
}

Reload PHP-FPM and Nginx

service php–fpm restart
service nginx restart

 

EXTERNAL SERVER’s CONFIG #

# PHP FPM POOL & INI CONFIG # #

Usual Location, look for .ini and .cnf files:

.cnf:
/etc/php/7.4/fpm/pool.d/yourdomain_file
/etc/opt/php/7.4/fpm/pool.d/yourdomain_file

.ini:
/etc/php/7.4/fpm/php.ini
/etc/opt/php/7.4/fpm/php.ini

*************
POOL Config:
*************
Bottom config sample based on a system with a 4-core CPU and 4GB of RAM:

pm = dynamic
pm.max_children = 40
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 1000
request_terminate_timeout = 600

**************
PHP.ini Config
**************
max_execution_time = 600
max_input_time = 600
memory_limit = 1024M ; adjust based on 40% of your VPS RAM

# MYSQL My.cnf CONFIG # #

Usual location, but can look for [mysqld] in relative config :
/etc/my.cnf
/etc/mysql/my.cnf

*****
add
*****
max_allowed_packet = 1024M ; adjust based on usage

****************************
and comment out if exists
****************************
#max_connections=200
#max_user_connections=50
#wait_timeout=10
#interactive_timeout=50
#long_query_time=5

# NGINX CONFIG # #

For your domain config file, if this value does not exist

Inside location, php add at the end ‘fastcgi_read_timeout & fastcgi_send_timeout’

location ~ \.php$ {

    fastcgi_read_timeout 600s;
fastcgi_send_timeout 600s;
}

# APACHE CONFIG #
#

For your domain config file, if this value does not exist
Inside VirtualHost, add at the  end ‘ProxyTimeout’

# Increase proxy timeout for long-running PHP scripts
ProxyTimeout 600

Leave a Reply