20151216_jeffrey - silenceuncrio/diary GitHub Wiki
- 0905 FastCGI - 專心地來做
- 0930 Mail - 我 - 申請 Apple 開發者帳號最後的進度情況
- 0940 FastCGI - study
- 0955 FastCGI - 安装spawn-fcgi ???
- 1045 FastCGI - 今天才知道 opkg 多好用
-
1100 FastCGI - Example 1: TinyFastCGI
- libpthread 在哪裡?
-
1210 FastCGI - 在 menuconfig 的
Base system
下找到 libpthread -
1325 FastCGI - 找到
libm
和libpthread
在~/openwrt/staging_dir/
之下 -
1345 FastCGI - 總結要告知
mips-openwrt-linux-gcc
那些事 -
1355 FastCGI - 將編出來的
tinyFastCGI
利用 WinSCP 放到 JS9331 的/tmp
去執行 - 1400 FastCGI - study
-
1420 FastCGI - check 目前的
nginx.conf
- 1505 FastCGI - 感動的瞬間
-
1510 FastCGI - 從
tinyFastCGI.c
出發 想辦法解析 POST 的參數- 卡在
getenv("QUERY_STRING")
總是回應 NULL
- 卡在
- 1635 FastCGI - fastcgi中的多线程使用
-
1705 FastCGI -
fcgitest.c
- 還是 NULL- 從 nginx 查起
-
1715 FastCGI -
/etc/nginx/nginx.conf
- 關鍵就是include fastcgi_params;
-
1900 FastCGI -
fcgitest_02.c
- 每秒處理量輕鬆破 100
今天專心地來做 fcgi
加油
明天的事就別煩惱了
該怎麼做
就怎麼做
小插曲
JEFFREY 於 2015/12/16 上午 09:36 寫道:
Hi Jim,
這是我申請 Apple 開發者帳號最後的進度情況
網頁截圖如下所示請看 Authority to Sign Legal Agreements 的部分
Apple 會用 mail 或是 電話 跟您作聯繫
來確認是否您授權我 Jeffrey 來替公司的 CEO 也就是您做這樣的申請動作希望一切順利
BR
Jeffrey[your enrollment is being processed]
Charlie 於 2015/12/16 上午 09:20 寫道:
Hi Jeffrey,
也 再麻煩你把一些相關iOS申 請的進度跟Jim 分 享一下.
這 樣Apple 來 電時, Jim 可 知道要回應甚麼. 或 有甚麼相關訊息這樣.
Thank you.
Best regards,Charlie Huang
Sales & Project Manager
- 快速通用网关接口(Fast Common Gateway Interface/FastCGI)是通用网关接口(CGI)的改进,描述了客户端和服务器程序之间传输数据的一种标准。
- FastCGI致力于减少Web服务器与CGI程式之间互动的开销,从而使服务器可以同时处理更多的Web请求。
- 与为每个请求创建一个新的进程不同,FastCGI使用持续的进程来处理一连串的请求。
- 这些进程由FastCGI进程管理器管理,而不是web服务器。
- 当进来一个请求时,Web 服务器把环境变量和这个页面请求通过一个unix domain socket(都位于同一物理服务器)或者一个IP Socket(FastCGI部署在其它物理服务器)传递给FastCGI进程。
- 由于 FastCGI 程序并不需要不断的产生新进程,可以大大降低服务器的压力并且产生较高的应用效率。它的速度效率最少要比CGI 技术提高 5 倍以上。
- 它还支持分布式的部署, 即 FastCGI 程序可以在web 服务器以外的主机上执行。
- spawn-fcgi是一个通用的FastCGI进程管理器,简单小巧,原先是属于lighttpd的一部分,后来由于使用比较广泛,所以就迁移出来作为独立项目了。
- CGI 就是所谓的短生存期应用程序,FastCGI 就是所谓的长生存期应用程序。
- FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。
-
nginx 不能像apache那样直接执行外部可执行程序,但nginx可以作为代理服务器,将请求转发给后端服务器,这也是nginx的主要作用之一。
-
其中nginx就支持FastCGI代理,接收客户端的请求,然后将请求转发给后端fastcgi进程。
-
下面介绍如何使用C/C++编写cgi/fastcgi,并部署到nginx中。
-
通过前面的介绍知道,fastcgi进程由FastCGI进程管理器管理,而不是nginx。这样就需要一个FastCGI管理,管理我们编写fastcgi程序。
-
本文使用spawn-fcgi作为FastCGI进程管理器。
-
spawn-fcgi是一个通用的FastCGI进程管理器,简单小巧,原先是属于lighttpd的一部分,后来由于使用比较广泛,所以就迁移出来作为独立项目了。
-
spawn-fcgi使用pre-fork 模型,功能主要是打开监听端口,绑定地址,然后fork-and-exec创建我们编写的fastcgi应用程序进程,退出完成工作。
-
fastcgi应用程序初始化,然后进入死循环侦听socket的连接请求。
安装spawn-fcgi ???
openWrt 上安裝 spawn-fcgi ???
在 OpenWrt 上做了 opkg update 後然後下了 opkg list... 8xxx 行
不要浪費 存起來
來列一下跟 cgi
字眼相關的資訊
- cgilib - 0.7-1 - Simple CGI Library
- fcgi - 2.4.0-1 - FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs.
- fcgixx - 2.4.0-1 - Shared library of FastCGI++
- freeswitch-mod-xml-scgi - 1.5.13b-5173471 - FS SCGI XML Gateway module
- haserl - 0.9.33-1 - A CGI wrapper to embed shell scripts in HTML documents
- libcupscgi - 1.5.4-1 - Common UNIX Printing System - CGI library
- lighttpd-mod-cgi - 1.4.35-2 - CGI module
- lighttpd-mod-fastcgi - 1.4.35-2 - FastCGI module
- lighttpd-mod-scgi - 1.4.35-2 - SCGI module
- mini-httpd - 1.19-1 - mini_httpd is a small HTTP server. Its performance is not great, but for low or medium traffic sites it's quite adequate. It implements all the basic features of an HTTP server, including:
- GET, HEAD, and POST methods
- CGI
- Basic authentication
- Security against ".." filename snooping
- The common MIME types
- Trailing-slash redirection
- index.html, index.htm, index.cgi
- Directory listings
- Multihoming / virtual hosting
- Standard logging
- Custom error pages
It can also be configured to do IPv6.
- perlbase-cgi - 5.20.0-6 - CGI perl module
- php5 - 5.4.27-1 - PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
This package contains only the PHP config file. You must actually choose
your PHP flavour (cli, cgi or fastcgi). - php5-cgi - 5.4.27-1 - PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
This package contains the CGI version of the PHP5 interpreter. - php5-fastcgi - 5.4.27-1 - As FastCGI support is now a core feature the php5-fastcgi package now depends on the php5-cgi package, containing just the startup script.
- rrdcgi - 1.2.30-3 - Round Robin Database (RRD) CGI graphing tool (v1.2.x)
- ruby-cgi - 1.9.3-p545-1 - Ruby CGI support toolkit
- spawn-fcgi - 1.6.3-1 - spawn-fcgi is used to spawn fastcgi applications
- uhttpd - 2014-08-25-dabd7dea6445aaa0e5b8d9add1872fa7393b3a85 - uHTTPd is a tiny single threaded HTTP server with TLS, CGI and Lua support. It is intended as a drop-in replacement for the Busybox HTTP daemon.
- uhttpd-mod-lua - 2014-08-25-dabd7dea6445aaa0e5b8d9add1872fa7393b3a85 - The Lua plugin adds a CGI-like Lua runtime interface to uHTTPd.
- wt-fcgi - 3.1.9-1 - FastCGI Connector
- cgilib - 0.7-1 - Simple CGI Library
終於看到讓我放心的字眼
spawn-fcgi - 1.6.3-1 - spawn-fcgi is used to spawn fastcgi applications
來裝吧
opkg install spawn-fcgi
root@JoySince:/# opkg install spawn-fcgi
Installing spawn-fcgi (1.6.3-1) to root...
Downloading http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/packages/oldpackages/spawn-fcgi_1.6.3-1_ar71xx.ipk.
Configuring spawn-fcgi.
看一下幫助信息
root@JoySince:/# spawn-fcgi -h
Usage: spawn-fcgi [options] [-- <fcgiapp> [fcgi app arguments]]
spawn-fcgi v1.6.3 (ipv6) - spawns FastCGI processes
Options:
-f <path> filename of the fcgi-application (deprecated; ignored if
<fcgiapp> is given; needs /bin/sh)
-d <directory> chdir to directory before spawning
-a <address> bind to IPv4/IPv6 address (defaults to 0.0.0.0)
-p <port> bind to TCP-port
-s <path> bind to Unix domain socket
-M <mode> change Unix domain socket mode
-C <children> (PHP only) numbers of childs to spawn (default: not setting
the PHP_FCGI_CHILDREN environment variable - PHP defaults to 0)
-F <children> number of children to fork (default 1)
-P <path> name of PID-file for spawned process (ignored in no-fork mode)
-n no fork (for daemontools)
-v show version
-?, -h show this help
(root only)
-c <directory> chroot to directory
-S create socket before chroot() (default is to create the socket
in the chroot)
-u <user> change to user-id
-g <group> change to group-id (default: primary group of user if -u
is given)
-U <user> change Unix domain socket owner to user-id
-G <group> change Unix domain socket group to group-id
進度超順利... opkg... 真可怕
我竟然今天才知道它有多好用
Example 1: TinyFastCGI
Here is a simple example of a responder FastCGI application written in C:
#include "fcgi_stdio.h" /* fcgi library; put it first*/
#include <stdlib.h>
int count;
void initialize(void) {
count=0;
}
void main(void) {
/* Initialization. */
initialize();
/* Response loop. */
while (FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_HOSTNAME"));
}
}
存成 tinyFastCGI.c
fcgi_stdio.h
在哪裡呢? 在 ~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include
jeffrey@jeffrey-virtual-machine:~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include$ ls
af_vfs.h faad.h json lua.h readline
alsa fastcgi.h json-c lualib.h sndfile.h
atmarpd.h fcgiapp.h lauxlib.h lzo sndfile.hh
atmarp.h fcgi_config.h libavcodec mac80211 speex
atmd.h fcgimisc.h libavdevice mac80211-backport termcap.h
atm.h fcgio.h libavfilter mad.h term.h
atmsap.h fcgios.h libavformat menu.h tremor
attr fcgi_stdio.h libavutil mp4ff.h ubi-media.h
...
相關的 library 在哪裡呢? 在 ~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib
- libfcgi
- libfcgi++
jeffrey@jeffrey-virtual-machine:~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib$ ls
...
libfcgi.a liblzo2.so libustream-ssl.so
libfcgi++.a liblzo2.so.2 libuuid.a
libfcgi.so liblzo2.so.2.0.0 libuuid.so
libfcgi++.so libmad.a libuuid.so.1
libfcgi.so.0 libmad.so libuuid.so.1.3.0
libfcgi++.so.0 libmad.so.0 libvalidate.so
libfcgi.so.0.0.0 libmad.so.0.2.1 libvorbis.a
libfcgi++.so.0.0.0 libmenu.a libvorbisenc.a
...
需要告知 mips-openwrt-linux-gcc
三件事
-I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/
-L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/
-lfcgi
就不要掛副檔名 .cgi
了, command 如下
mips-openwrt-linux-uclibc-gcc -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lfcgi -o tinyFastCGI tinyFastCGI.c
編不過
jeffrey@jeffrey-virtual-machine:/mnt/hgfs/share/js9331_event_cgi$ mips-openwrt-linux-uclibc-gcc -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lfcgi -o tinyFastCGI tinyFastCGI.c
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/bin/ld: warning: libm.so.0, needed by /home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib//libfcgi.so, not found (try using -rpath or -rpath-link)
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/bin/ld: warning: libpthread.so.0, needed by /home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib//libfcgi.so, not found (try using -rpath or -rpath-link)
/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib//libfcgi.so: undefined reference to `frexp'
collect2: error: ld returned 1 exit status
兩個 warning
- warning: libm.so.0, needed by
libfcgi.so
, not found - warning: libpthread.so.0, needed by
libfcgi.so
, not found
多告知 mips-openwrt-linux-gcc
一件事 -lfcgi
-I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/
-L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/
-lm
-lfcgi
jeffrey@jeffrey-virtual-machine:/mnt/hgfs/share/js9331_event_cgi$ mips-openwrt-linux-uclibc-gcc -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lm -lfcgi -o tinyFastCGI tinyFastCGI.c
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/bin/ld: warning: libpthread.so.0, needed by /home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib//libfcgi.so, not found (try using -rpath or -rpath-link)
一個 warning
- warning: libpthread.so.0, needed by
libfcgi.so
, not found
不過已經編譯過了
libpthread 在哪裡啊?
menuconfig 選單找不到阿
終於在 Base system
下找到
默認是安裝的不能更動
.config - OpenWrt Configuration
──────────────────────────────────────────────────────────────────────────────
┌────────────────────────────── Base system ──────────────────────────────┐
│ Arrow keys navigate the menu. <Enter> selects submenus --->. │
│ Highlighted letters are hotkeys. Pressing <Y> includes, <N> excludes, │
│ <M> modularizes features. Press <Esc><Esc> to exit, <?> for Help, </> │
│ for Search. Legend: [*] built-in [ ] excluded <M> module < > │
│ ┌────^(-)─────────────────────────────────────────────────────────────┐ │
│ │ ... │ │
│ │ -*- libpthread.......................................... POSIX th│ │
│ │ ... │ │
│ └────v(+)─────────────────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────────────────────┤
│ <Select> < Exit > < Help > < Save > < Load > │
└─────────────────────────────────────────────────────────────────────────┘
那 libpthread 在哪裡啊?
找到了
libm
和 libpthread
jeffrey@jeffrey-virtual-machine:~/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib$ ls
...
ld-uClibc-0.9.33.2.so libm-0.9.33.2.so libstdc++.so.6
ld-uClibc.so.0 libm.a libstdc++.so.6.0.19
lib libm_pic.a libstdc++.so.6.0.19-gdb.py
libatomic.a libm.so libsupc++.a
libatomic.la libm.so.0 libsupc++.la
...
libatomic.so.1.0.0 libpthread-0.9.33.2.so libthread_db_pic.a
libc.a libpthread.a libthread_db.so
libc_pic.a libpthread_nonshared.a libthread_db.so.1
libcrypt-0.9.33.2.so libpthread_nonshared_pic.a libuClibc-0.9.33.2.so
libcrypt.a libpthread_pic.a libutil-0.9.33.2.so
libcrypt_pic.a libpthread.so libutil.a
libcrypt.so libpthread.so.0 libutil_pic.a
libcrypt.so.0 libpthread_so.a libutil.so
...
math.h
和 pthread.h
jeffrey@jeffrey-virtual-machine:~/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/include$ ls
...
assert.h fnmatch.h malloc.h pthread.h stdio.h utime.h
atomic.h fpu_control.h math.h pty.h stdlib.h utmp.h
...
總結一下要告知 mips-openwrt-linux-gcc
那些事
-I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/
-L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/
-I~/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/include/
-L~/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib
-lm
-lpthread
-lfcgi
command 如下
mips-openwrt-linux-uclibc-gcc -I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lm -lpthread -lfcgi -o tinyFastCGI tinyFastCGI.c
jeffrey@jeffrey-virtual-machine:/mnt/hgfs/share/js9331_event_cgi$ mips-openwrt-linux-uclibc-gcc -I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lm -lpthread -lfcgi -o tinyFastCGI tinyFastCGI.c
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/bin/ld: warning: libdl.so.0, needed by /opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/lib/libpthread.so.0, not found (try using -rpath or -rpath-link)
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/lib/libpthread.so.0: undefined reference to `dlclose'
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/lib/libpthread.so.0: undefined reference to `dlopen'
/opt/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/lib/libpthread.so.0: undefined reference to `dlsym'
collect2: error: ld returned 1 exit status
- warning: libdl.so.0, needed by
libpthread.so.0
, not found
再一次
總結一下要告知 mips-openwrt-linux-gcc
那些事
-I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/
-L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/
-I~/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/include/
-L~/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib
-lm
-ldl
-lpthread
-lfcgi
command 如下
mips-openwrt-linux-uclibc-gcc -I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lm -ldl -lpthread -lfcgi -o tinyFastCGI tinyFastCGI.c
終於編過了... 超感動
jeffrey@jeffrey-virtual-machine:/mnt/hgfs/share/js9331_event_cgi$ mips-openwrt-linux-uclibc-gcc -I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lm -ldl -lpthread -lfcgi -o tinyFastCGI tinyFastCGI.c
將編出來的 tinyFastCGI
利用 WinSCP 放到 /tmp
去
執行看看
root@JoySince:/tmp# ./tinyFastCGI
Content-type: text/html
<title>FastCGI Hello! (C, fcgi_stdio library)</title><h1>FastCGI Hello! (C, fcgi_stdio library)</h1>Request number 1 running on host <i>(null)</i>
root@JoySince:/tmp#
動起來囉...
再來是要搭配 spawn-fcgi
了
理論上是敲這個
spawn-fcgi -a 127.0.0.1 -p 8081 -f /tmp/tinyFastCGI
root@JoySince:/tmp# spawn-fcgi -a 127.0.0.1 -p 8081 -f /tmp/tinyFastCGI
spawn-fcgi: child spawned successfully: PID: 7521
root@JoySince:/tmp#
一次就成功...
ince:/tmp#
root@JoySince:/tmp# ps
PID USER VSZ STAT COMMAND
1 root 1388 S /sbin/procd
...
7521 root 1252 S /tmp/tinyFastCGI
7522 root 1360 R ps
root@JoySince:/tmp#
再跑了...
Setting Up FastCGI Proxying
nginx can be used to route requests to FastCGI servers which run applications built with various frameworks and programming languages such as PHP.
The most basic nginx configuration to work with a FastCGI server includes using the fastcgi_pass directive instead of the proxy_pass directive, and fastcgi_param directives to set parameters passed to a FastCGI server.
Suppose the FastCGI server is accessible on localhost:9000. Taking the proxy configuration from the previous section as a basis, replace the proxy_pass directive with the fastcgi_pass directive and change the parameter to localhost:9000.
In PHP, the SCRIPT_FILENAME parameter is used for determining the script name, and the QUERY_STRING parameter is used to pass request parameters.
The resulting configuration would be:
check 一下目前的 nginx.conf
root@JoySince:/etc/nginx# cat /etc/nginx/nginx.conf
user nobody nogroup;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location / {
root /data/www;
}
location /images/ {
root /data;
}
location = /demo.cgi {
fastcgi_pass localhost:8081;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
root@JoySince:/etc/nginx#
上吧
root@JoySince:/etc/nginx# /etc/init.d/nginx start
check 一下
root@JoySince:/etc/nginx# /etc/init.d/nginx start
root@JoySince:/etc/nginx# ps
PID USER VSZ STAT COMMAND
1 root 1388 S /sbin/procd
...
7692 root 3420 S nginx: master process /usr/sbin/nginx
7693 nobody 3628 S nginx: worker process
7694 root 1360 R ps
起來囉
感動的瞬間
趕緊記錄一下
- tinyFastCGI.c
#include "fcgi_stdio.h" /* fcgi library; put it first*/
#include <stdlib.h>
int count;
void initialize(void) {
count=0;
}
void main(void) {
/* Initialization. */
initialize();
/* Response loop. */
while (FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running\n",
++count);
}
}
- 編譯的命令
jeffrey@jeffrey-virtual-machine:/mnt/hgfs/share/js9331_event_cgi$ mips-openwrt-linux-uclibc-gcc -I~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L~/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -I/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include/ -L/home/jeffrey/openwrt/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib/ -lm -ldl -lpthread -lfcgi -o tinyFastCGI tinyFastCGI.c
/etc/nginx/nginx.conf
events {
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
location / {
fastcgi_pass 127.0.0.1:8000;
}
}
}
-
/etc/init.d/nginx start
-
spawn-fcgi -p 8000 -n /tmp/tinyFastCGI
走到這一部可不容易
- 開發環境要做
make menuconfig
將 fcgi build 進去 - JS9331 板子上利用 opkg 裝了
- nginx
- spawn-fcgi
感謝這一篇 Writing Hello World in FCGI with C++
試著從 tinyFastCGI.c
出發
首先想辦法解析 POST 的參數
耐著性子從 header 檔出發
來掃一下有哪些 header 可以用
卡在 getenv("QUERY_STRING")
總是回應 NULL
- fcgitest.c
#include "fcgiapp.h"
#include <stdlib.h>
#include <string.h>
int main () {
FCGX_Stream *in, *out, *err;
FCGX_ParamArray envp;
while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
char *method = FCGX_GetParam("REQUEST_METHOD", envp);
FCGX_FPrintF(out,
"Content-type: text/html\r\n"
"\r\n"
"<h1>fastCGI Test</h1>"
);
if (!method) {
FCGX_FPrintF(out,
"FCGX_GetParam(\"REQUEST_METHOD\", envp);"
" NULL"
);
}
}
return 0;
}
還是 NULL
看來要從 nginx 查起
找到了
/etc/nginx/nginx.conf
events {
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
location / {
fastcgi_pass 127.0.0.1:8000;
include fastcgi_params;
}
}
}
關鍵就是 include fastcgi_params;
沒有 fastcgi_params
竟然萬萬不行
/etc/nginx/fastcgi_params
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_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 HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
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;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
- fcgitest.c
#include "fcgiapp.h"
#include <stdlib.h>
#include <string.h>
int main () {
FCGX_Stream *in, *out, *err;
FCGX_ParamArray envp;
while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
char *method = FCGX_GetParam("REQUEST_METHOD", envp);
FCGX_FPrintF(out,
"Content-type: text/html\r\n"
"\r\n");
if (strcmp(method, "POST") == 0) {
FCGX_FPrintF(out, "this is a post method");
} else if (strcmp(method, "GET") == 0){
FCGX_FPrintF(out, "this is a get method");
} else {
FCGX_FPrintF(out, "other method\n");
}
}
return 0;
}
動起來囉
真爽
- fcgitest_02.c
#include <stdio.h>
#include <json-c/json.h>
#include "fcgiapp.h"
#define STRCMP(a,b) (strcmp(a,b) == 0)
json_object *obj;
int main () {
FCGX_Stream *in, *out, *err;
FCGX_ParamArray envp;
while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
FCGX_FPrintF(out,
"Content-type: application/json\r\n"
"\r\n"
);
obj = json_object_new_object();
json_object_object_add(obj, "ok", json_object_new_boolean(TRUE));
FCGX_FPrintF(out,
"%s", json_object_to_json_string(obj));
json_object_put(obj);
}
return 0;
}
-
執行緒群組
- 執行緒屬性
- 執行緒數量 - 100
- 啟動延遲(秒) - 10
- 迴圈次數 - 5
- 執行緒屬性
-
HTTP 要求
- Web 伺服器
- 主機 IP - 192.168.1.251
- 端口號碼 - 8080
- HTTP 要求
- 方法 - POST
- Web 伺服器
Label | 取樣數 | 平均值 | 中間值 | 90% Line | 95% Line | 99% Line | 最小值 | 最大值 | 錯誤率 | 處理量/sec | 每秒千位元組 |
---|---|---|---|---|---|---|---|---|---|---|---|
總計 | 500 | 506 | 506 | 508 | 510 | 515 | 504 | 522 | 0.00% | 40.2 | 7.3 |
- 執行緒群組
- 執行緒屬性
- 執行緒數量 - 200
- 啟動延遲(秒) - 10
- 迴圈次數 - 5
- 執行緒屬性
Label | 取樣數 | 平均值 | 中間值 | 90% Line | 95% Line | 99% Line | 最小值 | 最大值 | 錯誤率 | 處理量/sec | 每秒千位元組 |
---|---|---|---|---|---|---|---|---|---|---|---|
總計 | 1000 | 507 | 507 | 512 | 515 | 520 | 503 | 525 | 0.00% | 79.7 | 14.4 |
- 執行緒群組
- 執行緒屬性
- 執行緒數量 - 300
- 啟動延遲(秒) - 10
- 迴圈次數 - 5
- 執行緒屬性
Label | 取樣數 | 平均值 | 中間值 | 90% Line | 95% Line | 99% Line | 最小值 | 最大值 | 錯誤率 | 處理量/sec | 每秒千位元組 |
---|---|---|---|---|---|---|---|---|---|---|---|
總計 | 1500 | 512 | 508 | 519 | 523 | 541 | 503 | 3509 | 0.00% | 96.9 | 17.5 |
- 執行緒群組
- 執行緒屬性
- 執行緒數量 - 400
- 啟動延遲(秒) - 10
- 迴圈次數 - 5
- 執行緒屬性
Label | 取樣數 | 平均值 | 中間值 | 90% Line | 95% Line | 99% Line | 最小值 | 最大值 | 錯誤率 | 處理量/sec | 每秒千位元組 |
---|---|---|---|---|---|---|---|---|---|---|---|
總計 | 2000 | 821 | 508 | 519 | 3506 | 9512 | 503 | 9537 | 0.00% | 107.6 | 19.4 |
每秒處理量輕鬆破 100