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

  1. Add a fcgi-app section to your load balancer configuration. This section declares how to connect to the FastCGI application.

    haproxy
    fcgi-app php-fpm
    log-stderr global
    option keep-conn
    docroot /var/www/my-app
    index index.php
    path-info ^(/.+\.php)(/.*)?$
    haproxy
    fcgi-app php-fpm
    log-stderr global
    option keep-conn
    docroot /var/www/my-app
    index index.php
    path-info ^(/.+\.php)(/.*)?$

    In this example:

    • log-stderr global sends FastCGI error messages to the log destination specified in the global section
    • option keep-conn keeps the connection open to the application after receiving the response
    • docroot /var/www/my-app is the directory that contains your PHP files on the remote server
    • index index.php is the name of the PHP script to call if no other file name is given in the request URL
    • path-info ^(/.+\.php)(/.*)?$ is the regular expression that extracts the PHP file name from the request URL
  2. Add a backend section that lists the servers hosting the FastCGI application:

    haproxy
    backend fcgi-servers
    mode http
    filter fcgi-app php-fpm
    use-fcgi-app php-fpm
    server s1 192.168.0.10:9000 proto fcgi
    server s2 192.168.0.11:9000 proto fcgi
    haproxy
    backend fcgi-servers
    mode http
    filter fcgi-app php-fpm
    use-fcgi-app php-fpm
    server s1 192.168.0.10:9000 proto fcgi
    server s2 192.168.0.11:9000 proto fcgi

    In this example:

    • filter fcgi-app line refers to the fcgi-app section you defined previously
    • use-fcgi-app refers to the fcgi-app section you defined previously
    • Each server line includes the proto fcgi argument
  3. Route requests for dynamic content to this backend. For example, the following frontend section uses the use_backend directive to route PHP requests to the FastCGI servers:

    haproxy
    frontend www
    mode http
    bind :80
    use_backend fcgi-servers if { path_end .php }
    default_backend static-file-servers
    haproxy
    frontend www
    mode http
    bind :80
    use_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
global
log /dev/log local0
user haproxy
# PHP-FPM application's UNIX socket is owned by the www-data group
group www-data
backend php_servers
use-fcgi-app php_fpm
server s1 /run/php/myapp.sock proto fcgi
haproxy
global
log /dev/log local0
user haproxy
# PHP-FPM application's UNIX socket is owned by the www-data group
group www-data
backend php_servers
use-fcgi-app php_fpm
server 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?