原文:http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/

上面的连接地址给出的是作者的原文地址。

另外一个作者稍微整理了一下,github 地址:https://github.com/homer6/fastcgi

下面给出我测试的截图:

这个用 C++ 写的cgi 程序哦, c++ 也可以写网站了,是不是很兴奋!!!

stackoverflow 的问题:http://stackoverflow.com/questions/2149709/c-language-fastcgi-with-nginx

俗话说:“好记性不如烂笔头”, 所以这里记录下原文地址,和稍微写一写自己的心得和一路的过程。

google 到这篇用 c++ (c)语言写的 fastCGi 程序,并能成功运行,中间实属不易。。

做 web (网站) 开发也有两三年了, 用 php 语言写的。 web server 用过 apache , nginx .  对于apache ,它会启动一个 php 的模块,来处理php脚本。 对于nginx,

则 fast-pass: 127.0.0.1:9000 , webserver (nginx)将 请求  ----cgi 协议--- 转发到 facscgi (php-fpm) 处理(当然 会传递 标准输入和一个 环境变量过去),php 解释器执行完

php 脚本后, 将标准输出  给webserver , webserver 然后将输出 通过 http response 传给 browser (client), 一个 http 的请求和响应就结束了。

cgi 程序可以用php, java, C#写,当然可以用 C, C++ 来写。

我一直也在研究一个 fastCGI 这个处理过程到底是怎么样的。 webserver 和 fastCGI 之间的交互过程是怎么样的。所以自己想写一个 C++ 的fastCGI 脚本来完成一个http 请求,于是我也找了很多资料,所以就有了这篇文章的产生。

关于 CGI 应用程序和 web server 之间的交互的过程,这里有几张图片,图片来源:(原作者:吴秦Tyler)  https://images0.cnblogs.com/blog/92071/201412/191104473136735.png

prerequistites:

  need some familiarity with C++;

Why fastCGI?

  FCGI (FastCGI) is a protocol in which web applications can talk to a web server to serve web requests.

Installation:

  We will need to install the libfcgi++ library, a web server (nignx in our case) and spawn-fcgi to run the fcgi app. We’ll also install curl to help test later on.

  

sudo apt-get install libfcgi-dev
sudo apt-get install spawn-fcgi
sudo apt-get install nginx
sudo apt-get install curl

  

The code:

  a c++ source file(echo.cpp)

  

#include <iostream>
#include "fcgio.h" using namespace std; int main(void) {
// Backup the stdio streambufs
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf(); FCGX_Request request; FCGX_Init();
FCGX_InitRequest(&request, 0, 0); while (FCGX_Accept_r(&request) == 0) {
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err); cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf); cout << "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>Hello, World!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Hello, World!</h1>\n"
<< " </body>\n"
<< "</html>\n"; // Note: the fcgi_streambuf destructor will auto flush
} // restore stdio streambufs
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf); return 0;
}

  

Nginx config:

  

Next up we’ll setup nginx to listen on port 80 for http requests and forward those along the the fcgi process which will listen on port 8000.

The key directive is fastcgi_pass 127.0.0.1:8000 indicates that nginx should forward the fcgi request to port 8000 at localhost. You can replace the address with an upstream directive if you want to want to load balance across many processes. The rest of the fastcgi_param directives are optional and just set appropriate environment variables which get forwarded to the fcgi application.

events {
worker_connections 1024;
} http {
server {
listen 80;
server_name localhost; location / {
fastcgi_pass 127.0.0.1:8000; fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
}
}
}

  

Running the code:

  The system now comprises of three parts. One will be the executable fcgi c++ app. In order to run this we need spawn-fcgi and to listen to a port. Nginx will then forward web requests to this port, translating the http proctocol into the fast cgi protocol.

# run nginx using the provided configuration
# 下面这一行其实我没用到。
sudo nginx -c <path to nginx.conf> # compile hello_world
g++ main_v1.cpp -lfcgi++ -lfcgi -o hello_world # spawn the fcgi app on port 8000 with no fork
spawn-fcgi -p 8000 -n hello_world

  Then just head to the browser and enter http://localhost/ into the location bar and voilà! Hello, World! See the next part in this tutorial to see how to incorporate the request uri and the request content into this simple app

