(原)nginx+FastCGI+c++

來源:互聯網
上載者:User

一 安裝

目的:不需支援php等。就html就行了。
步驟:
下載這些東西:
Lenovo:~/下載/download4nginx$ ls

md5-1.3.0.tgz openssl-1.0.1c.tar.gz zlib-1.2.7.tar.gz
nginx-1.3.4.tar.gz pcre-8.31.tar.gz

解壓縮nginx-1.3.4.tar.gz到~/下載/.

把其他庫都解壓縮得到md5-1.3.0、openssl-1.0.1c、zlib-1.2.7、pcre-8.31,
把這些拷貝到~/下載/nginx-1.3.4

Lenovo:~/下載$ ls 
download4nginx nginx-1.3.4
Lenovo:~/下載$ cd nginx-1.3.4
Lenovo:~/下載/nginx-1.3.4$ vi my_configure.sh

./configure --prefix=/usr/local/nginx \
--with-pcre=pcre-8.31 \
--with-zlib=zlib-1.2.7 \
--with-openssl=openssl-1.0.1c \
--with-http_stub_status_module \
--with-http_ssl_module
#上面這些庫都是比較常用的,建議還是分開把這些庫install了。

make

make install
Lenovo:~/下載/nginx-1.3.4$ chmod +x my_configure.sh
Lenovo:~/下載/nginx-1.3.4$ sudo ./my_configure.sh

二 測試

$ cd /usr/local/nginx
$ sudo ./sbin/nginx
瀏覽器:http://localhost 

三 nginx命令

啟動:nginx 
重啟:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`  #重新載入配置,並開啟新的背景工作處理序,關閉就的進程,此操作不會插斷要求.
殺死:kill -TERM `cat /usr/local/nginx/logs/nginx.pid`  #快速關閉程式,中止當前正在處理的請求 .
關閉 : kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`  #處理完當前請求後,關閉程式 .
nginx -t  測試組態檔案是否正確. 在運行時需要重新載入配置的時候,此命令非常重要,用來檢測所修改的設定檔是否有語法錯誤.

四、設定檔
$ sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_bak
參考:http://wiki.nginx.org/NginxFullExample

五、其他零散的經驗啊:

1.設伺服器的某網站網域名稱:
$ sudo vi /usr/local/nginx/conf/nginx.conf
在某個server{裡的server_name 後預設是localhost改成比如:my.nginxserver.com.
再修改hosts檔案:
$ sudo vi /etc/hosts
添加127.0.0.1 my.nginxserver.com
重啟nginx。

2.


六、nginx + fastcgi + C++

nginx是支援fastcgi的。然而我們需要下一個fastcgi進程管理器,啟動它才能執行fastcgi程式。(這裡有幾個概念要搞清楚:nginx是nginx,fastcgi是fastcgi,前者支援後者,但是前者本身沒有整合後者(/的功能)。對於ngingx,我們要配置conf.nginx來設定如何支援fastcgi。而對於fastcgi,我們寫的fastcgi程式需要一個調度者:fastcgi進程管理器。——純屬個人理解。)

1. 下載spawn-fcgi-1.6.3.tar.gz : http://redmine.lighttpd.net/news/spawn7
(這個spawn-fcgi就是fastcgi進程管理器。是lighttpd(一個類似apatch、nginx的項目,這裡不需要它)中的一個子項目,c嘎嘎的。)
$ ./configure && make
$ sudo cp ./src/spawn-fcgi /usr/local/nginx/sbin/

2.寫fastcgi程式還需要fastcgi的庫和標頭檔支援(就好像寫多線程程式需要-lpthread一樣):
下載fastcgi的庫 (裡面還有fastcgi程式的examples噢)

 編譯fastcgi庫可能會出錯,可能是fastcgi項目太老了,GCC又更新太快,修改include/fcgio.h,添加#include<cstdio>
編譯,安裝。

3. 現在寫一個fastcgi程式:
$ cd /usr/local/nginx
$ sudo mkdir myfastcgi
$ sudo vi helloFastCGI.c

#include <iostream>
#include <fcgi_stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
int count = 0;

while(FCGI_Accept() >= 0)
{

printf( "Content-type:text/html\r\n\r\n" );
printf( "<p> Hello FastCGI !  </ p>" );
printf( "<br /> Request number = [%d]", ++count );
printf( "<br /> Process ID: %d ", getpid() );

}

return 0;
}

$ sudo vi Makefile

all :
      g++ helloFastCGI.c -o helloFastCGI -L/usr/local/lib -lfcgi -Wl,-R /usr/local/lib
$ sudo make
$ ./helloFastCGI
看到printf了說明程式連結都沒問題了的。
$ cp helloFastCGI /usr/local/nginx/myFastCGI #這個檔案夾就放我的fast cgi程式。

4.啟動我們的fastcgi進程管理器——spawn-fcgi

/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 25 -f /usr/local/nginx/myFastCGI/helloFastCGI -F 1000

注意這是一行命令哦。注意可能要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

