1.cgi文件的代码

#include "fcgi_stdio.h"
#include <stdlib.h>
#include <stdio.h> int main()
{ /* Initialization Code */ int count = 0; /* Start of response loop */ while (FCGI_Accept() >= 0)
{
//* body of response loop /*/ //FCGI_printf("Content-type: text/html/r/n"
// "/r/n"
// ""
// "FastCGI Hello/! (C, fcgi_stdio library)"
// "Request number %d running on host %s "
// "Process ID: %d/n",++count,getenv("SERVER_NAME"), getpid()); FCGI_printf("Content-type: text/html\r\n\r\n");
FCGI_printf("<title>FastCGI Hello HI! (C, fcgi_stdio library)</title>\n");
FCGI_printf("Request number %d running on host %s Process ID: %d\n",++count,getenv("SERVER_NAME"), getpid());
}
/* End of response loop */
return 0;
}

过程中可能遇到一些问题,提示502的错误,原因在代码的cgi问题。

使用fastcgi c api时注意的问题

最近用fastcgi的c语言api写服务,发现一个问题。我用nginx来接收请求,并通过fastcgi_pass传递到c程序。在用curl测试请求的时候,发现c程序是有被调用的,但是nginx返回的响应一直是502 "upstream closed prematurely FastCGI stdout while reading response header from upstream"。在网上找了很久,也有同样的问题,但是一般都是说原因是printf的时候没有按照http协议,比如说一定要printf("Content-type: text/html \r\n\r\n")。我按照这个格式写了,但还是出错。最后发现是由于include了c和c++的一些头文件,比如,下面的代码就有可能出现这样的问题(也有可能不出现问题):

#include "fcgi_stdio.h"
#include <iostream>
#include <string>
#include <map>
#include <stdlib.h>
#include <stdio.h> int main()
{
while(FCGI_Accept() >= 0)
{
//////////////////////////////////////////////////////////////////////////
//get content type and length
//////////////////////////////////////////////////////////////////////////
printf("Content-type: text/*\r\n\r\n");
printf("<title>FastCGI Hello! (C, fcgi_stdio library)</title>\n");
}
}

在官方的api使用例子里面,只有include “fcgi_stdio.h”和“stdlib.h”这两个头文件。于是我看了一下fcgi_stdio.h这个文件,发现它定义了一个宏,也叫printf,而事实上是调用了FCGI_printf这个函数。 而include其他的头文件可能会导致链接的时候链接到原来的printf实现,所以最保险的做法是:

#include "fcgi_stdio.h"
#include <iostream>
#include <string>
#include <map>
#include <stdlib.h>
#include <stdio.h> int main()
{
while(FCGI_Accept() >= 0)
{
//////////////////////////////////////////////////////////////////////////
//get content type and length
//////////////////////////////////////////////////////////////////////////
FCGI_printf("Content-type: text/*\r\n\r\n");
FCGI_printf("<title>FastCGI Hello! (C, fcgi_stdio library)</title>\n");
}
}

2.nginx的配置文件

#user  nobody;

#daemon off;

#master_process off;

worker_processes  1;

error_log  logs/80_error.log;

error_log  logs/80_notice.log   notice;

error_log  logs/80_info.log     info;

error_log  logs/80_debug.log    debug;

pid        logs/80.pid;

events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; error_log logs/80_http_debug.log debug; access_log logs/80_http_access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on;

