How to Fix “Primary script unknown” Error in Apache 2.4 with PHP-FPM ProxyPassMatch Configuration


1 views

When setting up Apache 2.4 with PHP-FPM using ProxyPassMatch, a common issue arises when accessing directories that should default to index.php. The configuration:


    ServerName localhost
    DocumentRoot "/Users/apfelbox/WebServer"
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1

works fine for direct PHP file requests but fails for directory access with the error:

[proxy_fcgi:error] AH01071: Got error 'Primary script unknown\n'

The issue occurs because:

  1. Apache's DirectoryIndex directive tries to serve index.php
  2. The request gets internally rewritten to index.php
  3. ProxyPassMatch doesn't handle this internal rewrite properly

This configuration handles both direct PHP requests and directory indexes:


    ServerName localhost
    DocumentRoot "/Users/apfelbox/WebServer"
    
    DirectoryIndex index.php index.html
    
    
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    
    
    RewriteEngine On
    # Handle directory index files first
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.+/)?index\.php$ - [L]
    
    # Pass PHP files to FPM
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.+\.php)(/.*)?$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1 [P,L]
    
    # Handle directory index fallback
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1/index.php -f
    RewriteRule ^(.*?)/?$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1/index.php [P,L]

For cases where you need PATH_INFO support (like index.php/test/):

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.+\.php)(/.+)?$
RewriteRule ^(.+\.php)(/.*)?$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1$2 [P,L]

When using mod_rewrite instead of ProxyPassMatch:

  • Enable mod_rewrite logging during debugging with RewriteLogLevel 3
  • Consider adding cache-control headers for static assets
  • For production, combine this with opcache for PHP

When setting up PHP-FPM with Apache's ProxyPassMatch directive, a common issue arises when accessing directories rather than direct PHP files. The error manifests as:

[proxy_fcgi:error] AH01071: Got error 'Primary script unknown\n'

This occurs because:

  • Directory requests (like http://localhost/) trigger DirectoryIndex evaluation
  • The request gets internally rewritten to index.php but the original ProxyPassMatch doesn't handle path translation correctly
  • PHP-FPM receives an invalid script path

The original ProxyPassMatch configuration:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Users/apfelbox/WebServer"
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1
</VirtualHost>

Works for direct PHP file access but fails for directory requests. Let's examine why mod_rewrite solutions struggled:

Attempt 1: Basic RewriteRule

RewriteEngine on    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1 [L,P]

This fails because it doesn't account for DirectoryIndex rewriting happening afterwards.

Attempt 2: File Existence Check

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1 [L,P]

The -f check fails because it tests against the original request URI before DirectoryIndex processing.

After several iterations, this configuration handles both direct PHP requests and directory access:

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Users/apfelbox/WebServer"
    
    <Directory "/Users/apfelbox/WebServer">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    # Handle PHP files directly
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
    
    # Fallback for directory requests
    DirectoryIndex index.php index.html
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
</VirtualHost>

1. The SetHandler approach is more reliable than ProxyPassMatch for PHP-FPM
2. DirectoryIndex properly triggers for directory requests
3. mod_rewrite handles clean URLs and missing files
4. Full absolute paths ensure PHP-FPM receives correct script paths

For path info URLs (index.php/test/), ensure your PHP application handles PATH_INFO server variable correctly.

Enable these logs when troubleshooting:

LogLevel debug rewrite:trace6 proxy_fcgi:trace

Check PHP-FPM's access.log and slow.log for additional debugging information about received requests.