一 安裝
目的:不需支持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 *)。