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"
                   "\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Ā http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm#S4.

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
        FCGX_Init();
        FCGX_OpenSocket(":9345",500);

        //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 127.0.0.1:9345;
     }
  }
}

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

Troubleshooting

bind() to 0.0.0.0:80 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 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

External links

Leave a Reply

Your email address will not be published. Required fields are marked *