使用nginx php-fpm和ubuntu随机502网关错误

时间:2021-08-08 06:36:53

I was having an issue with random 502 gateway errors using nginx and php-fpm. In my case I discovered a scenario where various php.ini error_log settings and error_reporting levels were causing the random 502 gateway errors to appear.

使用nginx和php-fpm我遇到了随机502网关错误的问题。在我的情况下,我发现了一个场景,其中各种php.ini error_log设置和error_reporting级别导致出现随机502网关错误。

By changing the php.ini settings error_reporting and error_log I was able to make the 502 gateway errors disappear - but more importantly I was able to see what the real php errors were in the log and fix them.

通过更改php.ini设置error_reporting和error_log,我能够使502网关错误消失 - 但更重要的是,我能够看到日志中真正的php错误并修复它们。

The main issue was that if "error_reporting was set to display notices, "error_logging = On" then I needed make sure that error_log was set to a valid path tat was writable to the server.

主要问题是,如果“error_reporting设置为显示通知”,则“error_logging = On”然后我需要确保将error_log设置为有效路径,这对于服务器是可写的。

// !!! 502 Gateway Error (unhappy server)
error_reporting = E_ALL & E_NOTICE
; error_log = php_errors.log (note this is commented)

// Happy Server, no 502 gateway error
error_reporting = E_ALL & E_NOTICE
error_log = /valid/log/path/and/permissions

// Happy Server, no 502 gateway error
error_reporting = E_CORE_ERROR
; error_log = php_errors.log (note this is commented)

Note, the actual errors were php notices... however ngingx was throwing 502 gateway errors for php notices that were related to properties not being set.

注意,实际的错误是php通知...但是ngingx正在为与未设置的属性相关的php通知投掷502网关错误。

Also note, that 502 gateway errors were not being triggered on every php notice.

另请注意,每个php通知都没有触发502网关错误。

Hope this saves someone some frustration!

希望这可以节省一些人的挫败感!

4 个解决方案

#1


3  

502 gateway errors in Nginx are caused by php-fpm not having enough process and/or timeouts. Logging only tells you what the issues are and are not the cause of 502 errors.

Nginx中的502网关错误是由php-fpm没有足够的进程和/或超时引起的。记录仅告诉您问题是什么,而不是502错误的原因。

I use stunnel+haproxy+nginx+php-fpm on 25 servers. The defaults in pfp-fpm are very low, even for a moderately busy server. Configure the fpm child processes much the same way you would with apache mod_php.

我在25台服务器上使用stunnel + haproxy + nginx + php-fpm。 pfp-fpm的默认值非常低,即使对于中等繁忙的服务器也是如此。配置fpm子进程的方式与使用apache mod_php的方式非常相似。

I use the following:

我使用以下内容:

pm.max_children = 250
pm.start_servers = 20
pm.min_spare_servers =10
pm.max_spare_servers = 20
pm.max_requests = 1500

This is a server with 2GB ram. It serves 20-30GB traffic per day with no 502's

这是一台2GB内存的服务器。它每天提供20-30GB流量,没有502

#2


0  

I've run into this on windows machines running iis and php in fastCGI mode so it isn't just a nix problem.

我在使用快速CGI模式运行iis和php的Windows机器上遇到这个问题,所以这不仅仅是一个nix问题。

The culprit was php logging as well. You don't have to set it to a static directory though if you grant proper permissions it can write to the same directory that the script resides in. (Can be very helpful if you have a large and complicated site)

罪魁祸首是php日志记录。您不必将其设置为静态目录,但如果您授予适当的权限,它可以写入脚本所在的同一目录。(如果您有一个庞大而复杂的站点,则非常有用)

My server is setup this way and I just added a rule to not serve the error logs to the public.

我的服务器是这样设置的,我刚刚添加了一条规则,不向公众提供错误日志。

To sum it up, CHECK PERMISSIONS! =)

总结一下,检查许可! =)

#3


0  

I have a similar problem that resolved by disabling PHP warnings, however, I think the problem has something to do with NGINX configuration of the buffers which the error logging uses. The error only triggers after a certain number of warning messages are queued, and seems to be quite predictably repeatable; but I can't figure out what settings are involved so have shut off the warnings until I can resolve the settings that are broken.

