fastcgi 分布式

Posted fellow_jing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

Linux php FastCGI

基于lamp+fastcgi+https搭建phpMyAdmin和wordpress

社交网站部署——MFS分布式文件系统

搭建社交网站

部署社交网站