Protocol support
FastCGI
Available since
- HAProxy 2.1
- HAProxy Enterprise 2.1r1
- HAProxy ALOHA 12.0
A variation on the earlier Common Gateway Interface (CGI), FastCGI’s main objective is to reduce the overhead related to interfacing between a web server and CGI programs, thus allowing a server to handle more web page requests in less time. The load balancer can send HTTP requests directly to Responder FastCGI applications, bypassing the web server.
This feature implements all the capabilities of the FastCGI specification for the Responder application, including the ability to multiplex several requests on a single connection.
Set up FastCGI Jump to heading
-
Add a
fcgi-appsection to your load balancer configuration. This section declares how to connect to the FastCGI application.haproxyfcgi-app php-fpmlog-stderr globaloption keep-conndocroot /var/www/my-appindex index.phppath-info ^(/.+\.php)(/.*)?$haproxyfcgi-app php-fpmlog-stderr globaloption keep-conndocroot /var/www/my-appindex index.phppath-info ^(/.+\.php)(/.*)?$In this example:
log-stderr globalsends FastCGI error messages to the log destination specified in theglobalsectionoption keep-connkeeps the connection open to the application after receiving the responsedocroot /var/www/my-appis the directory that contains your PHP files on the remote serverindex index.phpis the name of the PHP script to call if no other file name is given in the request URLpath-info ^(/.+\.php)(/.*)?$is the regular expression that extracts the PHP file name from the request URL
-
Add a
backendsection that lists the servers hosting the FastCGI application:haproxybackend fcgi-serversmode httpfilter fcgi-app php-fpmuse-fcgi-app php-fpmserver s1 192.168.0.10:9000 proto fcgiserver s2 192.168.0.11:9000 proto fcgihaproxybackend fcgi-serversmode httpfilter fcgi-app php-fpmuse-fcgi-app php-fpmserver s1 192.168.0.10:9000 proto fcgiserver s2 192.168.0.11:9000 proto fcgiIn this example:
filter fcgi-appline refers to thefcgi-appsection you defined previouslyuse-fcgi-apprefers to thefcgi-appsection you defined previously- Each
serverline includes theproto fcgiargument
-
Route requests for dynamic content to this backend. For example, the following
frontendsection uses theuse_backenddirective to route PHP requests to the FastCGI servers:haproxyfrontend wwwmode httpbind :80use_backend fcgi-servers if { path_end .php }default_backend static-file-servershaproxyfrontend wwwmode httpbind :80use_backend fcgi-servers if { path_end .php }default_backend static-file-servers
Connect via a UNIX socket Jump to heading
If your FastCGI script is running on the load balancer itself, you can connect to it through a UNIX socket, in addition to TCP/IP.
Specifying a socket means that the FastCGI application runs locally and the load balancer needs to be able to access this socket. The group directive in the global section adds the load balancer to the group that owns the socket, which in our example is www-data.
haproxygloballog /dev/log local0user haproxy# PHP-FPM application's UNIX socket is owned by the www-data groupgroup www-databackend php_serversuse-fcgi-app php_fpmserver s1 /run/php/myapp.sock proto fcgi
haproxygloballog /dev/log local0user haproxy# PHP-FPM application's UNIX socket is owned by the www-data groupgroup www-databackend php_serversuse-fcgi-app php_fpmserver s1 /run/php/myapp.sock proto fcgi
See also Jump to heading
- For complete information on the specification of a FastCGI application, see the FastCGI reference.
Do you have any suggestions on how we can improve the content of this page?