核心功能模塊(Core functionality):主要對應配置文件的Main區塊和Events區塊。php
標準的http功能模塊:html
ngx_http_core_module | 包括一些核心的http參數配置,對應Nginx的配置爲HTTP區塊部分 |
ngx_http_access_module | 訪問控制模塊,用來控制網站用戶對Nginx的訪問 |
ngx_http_gzip_module | 壓縮模塊,對Nginx返回的數據壓縮,術語性能優化模塊 |
ngx_http_fastcgi_module | FastCGI模塊,和動態應用相關的模塊,例如PHP |
ngx_http_proxy_module | proxy代理模塊 |
ngx_http_upstream_module | 負載均衡模塊,能夠實現網站的負載均衡功能及節點的健康檢查 |
ngx_http_rewrite_module | URL地址重寫模塊 |
ngx_http_limit_conn_module | 限制用戶併發鏈接數及請求數模塊 |
ngx_http_limit_req_module | 根據定義的key限制Nginx請求過程的速率 |
ngx_http_log_module | 訪問日誌模塊,以指定的格式記錄Nginx客戶訪問日誌等信息 |
ngx_http_auth_basic_module | Web認證模塊,設置Web用戶經過帳號、密碼訪問Nginx |
ngx_http_ssl_module | ssl模塊,用於加密的http鏈接,如https |
ngx_http_stub_status_module | 記錄Nginx基本訪問狀態信息等的模塊 |
├── client_body_temp
├── conf #Nginx全部配置文件的目錄
│ ├── fastcgi.conf #fastcgi相關參數的配置文件
│ ├── fastcgi.conf.default #fastcgi.conf的原始配置文件
│ ├── fastcgi_params #fastcgi的參數文件
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types #媒體類型
│ ├── mime.types.default
│ ├── nginx.conf #Nginx默認的主配置文件
│ ├── nginx.conf.02
│ ├── nginx.conf.default
│ ├── scgi_params #scgi參數文件,通常用不到
│ ├── scgi_params.default
│ ├── uwsgi_params #uwsgi參數文件,通常用不到
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp #fastcgi臨時數據目錄
├── html #編譯安裝Nginx默認站點目錄,相似Apache的默認站點 #htdocs目錄
│ ├── 50x.html #錯誤頁面優雅替代顯示文件,例如出現502錯誤時,會調前端
#調用此頁面,error_page 500 502 503 504 /50x.html
│ ├── index.html #默認的首頁文件,在實際環境中,你們習慣用(不是必 #須)index.html,index.php,index.jsp來作網站的首頁文 #件,首頁文件名在nginx.conf中事先定義好的。
│ └── index.html.2017-10-22
├── logs #Nginx默認的日誌路徑,包括錯誤日誌及訪問日誌
│ ├── access.log #這是Nginx的默認訪問日誌文件,使用tail -f access.log能夠 #實現觀看用網站用戶的訪問狀況信息
│ ├── error.log #Nginx的錯誤日誌文件,若是Nginx出現啓動故障等問題,nginx
#能夠經過查看這個文件查明緣由。
│ └── nginx.pid #Nginx的pid文件,Nginx進程啓動後,會把進程的ID號寫入。
├── proxy_temp #臨時目錄
├── sbin #這是Nginx的命令目錄,如Nginx的啓動命令nginx
│ └── nginx
├── scgi_temp #臨時目錄
└── uwsgi_temp #臨時目錄瀏覽器
[root@localhost conf]# egrep -v "#|^$" nginx.conf
worker_processes 1; #worker進程的數量
events { #事件區塊的開始
worker_connections 1024; #每一個worker進程支持的最大鏈接數
} #事件區塊的結束
http { #http區塊開始
include mime.types; #nginx支持的媒體類型庫文件
default_type application/octet-stream; #默認的媒體類型
sendfile on; #開啓高效傳輸模式
keepalive_timeout 65; #鏈接超時
server { #第一個Server區塊開始,表示一個獨立的虛擬主機站點
listen 80; #提供服務的端口,默認80
server_name localhost; #提供服務的域名主機名
location / { #第一個location區塊的開始
root html; #站點的根目錄,至關於Nginx的安裝目錄
index index.html index.htm; #默認的首頁文件,多個用空格分開
} #第一個location區塊結束
error_page 500 502 503 504 /50x.html;#出現對應http狀態碼時,使用50x.html迴應客戶性能優化
location = /50x.html { #location區塊開始,訪問50x.html
root html;
}
}
} #http區塊結束bash
在企業運維實踐場中,每個配置操做處理完畢後都應該進行快速有效的檢查,這是一個合格運維人員的良好習慣。儘可能使得在Nginx啓動的同時,還會調用腳本經過獲取header信息或模擬用戶訪問指定URL(wget等方式)來自動檢查Nginx是否正常,最大限度的保證服務重啓後,能迅速肯定網站狀況,而無須手工敲命令查看。這樣若是配置有問題就能夠迅速使用上一版本備份配置文件覆蓋回來。服務器
[root@localhost conf]# cat check_url.sh
#!/bin/bash
#author:chenfu 2017-10-22 QQ532088799
#--------function split--------
. /etc/rc.d/init.d/functions
function checkURL()
{
checkUrl=$1
echo 'check url start ...'
judge=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1 | tr " " "\n"))
if [[ "${judge[1]}" == '200' && "${judge[2]}" == 'OK' ]]
then
action "${checkUrl}" /bin/true
else
action "${checkUrl}" /bin/false
echo -n "retrying again...";sleep 3;
judgeagain=($(curl -I -s --connect-timeout 2 ${checkUrl}| head -1| tr "\r" "\n"))
if [[ "${judgeagain[1]}" == '200' && "${judgeagain[2]}" == 'OK' ]]
then
action "${chekcUrl}, retried again" /bin/true
else
action "${chekcUrl}, retried again" /bin/false
fi
fi
sleep 1;
}
# usage method
checkURL http://www.etiantian.org併發
Active connections: 1 #Nginx正處理的活動鏈接數有1個
server accepts handled requests #第一個server表示Nginx啓動到如今共處理20個鏈接app
#第二個accepts表示Nginx啓動到如今共成功建立20次握 #手
#第三個handled requests表示共處理了23次請求
20 20 23
Reading: 0 Writing: 1 Waiting: 0 #Reading爲Nginx讀取到客戶端的Header信息數
#Writing爲Nginx返回給客戶端的Header數
#Waiting爲Nginx已經處理完正等候下一次請求指令的駐 #留鏈接。在開啓keepalive的狀況下
#這個值等於active-(reading+writing)
日誌記錄屬於核心功能模塊(ngx_core_module)的參數,該參數名字是error.log,針對本身來講最好定義在Main全局區塊中,固然也能夠放置不一樣的虛擬主機中單獨記錄。
error_log file level[debug|info|notice|warn|error|crit|alert|emerg];
關鍵字 日誌文件 錯誤日誌級別
error_log默認配置:
#default:error_log logs/error.log error;
能夠放置的標籤段:
#context:main,http,server,location
此功能由ngx_http_log_module模塊負責
參數 | 說明 |
log_format | 用來定義記錄日誌的格式(能夠定義多種日誌格式,取不一樣的名字便可) |
access_log | 用來指定日誌文件的路徑及使用何種日誌格式記錄日誌 |
Nginx日誌變量 | 說明 |
$remote_addr | 記錄訪問網站的客戶端地址 |
$http_x_forwarded_for |
當前端有代理服務器時,設置Web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器上也進行了相關的x_forwarded_for設置 |
$remote_user | 遠程客戶端名稱 |
$time_local | 記錄訪問時間與時區 |
$request | 用戶的http請求起始行信息 |
$status | http狀態碼,記錄請求返回的狀態,例如200,404,301 |
$body_bytes_sents | 服務器發送給客戶端的響應body字節數 |
$http_referer | 記錄這次請求是從哪一個連接訪問過來的,能夠根據referer進行防盜鏈設置 |
$http_user_agent | 記錄客戶端訪問信息,例如瀏覽器手機客戶端等 |
buffer=size #存放訪問日誌的緩衝區大小
flush=time #將緩衝區的日誌刷到磁盤的時間
gzip[=level] #表示壓縮級別
[if=condition] #表示其餘條件(通常的場景中這些都無需配置,極端優化 #時纔可能會考慮這些參數)
默認的配置:
access_log logs/access.log combined;
日誌內容配置:
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
[root@localhost nginx]# cat /server/script/cut_nginx_log.sh
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
Nginx經常使用的日誌收集分析工具備rsyslog、awstats、flume、ELK、storm等