Rewriting URLs while using mod_proxy_fcgi and PATH_INFO ends up with error 404
Solution 1:
After few days of struggle, I have captured and examined CGI headers sent between Apache and PHP-FPM and tested the same also with different versions of said components. It turns out that different versions of Apache set SCRIPT_FILENAME
variable differently which is unaccounted for in PHP-FPM.
Apache 2.4.18 (default in Ubuntu 16.04 which I use) populates the variable as follows:
SCRIPT_FILENAME proxy:fcgi://localhost/srv/www/index.php
Whereas Apache 2.4.25 sets:
SCRIPT_FILENAME /srv/www/index.php
Apache documentation of mod_proxy_fcgi mentions a directive ProxyFCGIBackendType available since Apache 2.4.26 (not released yet as of 2017-05-02), which defaults to "FPM". There is a following note in the description:
One example of values that change based on the setting of this directive is SCRIPT_FILENAME. When using mod_proxy_fcgi historically, SCRIPT_FILENAME was prefixed with the string "proxy:fcgi://". This variable is what some generic FastCGI applications would read as their script input, but PHP-FPM would strip the prefix then remember it was talking to Apache. In 2.4.21 through 2.4.25, this prefix was automatically stripped by the server, breaking the ability of PHP-FPM to detect and interoperate with Apache in some scenarios.
Seems I have coincidentally found "some scenario". Answer to my own question is then: Do not use Apache 2.4.21 through 2.4.25 as they contain a bug. Use either lower version or wait for newer where the bug is fixed.