FastCGI - How to run fastcgi and nginx on windows

注:該博文轉自 How to run fastcgi and nginx on windows,由於網上FastCGI相關的資料較少,故轉載存檔。原文章創作於2013年,原文中部分鏈接資料已經失效,並且有少量的顯示錯誤,現已更正,方便之後查閱。如有侵權,請聯繫刪除。
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 &lt;stdlib.h&gt;
 
void main(void)
{
    //initialize library &amp; 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.
在這裏插入圖片描述

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

  • http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/
  • https://code.google.com/p/heeds/wiki/HEEDS_FastCGI_nginx
  • http://stackoverflow.com/questions/1945293/how-can-fastcgi-applications-be-started-manually-on-windows
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章