關閉spawn-fcgi開啟的fastcgi程式
$ netstat -anp | grep 9000 #查看佔用9000連接埠的程式ID
$ kill -9 PID #或killall 進程名

5.修改nginx設定檔:

這個網上很多。我的如下(我特殊需求:不希望有緩衝)。
$ sudo vi /usr/local/nginx/conf/nginx.conf

#user nobody;
worker_processes 4;

 

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

 

#pid logs/nginx.pid;

 

worker_rlimit_nofile 102400;

 

events {
use epoll;
worker_connections 102400;
}

 

http {
include mime.types;
default_type application/octet-stream;

 

charset utf-8;

 

server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;

 

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

 

#access_log logs/access.log main;

 

sendfile on;
tcp_nopush on;

 

keepalive_timeout 60;

 

server {
listen 80; // 這兒可以加ip,如果多網卡的話。 --- listen 10.228.132.191:80;
server_name moa.provider.com;
index index.htm index.html;
#access_log logs/host.access.log main;

 

location / {
root html;
index index.html index.htm;
#fastcgi_pass 127.0.0.1:9000;
}

 

location ~ \.cgi$ {
#root fcgi;
#server_name moa.fastcgi.com;
#rewrite (.*).cgi /$1 break; //c/c++編譯出來沒有尾碼,所以這裡去掉尾碼,否則nginx會出現503,403等等錯誤(不知道為什麼就是沒有404)。
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME fcgi$fastcgi_script_name;
#include fastcgi.conf;
include fastcgi_params;
#fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; //定義fastcgi程式jue dui路徑
}

 

#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;
}
}
}

# 改了很多,很亂。不是個好例子。僅僅是個hello world~~

6  有幾個概念必須講清楚——
1. .ginx收到cgi請求後,會看有多少個該cgi程式的進程(spawn-fcgi -F指定的參數),然後根據並發量來調用(調度)cgi程式。
2. 原版spawn-fcgi(可參考下面七參考資料裡daemon版spawn-fcgi)在fork了cgi程式後,自己就退出了。這時fork了的cgi程式的父進程ID都是1了,即init系統進程。這樣,如果想並發就需要你的fastcgi去支援並發,可google:fastcgi並發
3. 關於php,nginx是用fastcgi來解析php的。這個負責解析的fastcgi程式並不多,好像就1個,因此這cgi不能大並發,但是沒關係nginx支援cgi緩衝~所以php網頁的並發請求跟fastcgi關係不大。其實可以把fastcgi對於php的作用當作一個編譯器,編譯完後,php都有了緩衝,再請求就不需要再次跑fastcgi來解析php指令碼了,php就是個該死的指令碼啊~~ 

 我自己也不知道講清楚沒。其實我自己也不知道自己清楚沒。:-)

 

 

七、參考資料:

 
1.http://wiki.nginx.org/NginxFullExample (配置)
2.Nginx配置與應用詳解 (詳你妹)

3.Nginx-httpFastCGIModule: http://wiki.nginx.org/HttpFcgiModule
4.網站壓力測試工具webbench[原創] (還不錯.  $ webbench -c 1000 -t 30 http://127.0.0.1/test.jpg)
5.spawn-fcgi與fcgi的運行機制分析 (這個還不錯,把spawn-fcgi改成了精靈。但是在調用daemon函數的時候應該傳參數(1, 0)。ps:實際上這個spawn-fcgi只是會截獲孩子進程的CLD_DUMPED(它coredumped了)然後增加一個孩子進程(就是檢測某到某孩子coredumped了就再fork一個).)
6.http://wbwk2005.blog.51cto.com/2215231/550540 講fastcGI技術優點已經如何效率的。必讀。

7. Fastcgi 白皮書 (不錯,簡單明了的講了fastcgi架構原理,給出了fastcgi白皮書連結,FCGI_Record就是http request內容 ~)
8. fastcgi並發: http://www.google.com.hk/search?sourceid=chrome&client=ubuntu&channel=cs&ie=UTF-8&q=fastcgi%E5%B9%B6%E5%8F%91

9. linux下進程的最大線程數、進程最大數、進程開啟的檔案數


八、 我的記錄

參考了上面的參考資料8和6之後,我決定spawn-fcgi -F 100, 然後在每個cgi程式裡開1000個線程(把線程的棧改設為1KB,或者還是1M吧),這樣並發就是100*1000 = 10W了。足夠了。不過我cgi程式會用來給後面的真正的推送服務主程式發請求。所以後面的並發也是一道關卡。

------------------------------------------------------------------------------
依然零散的經驗啊:
nginx添加被fastcgi識別的自訂header:
自訂header:
在nginx.conf裡面加:
http {
....
underscores_in_headers on;#這樣就可以支援底線的header了,預設不支援.
....
location  .... {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
        }
然後在fastcgi_params裡添加想增加的header:
fastcgi_param     REQUEST_COMMAND   $http_request_command;#“request_command”就是你添加的希望被識別的自訂header.在fastcgi程式i裡這樣取header:FCGX_GetParam("REQUEST_COMMAND", request->envp);
(request是FCGX_Request *)。  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.