我有一个类似的问题,通过禁用PHP警告解决,但是,我认为该问题与错误记录使用的缓冲区的NGINX配置有关。该错误仅在一定数量的警告消息排队后触发,并且似乎是可预测的可重复的;但我无法弄清楚涉及哪些设置,所以关闭警告,直到我可以解决被破坏的设置。

#4


0  

If above steps don't help you solve your issue, check if you have eaccelerator enabled.

如果上述步骤无法帮助您解决问题,请检查您是否启用了eaccelerator。

Disable it globally by disabling the load .so entry in either your php.ini or php.d/eaccelerator.ini

通过在php.ini或php.d / eaccelerator.ini中禁用load .so条目来全局禁用它

All problems are gone. Seems like the accellerator fails in certain situations in combination with php-fpm.

所有问题都消失了。似乎加速器在某些情况下与php-fpm结合失败。

#1


3  

502 gateway errors in Nginx are caused by php-fpm not having enough process and/or timeouts. Logging only tells you what the issues are and are not the cause of 502 errors.

Nginx中的502网关错误是由php-fpm没有足够的进程和/或超时引起的。记录仅告诉您问题是什么,而不是502错误的原因。

I use stunnel+haproxy+nginx+php-fpm on 25 servers. The defaults in pfp-fpm are very low, even for a moderately busy server. Configure the fpm child processes much the same way you would with apache mod_php.

我在25台服务器上使用stunnel + haproxy + nginx + php-fpm。 pfp-fpm的默认值非常低,即使对于中等繁忙的服务器也是如此。配置fpm子进程的方式与使用apache mod_php的方式非常相似。

I use the following:

我使用以下内容:

pm.max_children = 250
pm.start_servers = 20
pm.min_spare_servers =10
pm.max_spare_servers = 20
pm.max_requests = 1500

This is a server with 2GB ram. It serves 20-30GB traffic per day with no 502's

这是一台2GB内存的服务器。它每天提供20-30GB流量,没有502

#2


0  

I've run into this on windows machines running iis and php in fastCGI mode so it isn't just a nix problem.

我在使用快速CGI模式运行iis和php的Windows机器上遇到这个问题,所以这不仅仅是一个nix问题。

The culprit was php logging as well. You don't have to set it to a static directory though if you grant proper permissions it can write to the same directory that the script resides in. (Can be very helpful if you have a large and complicated site)

罪魁祸首是php日志记录。您不必将其设置为静态目录,但如果您授予适当的权限,它可以写入脚本所在的同一目录。(如果您有一个庞大而复杂的站点,则非常有用)

My server is setup this way and I just added a rule to not serve the error logs to the public.

我的服务器是这样设置的,我刚刚添加了一条规则,不向公众提供错误日志。

To sum it up, CHECK PERMISSIONS! =)

总结一下,检查许可! =)

#3


0  

I have a similar problem that resolved by disabling PHP warnings, however, I think the problem has something to do with NGINX configuration of the buffers which the error logging uses. The error only triggers after a certain number of warning messages are queued, and seems to be quite predictably repeatable; but I can't figure out what settings are involved so have shut off the warnings until I can resolve the settings that are broken.

我有一个类似的问题,通过禁用PHP警告解决,但是,我认为该问题与错误记录使用的缓冲区的NGINX配置有关。该错误仅在一定数量的警告消息排队后触发,并且似乎是可预测的可重复的;但我无法弄清楚涉及哪些设置,所以关闭警告,直到我可以解决被破坏的设置。

#4


0  

If above steps don't help you solve your issue, check if you have eaccelerator enabled.

如果上述步骤无法帮助您解决问题,请检查您是否启用了eaccelerator。

Disable it globally by disabling the load .so entry in either your php.ini or php.d/eaccelerator.ini

通过在php.ini或php.d / eaccelerator.ini中禁用load .so条目来全局禁用它

All problems are gone. Seems like the accellerator fails in certain situations in combination with php-fpm.

所有问题都消失了。似乎加速器在某些情况下与php-fpm结合失败。