文章目錄
- 1.1.安裝
- 1.2.管理
- 2.1.安裝lighttpd的spawn-fastcgi
- 2.2.安裝fastcgi庫
- 2.3.Hello world
- 2.4.啟動Spawn-cgi
- 2.5.修改Nginx設定檔
下面是我很早以前寫的一篇文檔,貼出來。 1.Nginx1.1.安裝
- Nginx 的中文維基 http://wiki.codemongers.com/NginxChs下載 Nginx 0.6.26(開發版)(請下載最新版本)
- tar zxvf nginx-0.6.26.tar.gz
- ./configure,注意了類似checking for *** ... not found項,可能是依賴包沒有,則需要安裝依賴包
- 缺少PCRE,sudo apt-get install pcre安裝。或者去:http://www.pcre.org/
- 如果缺少OpenSSL,sudo apt-get install libssl-dev,或者去:http://www.openssl.org
- 如果缺少zlib,可以apt-get install zlib1g,或者http://www.zlib.net/
- 配置請參考:http://wiki.codemongers.com/NginxChsInstall可以選擇安裝模組
- make & make install
- 預設安裝在/usr/local/nginx下
1.2.管理
- 執行選項
-c </path/to/config> 為 Nginx 指定一個設定檔,來代替預設的。
-t 不運行,而僅僅測試組態檔案。nginx 將檢查設定檔的文法的正確性,並嘗試開啟設定檔中所引用到的檔案。
-v 顯示 nginx 的版本。
-V 顯示 nginx 的版本,編譯器版本和配置參數。
- 檢查設定檔
$ sudo nginx -t2008/03/12 19:15:11 info 12384#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2008/03/12 19:15:11 info 12384#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
- 啟動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- 查看nginx主進程號
$ ps -ef | grep "nginx: master process"
| grep -v "grep"
| awk -F ' ' '{print $2}
26010
- 重新載入設定檔
$sudo kill -HUP 26010
通過系統的訊號控制 Nginx
可以使用訊號系統來控制主進程。預設,nginx 將其主進程的 pid 寫入到 /usr/local/nginx/logs/nginx.pid 檔案中。通過傳遞參數給 ./configure 或使用 pid 指令,來改變該檔案的位置。
主進程可以處理以下的訊號:
| 命令 |
說明 |
備忘 |
| TERM, INT |
快速關閉 |
|
| QUIT |
從容關閉 |
|
| HUP |
重載配置 |
用新的配置開始新的背景工作處理序 從容關閉舊的背景工作處理序 |
| USR1 |
重新開啟記錄檔 |
|
| USR2 |
平滑升級可執行程式 |
|
| WINCH |
從容關閉背景工作處理序 |
|
- 預設目錄
主目錄:/usr/local/nginx/
配置目錄:/usr/local/nginx/conf/
root目錄:/usr/local/nginx/html/
可執行檔路徑:/usr/local/nginx/sbin/
2.FastCGI2.1.安裝lighttpd的spawn-fastcgi
下載http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
./configure make cp ./src/spawn-fcgi /usr/local/nginx/sbin/
2.2.安裝fastcgi庫
下載http://www.fastcgi.com/dist/fcgi.tar.gz
./configure make make install
2.3.Hello world
#include <iostream>#include <fcgi_stdio.h>
using namespace std;
int main(){
/* Initialization Code */
int count = 0;
/* Start of response loop */
while (FCGI_Accept() >= 0)
{
//* body of response loop /*/
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()); }
/* End of response loop */ return 0;
}
編譯後為FastCGISameple
2.4.啟動Spawn-cgi
/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 25 -u www -f /usr/local/nginx/fcgi/FactCGISample
2.5.修改Nginx設定檔
vi /usr/local/nginx/conf/nginx.conf
location ~ /.cgi$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
別忘了,重新載入設定檔
$sudo kill -HUP 26010
開啟瀏覽器,輸入http://localhost/1.cgi,就顯示
FastCGI Hello! (C, fcgi_stdio library)Request number 1 running on host localhost Process
ID: 25473
3.webbench壓力測試工具
下載:http://home.tiscali.cz:8080/~cz210552/distfiles/webbench-1.5.tar.gz
安裝:
tar zxvf webbench-1.5.tar.gzcd 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