venerdì 9 ottobre 2009

How to share data between different HTTP requests

An easy way to share data between different HTTP requests of the same FastCGI is declaring variables outside the main loop of the FastCGI. The following FastCGI, a simple web counter, use this technique to maintain into an integer the count of accesses:

#include "fcgi_stdio.h"
int main( int argc, char *argv[] )
int c = 1;
while( FCGI_Accept() >= 0 )
printf( "Content-Type: text/plain\r\n\r\n" );
printf( "%d\n", c );
return 0;

Assuming that the source has been named webcounter.c and application has been compiled (see this post) as webcounter into the /var/fastcgi directory, it’s necessary to add the following lines to the /etc/lighttpd/conf-available/10-fastcgi.conf file:

fastcgi.server += ( "/fastcgi/webcounter" =>;
"bin-path" => "/var/fastcgi/webcounter",
"max-procs" => 1,
"socket" => "/tmp/webcounter.socket",
"check-local" => "disable"

then, after restarting lighttpd, we can try the counter at the URL http://ip-of-your-server/fastcgi/webcounter.

Only two notes about the configuration file: the “+” in the first added line before the “=” and the line “max-procs” => “1”, that force lighttpd to execute only one instance of the FastCGI to avoid malfunctions.

References: Structure and options of the FastCGIs’ configuration file

Nessun commento:

Posta un commento