Php – Nginx 502 Bad Gateway. Solved by increasing buffer. Why

fastcginginxPHP

I'm in the process of setting up a LEMP stack to run Drupal. I installed Nginx and PHP-FastCGI.

Nginx worked fine but any attempts to run PHP gave me the error "502 Bad Gateway".

A quick Google revealed: nginx 502 bad gateway, and increasing the buffer size solved the problem.

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;

The question is why?

My understanding

From the previous link, it would seem that nginx was sending requests to PHP-FastCGI and it wasn't responding. What about these requests made it time out?

Did it not have enough time to respond because the php was complex (it wasn't, it was phpinfo();). Now I've increased the buffer, when should I worry about having to increase the buffer again?

Best Answer

If you will check nginx error log, most probably you will see this message:
upstream sent too big header while reading response header from upstream

fastcgi_buffers sets the number and memory size of buffer segments used for the response of the FastCGI upstream.

Default values, presented in documentation:
fastcgi_buffers 8 4k|8k;
where default buffer size is equal to the PAGESIZE of your operating system.
getconf PAGESIZE allows to get current memory page size.

For example, in Ubuntu 14.01, default PAGESIZE is 4KB. That means, you have 8 segments, 4KB each. Total is 32KB. Response of FastCGI is more than this number, that is the reason, why we get response code 502 - server received

It is not great explanation, but it will help you to understand better, I hope.

Related Topic