Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

isimmons's avatar

homestead nginx increase fastcgi buffer and timeouts when provisioning

I've started having issues since first using L5. Many times I will get a 500, 504, or 502 error page from nginx when the problem is actually something like a typo or class not found or something where I should see the whoops error page.

I tried increasing the fastcgi buffers and timeouts as suggested here as solution 1 http://www.nginxtips.com/502-bad-gateway-using-nginx/ and it works every time.

The problem is I loose this any time I destroy and re-create the vm so I need to add this change in my homestead/scripts/homestead.rb

Anyone know how to write the following to etc/nginx/nginx.conf inside the http block at the top?

http {
        ##
        # Custom fast_cgi to avoid 502/504
        ##
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;

        ... other settings here
}

Or another way if there is an easier way to set these.

Thanks

0 likes
8 replies
noeldiaz's avatar
Level 23

One idea comes to mind, copy the actual final nginx.conf and modify the rb file to copy it over. Just like it does with the other files like aliases, etc. I'm doing that right now but with a custom my.cnf file for MySQL where I tune some of the settings. I copy the final version of the file on provision and then restart the service.

isimmons's avatar

Ah didn't think of that. Much easier than trying to write to the existing file. Thanks

intrepidws's avatar

@isimmons Do you have a good understanding of why the fastcgi buffer fix works and what is specifically does to prevent the 504/402 errors? I put the following in place, which fixed the problem temporarily:

fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;

This fixed the problem, but only for about 24 hours - then it started back up again. I have since put the following in place:

fastcgi_buffer_size 32k;
fastcgi_buffers 8 16k;

While this is working, I'm hoping to get a better understanding of how this whole thing works so I can figure out if this too will stop working in 24 hours.

bashy's avatar

@intrepidws I think it's to do with the amount of child processes you have available for php-fpm. Maybe you have them setup lower than you need.

intrepidws's avatar

@bashy Know of a good way for me to determine if this is the case, before changing it?

intrepidws's avatar

@bashy Really appreciate your help. I actually increased the max_children number from 5 to 25 and then changed back the buffer settings to what they were previously - so far no 502/504 errors.

My PHP-FPM logs don't have any errors actually. The errors are showing up in my site logs as follows (I starred out some domains, etc):

2016/01/04 08:30:30 [error] 896#0: *66902 connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: ***.***.***.***, server: ***.com, request: "GET /dashboard/play HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "***.com"

And

2016/01/04 08:35:18 [error] 896#0: *67198 upstream timed out (110: Connection timed out) while reading response header from upstream, client: ***.***.***.***, server: ***.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock", host: "***.com"

That seem consistent with an error that might be caused by max_children being too low?

bashy's avatar

@intrepidws Ah okay, could well be that! You have to set it up for your server (CPU cores/RAM etc).

Please or to participate in this conversation.