Topic: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Hello,

Following the instructions at http://blog.famillecollet.com/post/2014 … mprovement , I have configured my Apache/PHP-FPM installation like this, where example.com is the domain for the site and each site has its own PHP-FPM process, and this code is in a VirtualHost for the site being represented:

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

That works fantastic. It doesn't have all this issues of ProxyPassMatch. However, I recently encountered the situation where I need to set a higher timeout for Apache. I have a script that takes a few minutes to execute, and I want to enable that without getting a 503 Timeout/Service Unavailable. Currently, Apache sends a 503 after 30 seconds. I have confirmed that it is not PHP-FPM that is terminating the request (PHP continues to execute the request in the background).

I did some research and tried all of the following methods. None of the have worked. Apache still sends a 503 response after 30 seconds with each of these different "solutions".

#1:

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

#2:

<Proxy "unix:/var/run/php-fpm/example.com.sock|fcgi://localhost" timeout=300>
   ProxySet timeout=300
</Proxy>

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

#3:

TimeOut 300
ProxyTimeout 300

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

Do you have any proposed solution to this problem? I'd really like to keep using SetHandler instead of reverting back to ProxyPassMatch.

Thanks in advance!

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Interesting... never encounter (heard of) this issue.

Have you try ProxyTimeout inside the <Proxy> definition ?

# Proxy declaration
<Proxy "unix:/var/run/php-fpm/example.com.sock|fcgi://php-fpm-example">
    ProxyTimeout 300
</Proxy>

# Redirect to the proxy
<FilesMatch \.php$>
    SetHandler proxy:fcgi://php-fpm-example
</FilesMatch>

Notice, if you use a proxy block, you don't have to set the sock path in the SetHandler directive.

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Please also try with a network socket, I'm not sure timeout is properly managed for UDS.

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Have talk with an apache dev.

http://svn.apache.org/viewvc/httpd/http … rkup#l2798

Timeout is not set in the UDS case, see the if (conn->uds_path).
This will be fixed upstream.

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Hey Remi,

Well, the ProxyTimeout isn't valid inside <Proxy> directive (I tried, and Apache complained). However, your note about not duplicating the socket definition solved my problem! Here's what works:

<Proxy "unix:/var/run/php-fpm/example.com.sock|fcgi://localhost">
   ProxySet timeout=300
</Proxy>

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

Once I removed that duplicate socket path, the timeout in the Proxy directive worked. I guess Apache was ignoring the <Proxy> directive because I defined the socket directly in SetHandler. I'm not sure.

Whatever the issue was, thank you very much for helping me fix that. It was driving me crazy! Thanks again.

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

smile

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Which distro / apache versions are you running ?

Config #3 is working, with only ProxyTimeout (for me)

Config #1 is wrong because SetHandler have no timeout option

Config #2 si wrong because there is timeout in the name of the proxy and sock path should not be used in SetHandler

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

More tests:
It works with httpd 2.4.12 / 2.4.16 in Fedora, but doesn't work with httpd-2.4.6-31 in RHEL/CentOS 7.

Related to bug https://bugzilla.redhat.com/show_bug.cgi?id=1222328
(I can't say when the update will be available, but the bug is ON_QA).

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70

Re: How do I set timeout when using Apache and PHP-FPM via SetHandler?

Tested with httpd-2.4.6-40.el7.x86_64 (RHEL-7.2), it works (only ProxyTimeout)

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 24 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.8 x86_64 + EPEL + remi + remi-php70