php: PHP-FPM stderr logs cause nginx to return 502 errors to client
I have php-fpm running on docker with the following php.ini:
memory_limit = 512M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
ignore_repeated_errors = On
html_errors = Off
upload_max_filesize = 5M
post_max_size = 50M
max_input_vars = 5000
max_execution_time = 60
error_log = /var/log/php/error.log
log_level = error
catch_workers_output = yes
Everything worked and nginx was proxying perfectly. The problem with this is logs are never shown in the container’s stdout, so I changed it to:
error_log = /proc/self/fd/2
which meant that I could see the logs with docker logs -f, but I started seeing these errors on nginx:
2019/08/26 17:29:35 [error] 22#0: *6 FastCGI sent in stderr: "PHP message: PHP Deprecated: Function create_function() is deprecated in
So some plugin in my wordpress (and I know which one it is), is using a deprecated function, but this error is non critical, its actually a notice. The problem is that with this setup, I keep getting 502 Bad Gateway errors when a client requests a page. I actually also see that php-fpm is returning them via the fast cgi protocol, when I connect do it directly.
It all starts working when I set log_errors = Off. So I read a bit and realized the fast-cgi protocol can return a FCGI_STDERR packet to its client, which includes error logs, so I decided not to go down that rabbit hole and try to cheat by doing this:
ln -sf /dev/stderr /var/log/php/error.log
to make php-fpm think it was logging to a file and still have the logs show up in docker logs. No such luch, 502 only disappear is I log to /var/log/php/error.log without any connection to stdout/stderr or if I disable logging altogether.
I don’t know if its my poor understanding or if this is a bug, but there should be a way to log to stdout/stderr without returning errors to nginx.
Can anyone help?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 15 (4 by maintainers)
Commits related to this issue
- php.ini added important comment https://github.com/docker-library/php/issues/878 — committed to airstarh/alina by airstarh 6 months ago
- Docker fix default fastcgi.logging Mistake from https://github.com/RSS-Bridge/rss-bridge/pull/3500 Wrong file extension: should have been `.ini` and not `.conf` otherwise it has no effect. See https:/... — committed to Alkarex/rss-bridge by Alkarex 6 months ago
- Docker fix default fastcgi.logging (#3875) Mistake from https://github.com/RSS-Bridge/rss-bridge/pull/3500 Wrong file extension: should have been `.ini` and not `.conf` otherwise it has no effect. ... — committed to RSS-Bridge/rss-bridge by Alkarex 6 months ago
Hello everyone 👋
TL;DR: set
fastcgi.logging = Off
in yourphp.ini
Long explanation:
I know I’m late to the party, but this also means I’ve the advantage of having more google results available and I’m quite confident I found the real fix for what was reported by OP:
It was suggested that the
log_limit
“fixes” the problem, but this is only a accidental workaround which others also thought.It is true, in case the fastcgi stderr channel contains data and is too big, nginx will act on this, this is the source code thereover where this happens in nginx https://github.com/nginx/nginx/blob/c9c3f2f005c43fd885bd78f86e1044261e639bea/src/http/modules/ngx_http_fastcgi_module.c#L1746-L1762
nginx has a setting to control this behaviour and it’s called
fastcgi_next_upstream
So theoretically this behaviour can also be mitigated by configuring nginx to not act that way on fastcgi errors being too large.
But the real answer is from this stackoverflow question:
And this is IMHO the correct fix: set
fastcgi.logging
off in your php.iniWhat happens is:
error_log = On
and, in container environments, sends it to/dev/stdout
or similarfastcgi.logging
beingOn
means this is additionally send upstream, to nginx for many it seemscatch_workers_output = yes
, theerror_log
to stdout in PHP is already good/enoughThat’s why people also see the PHP errors (warnings, notices, etc.) twice:
Disabling does the desired thing:
In my case I was processing a GraphQL call which triggered an exception; the exception was caught inside the framework correctly and turned into a proper GraphQL JSON response. But the exception was also logged deliberately and what happen then is:
Personally (though I’m not an expert on this topic), since we’re talking about the “official docker PHP” images here, I’d say it would make very much sense to either:
HTH and have a nice day!
Thanks, I’ve seen this, but this is poor reasoning, in my opinion. In fact, he admits it wasn’t changed in PHP due to backwards compatibility. I think it should keep the PHP default and you can choose to override if you want, not the other way around. At the very least it should be documented that this was bumped and you will need to take that into account when pairing with nginx, because literally everyone using nginx will run into this problem at some point.
I’ve finally implemented https://github.com/docker-library/php/issues/878#issuecomment-938595965 over in #1360, but I don’t plan to let it merge until at least the new year (I’ve had a tab open to this issue for over a year now, intending to implement something 😩).
I am facing a similar issue, once too many notices come out of a request. We get a 502 from nginx.
I tried changing the following things: (in /usr/local/etc/php-fpm.d/docker.conf)
To
To
To
However, it did not solve the issue.
Instead of
/proc/self/fd/2
, try/proc/1/fd/2
(if that works, we should probably consider adjusting our default configuration too).