中文wiki社區:http://wiki.codemongers.com/NginxChs
一 . nginx安裝
1. 下載nginx : http://sysoev.ru/nginx/download.html(官方下載頁面)
wget http://sysoev.ru/nginx/nginx-0.7.19.tar.gz
2. 依賴模塊下載
gzip 模塊需要 zlib 庫 (http://www.zlib.net/)
rewrite 模塊需要 pcre 庫 (http://www.pcre.org/)
ssl 功能需要 openssl 庫 (http://www.openssl.org/)
./configure --prefix=/home/ljh/program/nginx --with-http_stub_status_module --with-pcre=./external/pcre-7.8 --with-zlib=./external/zlib-1.2.3 --with-openssl=./external/openssl-0.9.8i
make & make install
通過信號對 Nginx 進行控制
Nginx 支持下表中的信號:
信號名 作用描述
TERM, INT 快速關閉程序,中止當前正在處理的請求
QUIT 處理完當前請求后,關閉程序
HUP 重新加載配置,并開啟新的工作進程,關閉就的進程,此操作不會中斷請求
USR1 重新打開日志文件,用于切換日志,例如每天生成一個新的日志文件
USR2 平滑升級可執行程序
WINCH 從容關閉工作進程
有兩種方式來通過這些信號去控制 Nginx,第一是通過 logs 目錄下的 nginx.pid 查看當前運行的 Nginx 的進程 ID,
通過 kill – XXX <pid> 來控制 Nginx,其中 XXX 就是上表中列出的信號名。如果您的系統中只有一個 Nginx 進程,那您也可以通過 killall 命令來完成,例如運行 killall – s HUP nginx 來讓 Nginx 重新加載配置。
killall -s HUP nginx
二 nginx 配置
1. 監控配置
location /nginx_status {
# copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/
stub_status on;
access_log off;
allow SOME.IP.ADD.RESS;
deny all;
}
頁面結果解釋:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
active connections -- 對后端發起的活動連接數
server accepts handled requests -- nginx 總共處理了 16630948 個連接, 成功創建 16630948 次握手 (證明中間沒有失敗的), 總共處理了 31070465 個請求 (平均每次握手處理了 1.8個數據請求)
reading -- nginx 讀取到客戶端的Header信息數
writing -- nginx 返回給客戶端的Header信息數
waiting -- 開啟 keep-alive 的情況下,這個值等于 active - (reading + writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連接
2. 頁面緩存配置 http://wiki.codemongers.com/NginxChsMemcachedModule
server {
location / {
set $memcached_key $uri;
memcached_pass name:11211;
default_type text/html;
error_page 404 = /fallback;
}
location = /fallback {
proxy_pass backend;
}
3. 頁面配置IP訪問列表 http://wiki.codemongers.com/NginxHttpAccessModule
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
4. 配置頁面訪問控制
location / {
auth_basic "Restricted";
} auth_basic_user_file conf/htpasswd;
htpasswd格式為 用戶名:密碼。你可以使用來自 Apache 的 htpasswd 工具來創建密碼文件。
5. 限制每個IP的并發數 http://wiki.codemongers.com/NginxChsHttpLimit_zoneModule
limit_zone one $binary_remote_addr 10m;
server {
location /download/ {
limit_conn one 1;
}
}
定義一個叫“one”的記錄區,總容量為 10M,以變量 $binary_remote_addr 作為會話的判斷基準(即一個地址一個會話)。 限制 /download/ 目錄下,一個會話只能進行一個連接。
簡單點,就是限制 /download/ 目錄下,一個IP只能發起一個連接,多過一個,一律503
6. 代理模塊 http://wiki.codemongers.com/NginxChsHttpProxyModule
location / {
proxy_pass http://localhost:8000/hello;
proxy_redirect http:/localhost:8000/hello/ /;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 60
proxy_connect_timeout 60
}
配置項介紹:
daemon on | off 缺省值: on
可以在開發時開啟,但必須在真實環境中設置為off
debug_points [stop | abort] 缺省值: none
可以在debugger上停止nginx應用
error_log file [ debug | info | notice | warn | error | crit ] 缺省值: ${prefix}/logs/error.log
include vhosts/*.conf; 缺省值: none
如果配置文件很長,你可以在任意地方使用include指令實現配置文件的包含。*.conf匹配所有以.conf結尾的文件
lock_file /var/log/lock_file;
nginx采用以異步互斥進行訪問控制
master_process on | off 缺省值: on
和dameon on 都是在開發時使用
pid /var/log/nginx.pid;
進程id存儲文件。可以使用 kill -HUP cat /var/log/nginx.pid\ 對Nginx進行配置文件重新加載。
user user [group]
指定Nginx Worker進程運行用戶,默認是nobody帳號。
worker_processes number 缺省值: 1
配置工作進程。max_clients = worker_processes * worker_connections
worker_priority [-]number
設置工作進程的優先級
worker_cpu_affinity 0001 0010 0100 1000;
綁定worker進行到四個CPU
worker_rlimit_core size
指定每個進程的文件限制
access_log path [format [buffer=size]] | off 默認值: access_log log/access.log combined
指令 access_log 指派路徑、格式和緩存大小。參數 "off" 將清除當前級別的所有 access_log 指令。如果未指定格式,則使用預置的 "combined" 格式。緩存不能大于能寫入磁盤的文件的最大大小。在 FreeBSD 3.0-6.0 ,緩存大小無此限制。
log_format name format [format ...] 默認值: log_format combined "..."
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $apache_bytes_sent '
'"$http_referer" "$http_user_agent"';
expires [time|epoch|max|off] 默認值: expires off
使用本指令可以控制HTTP應答中的“Expires”和“Cache-Control”的頭標,(起到控制頁面緩存的作用)。
可以在time值中使用正數或負數。“Expires”頭標的值將通過當前系統時間加上您設定的 time 值來獲得。
epoch 指定“Expires”的值為 1 January, 1970, 00:00:01 GMT。
max 指定“Expires”的值為 31 December 2037 23:59:59 GMT,“Cache-Control”的值為10年。
“Cache-Control”頭標的值由您指定的時間來決定:
負數:Cache-Control: no-cache
正數或零:Cache-Control: max-age = #, # 為您指定時間的秒數。
"off" 表示不修改“Expires”和“Cache-Control”的值
待補充。。。。
Blog : http://agapple.javaeye.com/ 歡迎訪問