Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am migrating my server to use mod_proxy_fcgi and php-fpm instead of mod_php. Apache is able to forward .php requests to the fcgi proxy and PHP executes correctly. I've got this working with:

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://$1

Unfortunately, Apache forwards all .php requests to the proxy, even when the file doesn't exist. This causes a few problems. My ErrorDocument rule isn't invoked, and DirectoryIndex index.php index.html doesn't fall back to index.html.

I was able to fix these problems with mod_rewrite:

RewriteEngine On                                                           
RewriteCond %{REQUEST_FILENAME} ^/((.*\.php)(/.*)?)$                       
RewriteCond /var/www/html/%2 -f                                   
RewriteRule . fcgi:// [P] 

However, the Apache documentation does not recommend RewriteRule: "This is because this flag triggers the use of the default worker, which does not handle connection pooling."

Ideally, I think I'd either like to use ProxyPass in a FilesMatch block (currently unsupported), or define a new handler that proxies through fcgi and use it to handle .php requests, similar to what mod_php does.

Any suggestions for simulating a standard mod_php setup but actually proxying through fcgi?

share|improve this question

One option is to install mod_proxy_handler:

Or you can wait for Apache 2.4.10, which should include the module.

Basically the module lets you do this:

<FilesMatch \.php$>
SetHandler proxy:fcgi://localhost:9000

<FilesMatch \.php$>
    SetHandler "proxy:unix:/path/to/socket.sock|fcgi://./"
share|improve this answer

Ditto to the OP.

  • I also couldn't figure out how to get Apache VHOST config to route non-findable requests to a 404.
  • Similarly, I was also stymied by trying to figure out how to get extension-less URL's to map throuh ProxyPassMatch.
    • Sure... I could route everything through the php-fpm proxy, but that killed javascript and css files.

Theoretical Solution

My guess is that I'd have to write a custom 404.php processor -- and then route all requests through that appending the requested file(s) as a query string.

For example:

RewriteRule ^.*$   fcgi://{DOCUMENT_ROOT}/404.php?no_comprende=%{REQUEST_URI} [P]
ProxyPassMatch ^/(.*\.php(/.*)?)$    fcgi://{DOCUMENT_ROOT}/$1

in theory -- and assuming that the order in which they are listed still applies as normal:

Any 404'd file would be caught before the ProxyPassMatch directive was reached


  • php5-fpm (5.4.23)
  • Apache 2.4.2
  • Ubuntu 12.04 / 3.8.0-34 x86_64

To handle extension-less URLs with php-fpm I adapted the above to look for extension-less URL's as such in my VHOST config:

# Extensionless URL's
RewriteCond %{REQUEST_FILENAME} ^/((.*)(/.*)?)$
RewriteCond %{DOCUMENT_ROOT}/%2.php -f
RewriteRule !.*\.php$   fcgi://{DOCUMENT_ROOT}/%1.php   [P]

# files w/ .php extensions
RewriteCond %{REQUEST_FILENAME} ^/((.*\.php)(/.*)?)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule . fcgi://{DOCUMENT_ROOT}/%1 [P]

Not pretty, but it does get the job done :-(

share|improve this answer

Just for the record as a one liner:

AddHandler "proxy:unix:/path/to/socket.sock|fcgi://./" .php

You'll need a recent Apache 2.4 (RedHat back ported that to 2.4.6)


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.