nginx and fast-cgi – tips and tricks

How to pass full path to fast-cgi app

It’s necessary to configure which parameters are passed to fast-cgi application from nginx server. For this purposes serves fastcgi_param¬†keyword. It’s a good practice to keeps all fastcgi params together. For this purposes nginx has fastcgi.conf file. This file already contains most of useful fastcgi_param bindings.

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;

To include this file use include statement:

location / {
   include fastcgi_params;

How to pass custom headers to fast-cgi app

Default configuration pass only default headers to fast-cgi. It’s necessary to add few more configurations statements:

fastcgi_pass_request_headers on;

After that headers are passed as HTTP_* parameters:


External links

How to run fastcgi and nginx on windows

Step by step guide how to configure nginx on windows….

Download and installation

Download nginx package from nginx site. Unpack it and installation is done ūüėČ

Example of fast-cgi application

No create and complie fast-cgi application. (How to compile fast-cgi on windows)

    #include "fcgi_stdio.h"
    #include <stdlib.h>

    void main(void)
        int count = 0;
        while(FCGI_Accept() >= 0)
            printf("Content-type: text/html\r\n"
                   "<title>FastCGI Hello!</title>"
                   "<h1>FastCGI Hello!</h1>"
                   "Request number %d running on host <i>%s</i>\n",
                    ++count, getenv("SERVER_NAME"));

How to connect fast-cgi with web-server

There are two ways. When web-server supports fast-cgi you will be able to connect fast-cgi directly with web-server via configuration files. In all other cases fast-cgi offers cgi-fcgi  executable as bridge between webserver and your app. More info about both these approaches you can find here

Working way how to get fastcgi apps working

None of two ways described in previous article and in official documentation works for me on windows. I wasn’t able to get¬†cgi-fcgi or spawn-fcgi¬†running. First mentioned crashed everytime app tries to initialize STD_OUT, the second one isn’t possible to compile it because of missing¬†unistd.h header.

Fortunately there is one more way. FCGI library offers one more way how to utilize FCGI mechanism. It’s necessary add two more lines to example above and everything works perfectly.

    #include "fcgi_stdio.h"
    #include <stdlib.h>

    void main(void)
        //initialize library & open listening socket at port 9345

        //rest is the same as in example above
        while(FCGI_Accept() >= 0) ...

After this change application initialize listening port and waits for incoming data. All you need to do is directly execute the app.

Configure nginx to connect with fast-cgi

This step compared to previous ones is pretty easy. You need to tell nginx server what and where to pass. Open file nginx.conf and add following statement:

http {
  server {
    location / {

fastcgi_pass statement forward all requests to localhost:9345 address. Now when you start your nginx server, everything should work as expected.

nginx server cfg


bind() to failed

This error is caused by another application which uses port 80. In my case it was Skype which automatically uses port 80.

nginx: [emerg] bind() to failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

External links

How to compile FastCGI library in Visual Studio 2010 C++

FastCGI comes with Win32 project for VisualStudio6. Unfortunately when you try to open it in VisualStudio2010 the project will be corrupted.¬†To correctly compile it for VS2010 it’s necessary to download library sources, get patches from site and apply it.

Download library

Download latest version of Development Kit here and extract it to standalone directory.


Get and update patches

Download all five patches from Cybozu site. Now when you have patches it’s necessary to convert all files to correct windows-ending format. This can be done for example in Notepad++.

Convert to windows EOL

Get patch.exe application

Now you need some application capable to apply the patches. Unfortunately windows doesn’t contain any application for this task and also lot of comparing/merging applications can’t do that.

The simplest way is to download windows conversion of linux patch.exe app. Get it from gnuwin32 site Рpatch for windows.

Apply patch to project files

Apply patches

Open directory with fastcgi library, copy all patch files to this directory and open command line. As next run patch command for all fives patches:

...\fcgi-2.4.1-SNAP-0311112127>patch -Np1 < 4-vc9.patch
patching file Win32/FastCGI.sln
patching file Win32/authorizer.vcproj
patching file Win32/cgifcgi.vcproj
patching file Win32/config_h.vcproj
patching file Win32/echo-cpp.vcproj
patching file Win32/echo.vcproj
patching file Win32/echox.vcproj
patching file Win32/libfcgi.vcproj
patching file Win32/logdump.vcproj
patching file Win32/size.vcproj
patching file Win32/threaded.vcproj

In case you will see following error message:

patching file Win32/FastCGI.sln
Assertion failed: hunk, file ../patch-2.5.9-src/patch.c, line 354

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

it’s necessary to convert ALL patch files to correct Windows format (correct EOL) as mentioned in previous section.

More patches…

When you apply patches by cybozu it’s time to apply my own patches¬†;-).

Manually updated rejected file

As first, it’s probably that you will see following message:

1 out of 26 hunks FAILED -- saving rejects to file libfcgi/os_win32.c.rej

It’s because original patch is for older version of library and file os_win32.c¬† is slightly different. To fix that, edit this file at line 841 and manually update following line:

int len = p - bindPath + 1;

//updated line
intptr_t len = p - bindPath + 1;

Update VS project file

Now turn off generating the Map Files in project configuration. These files aren’t necessary and it isn’t possible to compile project correctly with this settings.

Turn off .map files


And that’s all. Now you will be able to sucesfully compile fast-cgi library.

Compilation complete

External links

FastCGI c++ library for all platforms (Windows, Mac and Linux)

List of existing libraries


Stackoverflow articles


Other links

CGICC – how to update it for VS2010 COMPILATION

  • ¬†error C2668: ‘cgicc::copy_if’ : ambiguous call to overloaded function –¬†add¬†cgicc:: to all¬†cgicc::copy_if instances
  • ¬†error LNK2019: unresolved external symbol –¬†add compiled .lib path (…Debug\cgicc.lib) to all dependend projects