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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值