Kassem
Kassem

Reputation: 63

How to make Apache check if PHP file exists before passing it to PHP-FPM?

I get the following error "No input file specified." instead of a file not found when I request a non-existent PHP file.

So is it possible to make Apache check if PHP file exists before passing it to the PHP-FPM server to avoid "No input file specified." error? or that error is normal/harmless?

This is how Apache is configured to handle PHP files:

<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php5-fpm.sock.example|fcgi://localhost"
</FilesMatch>

Upvotes: 6

Views: 4777

Answers (3)

Tito1337
Tito1337

Reputation: 126

I dislike using the RewriteEngine for that task. Here is the correct configuration to use a SetHandler directive, while checking that the PHP file actually exists :

<FilesMatch "\.(php|php[57]|phtml)$">
    <If "-f %{REQUEST_FILENAME}">
            SetHandler "proxy:fcgi://127.0.0.1:9000"
    </If>
</FilesMatch>

Source : PHP-FPM

Upvotes: 11

By default, all requests for a PHP script (even if the script don't exists on the filesystem) will be passed in the proxy_fcgi handler.. One way to fix this issue (Apache >= 2.4.10 required) is to put the following in your Apache2 vhost file:

# define worker
<Proxy "unix:/var/run/php5-fpm-domain.tld.sock|fcgi://domain.tld" retry=0>
    ProxySet connectiontimeout=5 timeout=30
</Proxy>

RewriteEngine On
RewriteOptions Inherit

<FilesMatch \.ph(p[3457]?|t|tml)$>
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .* - [H=proxy:fcgi://domain.tld,NC]
</FilesMatch>

Here, we set the proxy_fcgi handler only if the file exists. The PATH_INFO is correctly handled too. Note that we must inherite rewrite rules (case of rewrite rules that are stored in .htaccess file).

Upvotes: 0

covener
covener

Reputation: 17886

Here's a mod_rewrite (virtual host context) alternative recipe that only sets the Handler if the file exists.

RewriteEngine ON
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} -f
RewriteRule \.php$ - [H=proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/]

But it would not work with PATH_INFO as-is because it would check for the file+path info under the document root

Upvotes: 0

Related Questions