0

Tengo un script que importa datos de un sistema a otro por lo que es un sistema que requiere de tiempo ya que traspasa casi 300.000 registros.

Para ello he creado una clase con PHP usando el framework laravel pero me sale un error 504 al ejecutar el script

Gateway Timeout The gateway did not receive a timely response from the upstream server or application.

Apache/2.4.29 (Ubuntu) Server at local Port 80

Por eso lo que hago es, en la clase del constructor setear una serie de parámetros del php , pero con todo eso me sigue saliendo el error.

El código de la clase es tal como éste:

class CarsController extends Controller
{

function __construct() {
    ini_set('max_execution_time', 999999);
    ini_set('max_input_time', 999999);
    ini_set('default_socket_timeout', 999999);
    ini_set('memory_limit','4096M');

    parent::__construct();
}


public function readMigration()
{
//Realizar las acciones SQL
}
}

La configuración del php uso php7.2-fpm y tengo la siguiente

; Default timeout for socket based streams (seconds)
; http://php.net/default-socket-timeout
default_socket_timeout = 60


;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 36000

; Maximum amount of time each script may spend parsing request data. It's a good
; idea to limit this time on productions servers in order to eliminate unexpectedly
; long running scripts.
; Note: This directive is hardcoded to -1 for the CLI SAPI
; Default Value: -1 (Unlimited)
; Development Value: 60 (60 seconds)
; Production Value: 60 (60 seconds)
; http://php.net/max-input-time
max_input_time = 36000

; Maximum input variable nesting level
; http://php.net/max-input-nesting-level
;max_input_nesting_level = 64

; How many GET/POST/COOKIE input variables may be accepted
; max_input_vars = 1000

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = -1
7
  • 1
    Por ese mensaje imagino que estás usando php-fpm o php-cgi para ejecutar el código PHP en tu servidor web (apache), ¿Estoy en lo correcto? Si es así dime cómo tienes configurada la parte de CGI porque tiene la pinta de que tienes configurado un timeout muy bajo. el 25 jun. 2018 a las 11:32
  • Edito la respuesta principal con la información que me pides. Gracias por adelantado
    – ilernet
    el 25 jun. 2018 a las 11:33
  • La parte que necesito es en la que se configura el parámetro FastCgiExternalServer. el 25 jun. 2018 a las 11:38
  • Es en el fichero de configuración del apache o del php?
    – ilernet
    el 25 jun. 2018 a las 11:39
  • Es un archivo de configuración del apache. El problema raíz está en el apache, no en PHP. el 25 jun. 2018 a las 11:39

1 respuesta 1

1

El problema que tienes está en la conexión FCGI que existe entre el servidor Apache y el FPM de PHP que pone un tiempo de 30 segundos máximo de espera en la recepción de datos.

En Ubuntu dicho archivo está en /etc/apache2/conf-available/php7.2-fpm.conf o /etc/apache2/conf-available/php7.1-fpm.conf y contiene:

# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost"
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        # Deny access to raw php sources by default
        # To re-enable it's recommended to enable access to the files
        # only in specific virtual host or directory
        Require all denied
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

Debes cambiar la línea que pone:

SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost"

Por la siguiente (para aumentar el tiempo a 30 minutos):

SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost timeout=1800"

O bien configurar las siguientes directivas en lugar del bloque <FilesMatch>:

<Proxy "unix:/run/php/php7.1-fpm.sock|fcgi://localhost">
   ProxySet timeout=1800
</Proxy>
<FilesMatch ".+\.ph(ar|p|tml)$">
   SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost"
</FilesMatch>

He podido probar esta última edición, pero aún así te recomiendo que este tipo de trabajos los hagas a través de CLI y no a través del servidor web. Aumentar el tiempo máximo de espera puede desencadenar problemas posteriores. Por regla general una página web debería generarse en el menor tiempo posible.

1
  • 1
    Perfecto , justo lo que necesitava. Muchas gracias
    – ilernet
    el 25 jun. 2018 a las 14:59

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.