fastcgi 分布式
以lighttpd fastcgi写一下自己对fastcgi分布式的理解。
假设一台机器A上运行lighttpd,在这台主机上只是对请求进行分发。
而在其他多台机器上运行多个fastcgi进程,用来接收来自机器A分发过来的请求,并进行处理。
那lighttpd的配置fastcgi部分,应该配置ip和port(配置socket时是针对lighttpd和fastcgi进程在同一台机器上)。
而fastcgi进程应该监听ip:port上的请求。
稍微修改下前面的配置文件和代码。
server.document-root = "/mnt/hgfs/share/test/fcgi/"
server.port = 9090
socket_dir = "/tmp/"
server.username = "weifeilong"
server.groupname = "weifeilong"
server.errorlog = "/mnt/hgfs/share/test/fcgi/err.log"
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)
static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" )
index-file.names = ( "index.html" )
server.modules += ("mod_fastcgi")
fastcgi.server = (
"/fellow" => (
"test.fastcgi.handler" => (
"host" => "127.0.0.1"//对于多台机器的,可以为其他运行fastcgi进程的ip
"port" => "9090"
#"socket" => socket_dir + "test.fastcgi.socket",
"check-local" => "disable",
#"bin-path" => "/mnt/hgfs/share/test/fcgi/test.fastcgi",
"max-procs" => 10,
)
)
)
fastcgi进程:
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <alloca.h>
#include <fcgiapp.h>
#include <stdio.h>
#define LISTENSOCK_FILENO 0
#define LISTENSOCK_FLAGS 0
int main(int argc, char** argv) {
openlog("testfastcgi", LOG_CONS|LOG_NDELAY, LOG_USER);
int err = FCGX_Init(); /* call before Accept in multithreaded apps */
if (err) { syslog (LOG_INFO, "FCGX_Init failed: %d", err); return 1; }
FCGX_Request cgi;
int sock = FCGX_OpenSocket("127.0.0.1:9090", 10);//在127.0.0.1:9090上监听。
err = FCGX_InitRequest(&cgi, sock, LISTENSOCK_FLAGS);
if (err) { syslog(LOG_INFO, "FCGX_InitRequest failed: %d", err); return 2; }
while (1) {
err = FCGX_Accept_r(&cgi);
if (err) { syslog(LOG_INFO, "FCGX_Accept_r stopped: %d", err); break; }
#if 1
char* header = (char*) alloca(200);
strcpy(header, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n");
FCGX_PutStr(header, strlen(header), cgi.out);
char response[1024];
if(!strncmp("GET", FCGX_GetParam("REQUEST_METHOD", cgi.envp), strlen("GET")))
{
FCGX_PutStr("GET", strlen("GET"), cgi.err);
if(!strncmp("action=getInfo", FCGX_GetParam("QUERY_STRING", cgi.envp), strlen("action=getInfo")))
{
snprintf(response, sizeof(response), "{\"action\": \"getInfo\"}\n");
}
}
else if(!strncmp("POST", FCGX_GetParam("REQUEST_METHOD", cgi.envp), strlen("POST")))
{
char *post_data = (char*)malloc(1024);
FCGX_GetStr(post_data, 1024, cgi.in);
FCGX_PutStr(post_data, 1024, cgi.err);
if(!strncmp("action=resetInfo", post_data, strlen("action=resetInfo")))
{
snprintf(response, sizeof(response), "{\"action\": \"resetInfo\"}\n");
}
}
FCGX_PutStr(response, strlen(response), cgi.out);
#endif
}
return 0;
}
先运行fastcgi程序,然后启动lighttpd。
fastcgi 分布式的更多相关文章
- 【转】搞清FastCgi与PHP-fpm之间的关系
一.问题:网上有的说,fastcgi是一个协议,php-fpm实现了这个协议: 有的说,php-fpm是fastcgi进程的管理器,用来管理fastcgi进程的: 有的说,php-fpm是php内核的 ...
- PHP的FastCGI
CGI全称是“通用网关接口”(Common Gateway Interface), 它可以让一个客户端,从网页浏览器向执行在Web服务器上的程序请求数据. CGI描述了客户端和这个程序之间传输数据的一 ...
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
什么是CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用 ...
- CGI,FastCGI,PHP-CGI与PHP-FPM
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- Nginx + CGI/FastCGI + C/Cpp
接着上篇<Nginx安装与使用>,本篇介绍CGI/FASTCGI的原理.及如何使用C/C++编写简单的CGI/FastCGI,最后将CGI/FASTCGI部署到nginx.内容大纲如下: ...
- php的几种运行模式CLI、CGI、FastCGI、mod_php
1.CLI:就是命令行,例如可以在控制台或者是shell中键入命令: php -f index.php 然后获取输出 2.CGI:以下是不同的说法与理解 公共网关接口”(Common Gateway ...
- Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布
FastCGI编程包括四部分:初始化编码.接收请求循环.响应内容.响应结束循环. FCGX_Request request; FCGX_Init(); ); FCGX_InitRequest(& ...
- lighttpd与fastcgi+cgilua原理、代码分析与安装
原理 http://www.cnblogs.com/skynet/p/4173450.html 快速通用网关接口(Fast Common Gateway Interface/FastCGI)是通用网关 ...
- 知识积累:CGI,FastCGI,PHP-CGI与PHP-FPM
CGICGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上.CGI可以用任何一种语 ...
随机推荐
- aspose.word使用简单方法
概念介绍 使用aspose生成word报表步骤: 加载word模板 提供数据源 填充 加载模板 提供了4种重载方法 public Document(); public Document(Stream ...
- 《Unix网络编程》卷2 读书笔记 第1章-简介
1. 概述 2. 进程.线程与信息共享 Unix进程间的信息共享有多种方式:注意下图中内核的位置 左边的两个进程共享存留于文件系统中某个文件上的某些信息.为访问这些信息,每个进程都得穿越内核. 中 ...
- Task和BackTask
一.总结性知识点: 1.Android应用运行时会创建任务Task,用于存放主窗口 2.每一个任务包含一个堆栈数据结构,用于保存当前应用已创建的窗口对象,这个堆栈即回退栈BackSta ...
- every day english
job is in your freedom, not your compliance. through no fault of his own. as far as I understand you ...
- Java中的OutOfMemoryError的各种情况及解决和JVM内存结构
在JVM中内存一共有3种:Heap(堆内存),Non-Heap(非堆内存) [3]和Native(本地内存). [1] 堆内存是运行时分配所有类实例和数组的一块内存区域.非堆内存包含方法区和JVM内部 ...
- yii 操作session和cookie
一,在Yii中使用session 1,CHttpSession 与原生态php5的session使用差别是,php5使用session_start();$_session['key'] = $valu ...
- Page 16 Exercises 1.2.3 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
Below are four faulty programs. Each includes a test case that results in failure. Answer the follow ...
- hive 面试题 转载
转自:http://blog.csdn.net/ningguixin/article/details/12852051 有一张很大的表:TRLOG该表大概有2T左右TRLOG:CREATE TABLE ...
- PHP 解压zip文件的函数封装
/** * zip文件解压 * * @param $zipFilePath zip文件的路径,可以不加zip文件后缀.如果其他类型的文件伪装成zip解压也会失败 * @param $directory ...
- Name-based virtual servers 给予名称的虚拟服务
nginx first decides which server should process the request. Let’s start with a simple configuration ...