1. 程式人生 > >Nginx技術深入剖析

Nginx技術深入剖析

錯誤頁 域名 單獨 好習慣 tab 有效 tin cgi 支持

Nginx軟件功能模塊說明

核心功能模塊(Core functionality):主要對應配置文件的Main區塊和Events區塊。

標準的http功能模塊:

企業 場景常用的Nginx http功能模塊匯總
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基本訪問狀態信息等的模塊

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.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區塊結束

企業場景中重啟Nginx後的檢測策略

在企業運維實踐場中,每一個配置操作處理完畢後都應該進行快速有效的檢查,這是一個合格運維人員的良好習慣。盡量使得在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

Nginx status結果詳解

Active connections: 1         #Nginx正處理的活動連接數有1個
server accepts handled requests   #第一個server表示Nginx啟動到現在共處理20個連接

                  #第二個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)

Nginx增加錯誤日誌(error_log)配置

日誌記錄屬於核心功能模塊(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

Nginx訪問日誌(access_log)

此功能由ngx_http_log_module模塊負責

控制日誌的參數
參數 說明
log_format 用來定義記錄日誌的格式(可以定義多種日誌格式,取不同的名字即可)
access_log 用來指定日誌文件的路徑及使用何種日誌格式記錄日誌
Nginx日誌變量說明
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"‘;

Nginx訪問日誌輪詢切割

[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等

Nginx技術深入剖析