Monkey HTTP Server v1.5

FastCGI

FastCGI Plugin is the client side implementation of the FastCGI protocol for interfacing interactive programs with a HTTP Server like Monkey.

In the old fashion mechanism called CGI, to trigger the execution of a script or a binary program, the Server requires to spawn a new process context, this new process is in charge to execute the target file or run it through an interpreter, this is a very expensive procedure at Operating System level. As opposite, FastCGI implements a client/server model where the FastCGI server side, make sure the target resource program is running in an infinite loop and will only by called upon request, when done instead of exit, the program is started again inside the same process context, avoiding process creation waste. This is usually provided by a third party program and for different Script languages exists many FastCGI server implementations.

Using FastCGI is highly recommended when running scripting web programs such as Python, PHP, Perl or any other.

Enable Plugin

To enable the FastCGI plugin, please follow the steps mentioned on Plugins section. The plugin name is monkey-fastcgi.so, so make sure the plugin entry is Load and the absolute path is correct.

Configuring

The FastCGI plugin takes a global setup that affect all Virtual Hosts at once on the configuration file conf/plugins/fastcgi/fastcgi.conf. The configuration schemas defines two available sections: [FASTCGI_SERVER] and [FASTCGI_LOCATION]:

FASTCGI_SERVER

The section named FASTCGI_SERVER aims to represent a FastCGI backend server that is listening for incoming FastCGI requests. It supports four main keys: ServerName, ServerAddr, ServerPath and MaxConnections.

key description
ServerName Local name to represent the FastCGI server.
ServerAddr If the FastCGI Server is listening on a TCP connection, use this key to specify as value the Host IP and target TCP port, e.g: 192.168.0.4:9000.
ServerPath Use this key if the FastCGI Server is listening in a local Unix socket file.
MaxConnections Set the maximum number of open connections to the FastCGI Server. This number is a total across Monkey workers.

Note: You may use ServerAddr or ServerPath, not both.

FASTCGI_LOCATION

This section aims to trap a HTTP request that targets a specific URL location. So when a match occurs, the FastCGI plugin redirect the requests to the FASTCGI_SERVER associated on this Location definition. The section supports four main keys: LocationName, ServerNames, KeepAlive and Match:

key description
LocationName Local name to represent this location
ServerNames Space separated list of FastCGI Servers (required)
KeepAlive Keep FastCGI server connections alive. Make sure to tune the MaxConnections on [FASTCGI_SERVER] directives and the Worker count on [SERVER] in conf/monkey.conf to avoid threads stall if available concurrent connections is less than the number of workers. Boolean value, it accepts on or off (default: off)
Match Space separated list of regular expression.

Configuration Example for PHP-FPM

The following example aims to provide a setup to make Monkey FastCGI plugin talk to a FastCGI Server that process PHP scripts: php5-fpm.

[FASTCGI_SERVER]
    ServerName     php5-fpm1
    ServerPath     /var/run/php5-fpm.sock
    MaxConnections 20

[FASTCGI_LOCATION]
    LocationName   php5_location
    ServerNames    php5-fpm1
    KeepAlive      On
    Match          /*.php

This example will pass the control of each request to a file ending in .php to the FastCGI server running on the Unix socket /var/run/php5-fpm.sock file.