Forwarding PHP requests via ProxyPassMatch as a handler, or only when file exists
Solution 1
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>
Solution 2
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)
Oliver
Solution 3
I had same issue when file did not existed was showing "File not found." message , this fixed my issue and allowed me to setup a 404 page :
<VirtualHost *:80>
---------- content --------
DocumentRoot /home/user/public_html/domain.tld
#this disables php execution if you wish to show only html files
#ProxyPass /errors !
ProxyErrorOverride On
# /errors folder is located in public_html
ErrorDocument 404 /errors/404.php
</VirtualHost>
Related videos on Youtube
![ide](https://i.stack.imgur.com/E3LaP.jpg?s=256&g=1)
ide
Updated on September 18, 2022Comments
-
ide almost 2 years
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?
-
mcdado over 6 yearsYAEEES! I lost an entire day to trial and error, what solved the issue for me was using the #tcp block above. Using
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/usr/local/var/www/$1
does not work withVirtualDocumentRoot
s, and the rewriting trick never worked for me. -
mcdado over 6 yearsAlso, it's important to note that
proxy:fcgi://localhost:9000
does not want the path after it. This was a big time misunderstanding for me. -
mcdado over 6 yearsFYI I wrote this up in a blog post: mcdado.com/post/166619749589/apache-and-php-fpm-via-homebrew
-
8ctopus about 4 yearsMake sure that php-fpm is listening using a socket instead of a TCP port as otherwise it won't work (displays the php code in the browser). The php-fpm listen config should look like
listen = /var/run/php-fpm7.sock