valet: 504 Gateway Timeout

I think laravel/valet should provide more relaxed defaults for a development environment. Importing a large(ish) database in phpmyadmin causes a 504 Gateway Time-out error.

To fix the 504 error you need to change the default timeout to more than 30 seconds. Here’s how to change it to 5min (300 seconds):

  1. /usr/local/etc/php/{version}/conf.d/php.ini
max_execution_time = 300
  1. https://github.com/laravel/valet/blob/master/cli/Valet/PhpFpm.php#L57
function updateConfiguration()
{
    $contents = $this->files->get($this->fpmConfigPath());

    $contents = preg_replace('/^user = .+$/m', 'user = '.user(), $contents);
    $contents = preg_replace('/^group = .+$/m', 'group = staff', $contents);
    $contents = preg_replace('/^listen = .+$/m', 'listen = '.VALET_HOME_PATH.'/valet.sock', $contents);
    $contents = preg_replace('/^;?listen\.owner = .+$/m', 'listen.owner = '.user(), $contents);
    $contents = preg_replace('/^;?listen\.group = .+$/m', 'listen.group = staff', $contents);
    $contents = preg_replace('/^;?listen\.mode = .+$/m', 'listen.mode = 0777', $contents);
    // Enable the "request_terminate_timeout" parameter
    $contents = preg_replace('/^;?request_terminate_timeout = .+$/m', 'request_terminate_timeout = 300', $contents);

    $this->files->put($this->fpmConfigPath(), $contents);
}
  1. https://github.com/laravel/valet/blob/master/cli/stubs/valet.conf#L25
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:VALET_HOME_PATH/valet.sock;
    fastcgi_index VALET_SERVER_PATH;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME VALET_SERVER_PATH;
    # Set the "fastcgi_read_timeout" parameter
    fastcgi_read_timeout 300;
}
  1. https://github.com/laravel/valet/blob/master/cli/stubs/secure.valet.conf#L34
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:VALET_HOME_PATH/valet.sock;
    fastcgi_index VALET_SERVER_PATH;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME VALET_SERVER_PATH;
    # Set the "fastcgi_read_timeout" parameter
    fastcgi_read_timeout 300;
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 62
  • Comments: 40 (9 by maintainers)

Most upvoted comments

@bkkrishna I have solved my issue adding

client_header_timeout 3000;
client_body_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffers 8 128k;
fastcgi_buffer_size 128k;

to ~/usr/local/etc/nginx.confg file under http {} object.

Then restart the services valet restart

For those who are stuck with 5 concurrent max requests:

/usr/local/etc/php/7.1/php-fpm.d/www.conf
pm.max_children = 200

If you want to boost the perf:

pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
pm.max_requests = 500
sudo brew services restart php71

Valet just installs nginx via brew (unless it is already installed). You can always modify the configs under /usr/local/etc/nginx/. If you have used valet secure on the site then there is also a site specific nginx config created under ~/.valet/Nginx/

Thanks @AbhinavDobhal

Putting them in /usr/local/etc/nginx/fastcgi_params works well too, since it’s a central location and valet loads that file from the same block you mentioned. I recently started exploring using the following to test a suggestion made elsewhere for cookie payload size issues.

/usr/local/etc/nginx/fastcgi_params

...

# Very large cookies/headers may throw errors without the following (extremely generous) settings:
fastcgi_buffer_size 4096k;
fastcgi_buffers 128 4096k;
fastcgi_busy_buffers_size 4096k;

# Long-running/slow services
fastcgi_read_timeout 600;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;

非常感谢楼主,对于一直没有解决问题的小伙伴看好了. 直接修改 /usr/local/etc/nginx/valet/valet.conf 在差不多22行左右

   location ~ \.php$ {
        #下面这行是新加的,加好就好了 哈哈 
        fastcgi_read_timeout 300; 
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

当然php.ini里也是要设置好的 php71的配置在 /usr/local/etc/php/{version}/php.ini 不要偷懒,输入phpinfo就能看到配置文件地址. 改好后记得要重启valet

# Very large cookies/headers may throw errors without the following (extremely generous) settings:
fastcgi_buffer_size 4096k;
fastcgi_buffers 128 4096k;
fastcgi_busy_buffers_size 4096k; 
# Long-running/slow services
fastcgi_read_timeout 600;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;

Added a new fastcgi_params inside ~/.config/Valet/Nginx with these details and did a valet restart and that worked. Thanks!

HI all,

I have been experiencing the same issue. I followed the same solution as @simonhamp.

For anyone else still having trouble, I specifically edited /usr/local/etc/php/7.3/php-fpm.d/www.conf

With the following updates:

pm.max_children = 200
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
pm.max_requests = 500

See https://dev.to/aubreypwd/how-i-configured-niginx-valet-to-stop-the-504-gateway-time-out-during-long-xdebug-sessions-174i


How I solved this

Added:

    proxy_connect_timeout 1200;
    proxy_read_timeout 1200;
    proxy_send_timeout 1200;
    fastcgi_read_timeout 1200;
    fastcgi_send_timeout 1200;

in /opt/homebrew/etc/nginx/nginx.conf, right before:

    include "/Users/.../.config/valet/Nginx/*";
    include servers/*;
    include valet/valet.conf;

I spent a lot of time trying to find ~/.valet/Nginx, turns out in newer versions of valet it is under ~/.config/Valet/Nginx. 😃

Solving this issue is easy: just open the file /usr/local/etc/nginx/valet/valet.conf and add the following lines to the block location ~ .php$ { } : proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; send_timeout 600; fastcgi_read_timeout 300; fastcgi_buffers 8 128k; fastcgi_buffer_size 256k;

Now all you have to do is to run on your valet restart terminal and everything should work fine.

PHP 7.3 is available on homebrew.

If everything is saying bad gateway then it’s likely that your PHP is the problem.

A complete reset of valet (er, well, the nginx/php part anyway; I’m assuming dnsmasq is resolving the .test TLD properly) can be done via:

valet stop
brew services list  # (should show both nginx and php as stopped)
brew uninstall php nginx
brew update
rm -rf /usr/local/etc/nginx /usr/local/etc/php
rm -rf ~/.config/valet
composer global update
valet install
valet start

I’ve got the same issue, but much more frequent when I make multiple requests. However,

  1. /usr/local/etc/php/{version}/conf.d/php.ini I don’t have php.ini. I do have php-memory-limits.ini instead. Should I create php.ini or set in the php-memory-limits.ini?

2/3/4 - Couldn’t find those files, any clue?

$ valet -V
Laravel Valet version 2.0.5
$ php -v
PHP 7.1.8 (cli) (built: Aug  7 2017 15:02:45) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
$ ls /usr/local/etc/php/7.1/conf.d/
php-memory-limits.ini 

Nice tip @jasperf. That and an entry in php.ini helped a long-running script run to completion.

Closing this issue as it seems there are myriad solutions presented but no forward movement for their impact on Valet; @ctf0 if you’d like to continue that proposal, would you please open it as a separate (and more fleshed-out) issue?

  1. Which files should be changed on an existing/working Valet installation? Something in ~/.valet ?

  2. Is it possible to disable Timeout completely (I use Valet on my notebook, not on server, so I an stop any processes myself, if need to)?