php Nginx 502 错误网关。通过增加缓冲区解决。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19539501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:40:33  来源:igfitidea点击:

Nginx 502 Bad Gateway. Solved by increasing buffer. Why?

phpnginxfastcgi

提问by Dominic Woodman

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

我正在设置一个 LEMP 堆栈来运行 Drupal。我安装了 Nginx 和 PHP-FastCGI。

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

Nginx 运行良好,但任何运行 PHP 的尝试都会给我错误“502 Bad Gateway”。

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

一个快速的谷歌透露:nginx 502 bad gateway,增加缓冲区大小解决了这个问题。

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?

从上一个链接来看,nginx 似乎正在向 PHP-FastCGI 发送请求,但它没有响应。这些请求如何让它超时?

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?

它没有足够的时间来响应,因为 php 很复杂(它不是,它是phpinfo();)。现在我已经增加了缓冲区,我什么时候应该担心不得不再次增加缓冲区?

回答by antonbormotov

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

如果您检查 nginx 错误日志,您很可能会看到以下消息:
upstream sent too big header while reading response header from upstream

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

fastcgi_buffers设置用于响应 FastCGI 上游的缓冲区段的数量和内存大小。

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

文档中提供的默认值:
fastcgi_buffers 8 4k|8k;
其中默认缓冲区大小等于操作系统的 PAGESIZE。
getconf PAGESIZE允许获取当前内存页面大小。

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

例如,在 Ubuntu 14.01 中,默认 PAGESIZE 为 4KB。这意味着,您有 8 个段,每个段为 4KB。总共是 32KB。FastCGI的响应比这个数字多,这就是为什么我们得到响应码的原因502 - server received

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

这不是很好的解释,但它会帮助你更好地理解,我希望。

回答by Danila Vershinin

Actually, the problem is directly related only to fastcgi_buffer_size. This is a very special buffer that holds only the HTTP headers from response.

实际上,该问题仅与 直接相关fastcgi_buffer_size。这是一个非常特殊的缓冲区,它只保存响应中的 HTTP 标头。

If your application emits a lot of Set-Cookieheaders (or something else contributing to the total size of HTTP headers), the default buffer size here may not be sufficient and you need to increase it.

如果您的应用程序发出大量Set-Cookie标头(或其他导致 HTTP 标头总大小的东西),则此处的默认缓冲区大小可能不够,您需要增加它。

To understand howyou need to increase it, you can read my super detailed writeup here- it's about proxy_buffer_sizebut fastcgi_buffers behave very similarly. To quote the essential command:

要了解如何你需要增加它,你可以阅读我的超详细的书面记录在这里-这是关于proxy_buffer_sizefastcgi_缓冲区表现非常相似。引用基本命令:

curl -s -w \%{size_header} -o /dev/null https://example.com

Ensure to test against proper URL and add request headers via -H, if needed.

-H如果需要,请确保针对正确的 URL 进行测试并通过 添加请求标头。

This will give you the header size in bytes. You will then need to align resulting value to 4k (typical size of memory page).

这将为您提供以字节为单位的标头大小。然后,您需要将结果值与 4k(内存页的典型大小)对齐。

So if you got, e.g. 14342 bytes, then it's required to set:

所以如果你得到,例如 14342 字节,那么它需要设置:

fastcgi_buffer_size 16k;

The tricky part is not there, but rather in the fact that when you increase this buffer size, you need to increase either fastcgi_buffer_sizeand/or fastcgi_busy_buffers_sizeas welldue to the way NGINX uses/calculates the default value for the latter.

棘手的部分不在那里,而是事实上,当您增加此缓冲区大小时,由于 NGINX 使用/计算后者的默认值的方式,您需要增加fastcgi_buffer_size和/或fastcgi_busy_buffers_size增加。

Either way, don't set those buffers too high and use calculations specific to your app. Arbitrarily high values won't do good to your RAM, because those buffers are used per connection.

无论哪种方式,都不要将这些缓冲区设置得太高,并使用特定于您的应用程序的计算。任意高的值对您的 RAM 没有好处,因为每个连接都会使用这些缓冲区。