Custom 404 message when using PHP-FPM with Apache
Asked Answered
T

3

10

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?

Tman answered 21/11, 2015 at 13:15 Comment(0)
D
9

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

Durante answered 26/2, 2016 at 22:4 Comment(3)
Unfortunately, this option is available in version 2.3.10 and later and the system in question runs version 2.2.Tman
not according to the 2.2 documentation httpd.apache.org/docs/2.2//mod/…Durante
Works on Apache 2.4Emigrant
E
4

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
Elongation answered 20/12, 2019 at 19:29 Comment(1)
You only need "systemctl reload apache2" (not restart)Sheliasheline
S
1

Option: ProxyErrorOverride

ProxyErrorOverride can be used if you have access to the server's configuration. But it doesn't work within the .htaccess context and it will prevent PHP from outputting dynamic response bodies for all configured status codes (default: 400 to 599).

Option: <If> directive (Apache 2.4+)

Let Apache check if the file exists, before invoking PHP:

<Files "*.php">
    <If "-e %{REQUEST_FILENAME}">
        # Assuming PHP-FPM over Unix socket via mod_proxy_fcgi.
        SetHandler proxy:unix:/path/to/php-fpm.sock|fcgi://localhost/
    </If>
    <Else>
        # Ensure that *.php files are never handled by the default handler.
        Redirect 404
    </Else>
</Files>

Docs: <If>, Expression parser, Redirect

Option: mod_rewrite

The ErrorDocument can be triggered using mod_rewrite:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "\.php$" - [R=404]

Note: This only works if REQUEST_FILENAME has already been determined (so not in the server config or virtual host, but in a directory or .htaccess context). Otherwise it is equal to REQUEST_URI and that probably wouldn't be an existing local file.

Stepfather answered 20/5, 2022 at 13:56 Comment(1)
This worked for me without needing access confStipulation

© 2022 - 2024 — McMap. All rights reserved.