用c++写一个 “hello,world” 的 FastCGI程序的更多相关文章

  1. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...

  2. 写一个带文本菜单的程序,菜单项如下 (1) 取五个数的和 (2) 取五个数的平均值 (X) 退出。

    问题: 写一个带文本菜单的程序,菜单项如下(1)    取五个数的和 (2)     取五个数的平均值(X)    退出. 由用户做一个选择, 然后执行相应的功能.当用户选择退出时程序结束. 实现: ...

  3. 郑晔谈 Moco 框架的开发:写一个好的内部 DSL ,写一个表达性好的程序

    作者:张龙 出处:http://www.infoq.com/cn/news/2013/07/zhengye-on-moco 郑晔谈Moco框架的开发:写一个好的内部DSL,写一个表达性好的程序 作者  ...

  4. CBrother脚本10分钟写一个拯救“小霸王服务器”的程序

    CBrother脚本语言10分钟写一个拯救“小霸王服务器”的程序 到了一家新公司,接手了一坨c++服务器代码,到处内存泄漏,这服务器没有数据库,挂了后重启一下就好了,公司就这么凑活着用了几年了,定时重 ...

  5. 使用python写一个最基本的mapreduce程序

    一个mapreduce程序大致分成三个部分,第一部分是mapper文件,第二个就是reducer文件,第三部分就是使用hadoop command 执行程序. 在这个过程中,困惑我最久的一个问题就是在 ...

  6. 用java代码写一个简单的网上购物车程序

    需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修 ...

  7. php 如何写一个自己项目的安装程序

    版权声明:此篇文章只是用作笔记,如果版权冲突,请邮件通知一下(15201155501@163.com) https://blog.csdn.net/shenpengchao/article/detai ...

  8. 用qpython3写一个发送短信的程序

    用qpython3写一个最简单的发送短信的程序 用qpython3写一个最简单的发送短信的程序到目前为止并没有多少手机应用是用python开发的,不过qpython可以作为一个不错的玩具推荐给大家来玩 ...

  9. 用go写一个简单的看门狗程序(WatchDog)

    简述 因为公司的一些小程序只是临时使用一下(不再维护更新),有的有一些bug会导致崩溃,但又不是很严重,崩溃了重新启动一下就好. 所以写了一个看门狗程序来监控程序,挂了(因为我这里并不关心程序的其他状 ...

随机推荐

  1. 7.在第4题中Hello.class所在路径下, 输入命令:java Hello.class,会出现什么结果,为什么?

    java Hello已经是加载类了

  2. PHP图形处理函数试题

    一.问答题 1.取得当前安装的 GD 库的信息的函数是? 2.取得图像大小的函数是? 3.为一幅图像分配颜色 + alpha的函数是? 4.新建一个基于调色板的图像的函数是? 5.新建一个黑色图像的函 ...

  3. 树形dp Codeforces Round #364 (Div. 1)B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  4. WPF(x:Null 使用)

    <Window x:Class="TestOfNull.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  5. OPenGL中的缓冲区对象

    引自:http://blog.csdn.net/mzyang272/article/details/7655464 在许多OpenGL操作中,我们都向OpenGL发送一大块数据,例如向它传递需要处理的 ...

  6. CentOS下载及版本选择-CentOS LiveCD、LiveDVD和BinDVD区别

    1.CentOS系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.x-i386-bin-DVD1.iso(32位)或者CentOS-6.x-x86_64-bin-DVD1.iso(64位), ...

  7. UIImagePickerController 相关

    UIImagePickerController是系统封装好的一个导航视图控制器,使用其开发者可以十分方便的进行相机相册相关功能的调用.UIImagePickerController继承于UINavig ...

  8. mysql Group By

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  9. 基础-session,cookie,jsp,EL,JSTL

    会话可以简单理解为:用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. kookie是在服务器端创建的,返回给浏览器,在浏览器的目录中保存了,下一次 ...

  10. java内部类继承--构造函数传参

    /: innerclasses/InheritInner.java // Inheriting an inner class. class WithInner { class Inner {} } / ...