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

devinfd's avatar

502 Bad Gateway with "large" file uploads

I was getting a 413 Request Entity Too Large error when uploading files so I changed upload_max_filesize to 10M and post_max_size to 10M. I've also added client_max_body_size 10M; to /etc/nginx/nginx.conf. And before you ask it, yes I have reloaded and restarted nginx. But now I am getting 502 errors and I can figure out why. When I look at the nginx/default-error.log it says:

recv() failed (104: Connection reset by peer) while reading response header from upstream, ...

The file I'm trying to upload is only 5M so this should be rather simple, but of course it's not.

0 likes
7 replies
austenc's avatar

It could because it's hitting the fastcgi timeout, I've had a similar problem. Basically add the below to the http section of your /etc/nginx/nginx.conf file to see if it fixes it:

fastcgi_read_timeout 300;

Then restart nginx. Hope that helps, cheers!

devinfd's avatar

I appreciate the help but unfortunately that did not resolve it.

austenc's avatar

Darn, thought it might be something easy... maybe you've tried this too, but after some googling it looks like it might be a php-fpm opcache issue?

Try removing the comment from this line in /etc/php5/fpm/php.ini

opcache.enable=0

Then restart nginx and cross your fingers!

1 like
devinfd's avatar

Yep, I've tried that too. no joy. I've now wasted 4 hours on this and I'm about to loose it. I honestly can not understand what the problem is. Thanks for trying though.

1 like
devinfd's avatar
devinfd
OP
Best Answer
Level 13

For all those hopeless soles out there also experiencing the "recv() failed (104: Connection reset by peer)" issue, I was only able to resolve this by increasing request_terminate_timeout in /etc/php5/fpm/pool.d/www.conf

I had originally set fastcgi_read_timeout to 300 and I guess those two settings need to be in sync.

The dark beauty of this industry is that to learn something, you must first break it.

9 likes

Please or to participate in this conversation.