Load balancing
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-app
section 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 global
sends FastCGI error messages to the log destination specified in theglobal
sectionoption keep-conn
keeps the connection open to the application after receiving the responsedocroot /var/www/my-app
is the directory that contains your PHP files on the remote serverindex index.php
is 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
backend
section 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-app
line refers to thefcgi-app
section you defined previouslyuse-fcgi-app
refers to thefcgi-app
section you defined previously- Each
server
line includes theproto fcgi
argument
-
Route requests for dynamic content to this backend. For example, the following
frontend
section uses theuse_backend
directive 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
.
haproxy
globallog /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
haproxy
globallog /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
Do you have any suggestions on how we can improve the content of this page?