Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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://127.0.0.1:9000/var/www/html/$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://127.0.0.1:9000/var/www/html/%1 [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
add comment

2 Answers

One option is to install mod_proxy_handler: https://gist.github.com/progandy/6ed4eeea60f6277c3e39

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

Basically the module lets you do this:

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

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

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:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-l
RewriteRule ^.*$   fcgi://127.0.0.1:9000%{DOCUMENT_ROOT}/404.php?no_comprende=%{REQUEST_URI} [P]
...
ProxyPassMatch ^/(.*\.php(/.*)?)$    fcgi://127.0.0.1:9000%{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

MY SOLUTION

  • 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://127.0.0.1:9000%{DOCUMENT_ROOT}/%1.php   [P]

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

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

share|improve this answer
add comment

Your Answer

 
discard

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.