Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom 404 message when using PHP-FPM with Apache

I have Apache (2.2.22 on Debian) configured to handle PHP files via FastCGI:

<FilesMatch ".+.php$">

SetHandler application/x-httpd-php

</FilesMatch>

Action application/x-httpd-php /fcgi-bin/php5-fpm virtual Alias

/fcgi-bin/php5-fpm /fcgi-bin-php5-fpm FastCgiExternalServer

/fcgi-bin-php5-fpm -socket /var/run/php5-fpm.sock -idle-timeout 600 -pass-header Authorization

To show a custom File Not Found (HTTP 404) page is configured in Apache as follows:

<Directory "/home/http/domain/root">

..

ErrorDocument 404 /pagenotfound.htm

..

</Directory>

Requests for non-existing non-PHP files are answered with the custom 404 pagenotfound.htm file. No problem.

But requests for non-existing PHP files are answered with http-status-header "HTTP/1.1 404 Not Found" and contents "File not found.", so not my custom error page. Problem!

The Apache error log shows (in the latter case):

[Sat Nov 21 14:03:07 2015] [error] [client xx.xxx.xx.xx] FastCGI: server "/fcgi-bin-php5-fpm" stderr: Primary script unknown

How can I configure a custom 404 page for non-existing PHP files when using PHP-FPM?

like image 851
coret Avatar asked Nov 21 '15 13:11

coret


2 Answers

set "ProxyErrorOverride on" in either your global server config or in individual virtual hosts, see http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxyerroroverride

like image 130
Alan Birtles Avatar answered Sep 24 '22 14:09

Alan Birtles


When 'File not found' is shown instead of custom error page for non-existing .php files (and all other non-existing files get the correct custom error page)...

Centos 8, PHP 7.2.11 File: /etc/httpd/conf.d/php.conf

Add 'ProxyErrorOverride On' after the SetHandler

<FilesMatch \.(php|phar)$>
    SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    ProxyErrorOverride On
</FilesMatch>

Not sure if required, but I then did:

    systemctl restart httpd
like image 30
Danny Avatar answered Sep 22 '22 14:09

Danny