6
votes

Here is my vhost file:

 <VirtualHost *:80>
   ServerName awesome.dev

   ## Vhost docroot
   DocumentRoot "/var/www/awesome"

   ## Directories, there should at least be a declaration for /var/www/awesome
   <Directory "/var/www/awesome">
     Options Indexes FollowSymLinks MultiViews
     DirectoryIndex index.php
     AllowOverride All
     Require all granted
   </Directory>

   ## Logging
   ErrorLog "/var/log/apache2/w0JhArMoDehc_error.log"
   ServerSignature Off
   CustomLog "/var/log/apache2/w0JhArMoDehc_access.log" combined

   ## Server aliases
   ServerAlias www.awesome.dev

   ## SetEnv/SetEnvIf for environment variables
   SetEnv APP_ENV dev

   ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/awesome/$1
 </VirtualHost>

I'm trying to catch all requests for non-existing *.php files.

For example, if /var/www/awesome/index.php exists and I go to http://foo.com/index.php I get the correct response, but if /var/www/awesome/foo.php does not exist and I go to http://foo.com/foo.php, I am simply getting a response of File not found..

The .htaccess file isn't being read because Apache hands everything off to PHP-FPM.

I need to catch all 404 requests and show a common error page, as you would normally see on any site.

However, since Apache hands everything off to php-fpm, it doesn't seem to be handling these errors properly.

3

3 Answers

3
votes

I used to have the same problem and finally I fixed it.

Try add this after ProxyPassMatch setting:

ProxyErrorOverride on

BTW, do not forget your

ErrorDocument 404 /path/to/file

setting.

0
votes

As many problems can occur during apache / php-fpm process, many errors can lead to the response

"File not found" and in logs "AH01071: Got error 'Primary script unknown\n'": (double slashes in paths, permissions,...)

To track them you can:

  • Put in your apache configuration "LogLevel debug" and check error log.
  • And/Or revert temporary your configuration to "simple apache only try", in my case it lead me to permissions problems (www 0751 needed to be 0755) error wich was invisible before.

Ps: Take care on a other thread, people says that using ProxyErrorOverride for that is "really a bad idea": Server Fault | Apache 2.4 + PHP-FPM + ProxyPassMatch

0
votes

For anyone reading today, HERE is the correct answer, thanks to Tito1337 for his answer.

ProxyErrorOverride may give you problems or break your application if you set 404's or handle some errors elsewhere in your code, and is more complicated to implement.

Instead, you should pass the request to php-fpm only if the file exists. If the file does not exist, Apache will direct to your defined ErrorDocument. You can add this check around your PHP handler in the Apache config.

Example for CentOS 8:

#
# Redirect to local php-fpm (no mod_php in default configuration)
#
<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    # Enable http authorization headers
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(php|phar)$>
        
        # NEW ADDITION - CHECK IF FILE EXISTS FIRST
        <If "-f %{REQUEST_FILENAME}">
            
            SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
            
        </If>
        
    </FilesMatch>
  </IfModule>
</IfModule>