IIS 6.0 - FastCGI

CGI application were discouraged on IIS webservers because for every request a new host process had to be spawned raising questions about performance.

The FastCGI protocol is set to change this. The concept is simple instead of creating a new host process for each request and killing it ...reuse a bunch(or single) of processes to handle the requests.

You gain significant performance improvement by reusing the process instead of killing it and creating a new one.

How do I start ?

You can download the installer here

FastCGI Extension for Internet Information Services 6.0

INSTALLATION

The installation is pretty simple. It basically copies three files to your \system32\inetsrv folder. The files are

fcgiconfig.js       - Script file that can be use to configure FastCGI settings(you can change configurations manually as well)

fcgiext.dll          - The ISAPI extension that helps in passing requests to the CGI process.

fcgiext.ini         - The settings file that controls the behavior of  the FastCGI handler (If you do not feel like using fcgiconfig.js you have to tweak this file)

It also adds an entry in IIS Web Service Extensions "FastCGI Handler" and enables it.

In case you are uninstalling FastCGI extension note that the ini file is left intact on the server. In future if you reinstall FastCGi you may find some settings already configured because of this.

CONFIGURATION

FCGICONFIG.JS is the configuration script that you use to configure FastCGI settings (You can always do it manually as well)

Using fcgiconfig.js will give you a first hand experience on how changing IIS 7 configuration from command line will look.

fcgicongif

cscript fcgiconfig.js -add -section:"PHP" - extension:php -path: "C:\php-5.2.5-nts-Win32\php-cgi.exe"

(You can use the -site switch if you want to configure only one website)

What this does is it puts an ISAPI extensions mapping for .php and points it to the fcgiext.dll

isapiextension

It also adds entries in the fcgiext.ini file mapping to tell the fcgi handler  which cgi executable is mapped to the .php extension.

[Types]
php=PHP

[PHP]
ExePath=C:\php-5.2.5-nts-Win32\php-cgi.exe

It then recycles the apppools in case of IIS 6.0 or restarts the service in IIS 5.0

If you have multiple mappings say for PHP and RoR then in IIS both the extensions will be mapped to fcgiext.dll but in the fcgiext.ini we will specify which executable to use to handle the requests.

The above image shows a clear difference as to how a CGI application configured with FastCGI looks. If you notice I even have the perl cgi mapped to ".pl" in the above image. It is mapped as CGI. But in case of ".php" it is mapped to the FastCGI handler(fcgiext.dll) which now in turn will invoke the "php-cgi.exe" as required.

So for every "*.pl" request a new instance of perl.exe process is created but for ".php" requests the process(es) is reused.

By default the FastCGI handler creates a pool of 10 CGI processes. This can be tweaked using the MaxInstances parameter in the ini file. The fcgiext.ini can be used to tweak the following parameters Protocol / MaxInstances / QueueLength / IdleTimeout / ActivityTimeout / RequestTimeout / InstanceMaxRequests / ResponseBufferLimit / FlushNamedPipe / UnhealthyOnQueueFull / IgnoreExistingFiles / IgnoreExistingDirectories

So now when a request is made for a php file IIS hands off the request to the fcgiext.dll ISAPI extension. The FastCGI handler then checks the association in the [Types] section. It then spawns the corresponding CGI process and hands off the request to the executable. Once the request is processed and returned back the process is not killed but kept alive till IdleTimeout (300 sec default). If another request for a php file is made the same process is reused.

Bookmark and Share