//部署负载均衡
#upstream resinserver{
# server 127.0.0.1:8081 weight=1;
# server 127.0.0.1:8082 weight=1;
# server 127.0.0.1:8083 weight=1;
# keepalive 1024;
#} server {
listen 8080;
server_name 192.168.104.101 127.0.0.1 localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / {
# root html;
# index index.html index.htm;
#proxy_pass http://resinserver/;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#}
//部署cgi文件
#location ~/.cgi$ {
# fastcgi_pass 127.0.0.1:9002;
# fastcgi_index index.cgi;
# fastcgi_param SCRIPT_FILENAME cgibin$fastcgi_script_name;
# include fastcgi_params;
#} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
//这是我自己添加的
location ~\.cgi$ {
root /usr/local/nginx/cgibin;
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
# #server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

3.编译1.cgi

gcc -o 1.cgi 1.c -lfcgi

4.运行1.cgi文件

注意这是一行命令哦。注意可能要sudo.
-p是指定fastcgi控制器绑定的TCP端口listen的.
如果你想调试单个fastcgi程序,可以把-f换成-n.
-F指定spawn-fcgi将fork多少个child进程。之后nginx对于此cgi的请求就可以并发了。显然这里的直接并发量是1000.
其他参数可以help看看:(看起来-C对不要php的我来说没啥用.本文是初次使用记录性教程,这里就不纠结这些参数了)

Lenovo:/usr/local/nginx$ sbin/spawn-fcgi --help

/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -C 25 -f /usr/local/nginx/cgibin/1.cgi

监听的端口是9002,对应nginx的配置文件的9002

关闭spawn-fcgi打开的fastcgi程序: 
$ netstat -anp | grep 9000 #查看占用9000端口的程序ID
$ kill -9 PID #或killall 进程名

5.运行nginx

./sbin/nginx -c nginx.conf

6.在浏览器查看

http://192.168.104.101:8080/1.cgi

这是网上的一些测试,待有时间再做验证,先保存起来。。。

3.webbench压力测试工具

下载:http://home.tiscali.cz:8080/~cz210552/distfiles/webbench-1.5.tar.gz

安装:

tar zxvf webbench-1.5.tar.gz
cd webbench-1.5 make && make install

使用:

webbench -c 500 -t 30 http://127.0.0.1/test.jpg

500是并发连接数,30是时间单位是秒

用ab测试的结果,在我的虚拟机上,看起来性能很不错

(Apache Bench是Apache自带的工具包)

ab -n 10000 -c 100 http://127.0.0.1/1.cgi
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software:        nginx/0.7.38
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /1.cgi
Document Length:        143 bytes

Concurrency Level:      100
Time taken for tests:   3.982 seconds
Complete requests:      10000
Failed requests:        8399
   (Connect: 0, Receive: 0, Length: 8399, Exceptions: 0)
Write errors:           0
Total transferred:      2658399 bytes
HTML transferred:       1438399 bytes
Requests per second:    2511.06 [#/sec] (mean)
Time per request:       39.824 [ms] (mean)
Time per request:       0.398 [ms] (mean, across all concurrent requests)
Transfer rate:          651.89 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   4.6      0      51
Processing:    22   39   6.8     36      93
Waiting:        4   39   6.8     36      93
Total:         24   39   8.2     36      97

Percentage of the requests served within a certain time (ms)
  50%     36
  66%     39
  75%     41
  80%     42
  90%     48
  95%     54
  98%     70
  99%     84
 100%     97 (longest request)

4.Nginx模块

模块列表
http://wiki.codemongers.com/NginxModules

FastCGI模块源码:nginx/src/http/modules/ngx_http_fastcgi_module.c

nginx下面部署fast-cgi和C++【原】的更多相关文章

  1. CentOS+uwsgi+django+nginx 环境部署及分析

    写在部署前 在线上部署django项目时,比较成熟的方案是:nginx + uWSGI + Django. nginx和Django 都比较熟悉了,uWSGI是什么呢?WSGI是一个协议,python ...

  2. Nginx 热部署和日志切割,你学会了吗?

    上篇文章,我们已经安装好 Nginx,并且配置好 Nginx 文件后,这个时候我就需要操作 Nginx 的命令行了,这篇文章主要讲解 Nginx 命令行相关知识,并通过日常遇到的热部署.切割日志文件场 ...

  3. Tomcat+nginx+Keepalived部署实现集群

    Tomcat+nginx+Keepalived部署实现集群 环境说明: 系统:Centos-7 主机:Centos-7 x3 IP地址: 服务器1(192.168.10.102/24) 服务器2(19 ...

  4. nginx 安装部署前篇

    官网:https://nginx.org/ 特性:既可以作为HTTP服务器,也可以作为反向代理服务器或者邮件服务器或者邮件服务器:能够快递响应静态页面的请求:支持 Fast CGI.SSL.Virtu ...

  5. Flask + WSGI + Nginx 云部署

    这几天学着用flask写一些rest api,然后部署到云上.这个过程虽然网上有很多的教程,但还是遇到不少的问题! 采用flask的原因是因为它比较容易上手吧.用flask有专门restful api ...

  6. nginx上部署python web

    nginx上部署python web http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

  7. Nginx上部署HTTPS

    Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev,且ln -s /usr/lib/x86_64-linux-gnu/libssl.so  /usr/lib/, ...

  8. nginx服务部署 说明

    第1章 常用的软件 1.1 常用来提供静态服务的软件   Apache :这是中小型Web服务的主流,Web服务器中的老大哥,   Nginx :大型网站Web服务的主流,曾经Web服务器中的初生牛犊 ...

  9. 在nginx上部署vue项目(history模式);

    在nginx上部署vue项目(history模式): vue-router 默认是hash模式,使用url的hash来模拟一个完整的url,当url改变的时候,页面不会重新加载.但是如果我们不想has ...

随机推荐

  1. 转载 网页打印时设置A4大小

    最近开发项目时遇到了网页打印的问题,这是问题之二,打印宽度设置 在公制长度单位与屏幕分辨率进行换算时,必须用到一个DPI(Dot Per Inch)指标. 经过我仔细的测试,发现了网页打印中,默认采用 ...

  2. OpenStack/Gnocchi简介——时间序列数据聚合操作提前计算并存储起来,先算后取的理念

    先看下 http://www.cnblogs.com/bonelee/p/6236962.html 这里对于环形数据库的介绍,便于理解归档这个操作! 转自:http://blog.sina.com.c ...

  3. 获取IE代理服务器信息

    “局域网设置”里有自动配置.代理服务器的设置项目,在进行网络通讯相关的开发时,需要使用到它们,下边介绍如何将这些设置信息读取出来. 当“使用自动配置脚本”不使用时,使用WinHttpGetIEProx ...

  4. linux 系统安装 mysql

    安装mysql所需要的依赖环境 yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib*  libxml* ncurses-devel li ...

  5. HDU 1688 Sightseeing

    题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...

  6. .NET C#使用微信公众号登录网站

    适用于:本文适用于有一定微信开发基础的用户 引言:花了300大洋申请了微信公众平台后,发现不能使用微信公众号登录网站(非微信打开)获得微信帐号.仔细研究后才发现还要再花300大洋申请微信开放平台才能接 ...

  7. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

  8. HDU 1166 单点更新,区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. Jmeter中的几个重要测试指标释义

    一.基本概念 1.测试计划是使用 JMeter 进行测试的起点,它是其它 JMeter 测试元件的容器. 2.线程组:代表一定数量的并发用户,它可以用来模拟并发用户发送请求.实际的请求内容在Sampl ...

  10. 二模 (3) day2

    第一题: 题目大意:(难以概括,就不贴了把.) 解题过程: 1.担心被精度问题恶心,就把平均数的地方乘了N,这样只有最后计算的时候才会是小数.. 2.数组保存的时候蛋疼的 没改成double.结果全部 ...