Nginx
基礎(chǔ)篇
Nginx的基本情況
- Nginx作為新一代的HTTP服務(wù),解決了c10k問(wèn)題,同時(shí)還可以作為反向代理、IMAP、POP3和SMTP程序
- Nginx的優(yōu)點(diǎn):I/O多路復(fù)用,節(jié)省系統(tǒng)資源。
epoll - 關(guān)于
epoll:其為Nginx中實(shí)現(xiàn)I/O多路復(fù)用的方法,經(jīng)過(guò)select和poll的演化,epoll當(dāng)前的版本可以做到線程的安全性,并在實(shí)際使用中告知具體哪個(gè)sock有數(shù)據(jù)。具體的特點(diǎn)為:異步非阻塞。
Nginx的部署
YUM
配置yum源如下
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
編譯安裝及參數(shù)
- configure arguments 配置參數(shù)
- --prefix=/etc/nginx 配置文件路徑
- --sbin-path=/usr/sbin/nginx 程序文件
- --modules-path=/usr/lib64/nginx/modules
- --conf-path=/etc/nginx/nginx.conf
- --error--log-path=/var/log/nginx/error.log
- --http-log-path=/var/log/nginx/access.log
- --pid-path=/var/run/nginx.pid
- --lock-path=/var/run/nginx.lock
- --http-proxy-temp-path=/var/cache/nginx/proxy_temp
- --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
- --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
- --http-scgi-temp-path=/var/cache/nginx/scgi_temp
- --user=nginx
- --group=nginx
- --with-compat
- --with-file-aio
- --with-threads
- --with-http_addition_module
- --with-http_auth_request_module
- --with-http_dav_module
- --with-http_flv_module
- --with-http_gunzip_module
- --with-http_gzip_static_module
- --with-http_mp4_module
- --with-http_mp4_module
- --with-http_random_index_module
- --with-http_realip_module
- --with-http_secure_link_moodule
- --with-http_slice_link_module
- --with-http_ssl_module
- --with-http_stub_status_module
- --with-http_sub_module
- --with-http_v2_module
- --with-mail
- --with-mail_ssl_module
- --with-stream
- --with-stream_realip_module
- --with-stream_ssl_module
- --with-stream_ssl_preread_module
- --with-cc-opt='-O2 -g -pipr -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --parram=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
- --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pir' 1l
與Nginx相關(guān)的全部文件
-
/etc/logrotate.d/nginx日志輪轉(zhuǎn) -
/etc/nginx配置文件總目錄 -
/etc/nginx/conf.d自配置文件文件夾 - /etc/nginx/conf.d/default.conf 默認(rèn)的網(wǎng)站配置文件
- /etc/nginx/fastcgi_params
- /etc/nginx/scgi_params
- /etc/nginx/uwsgi_params
- /etc/nginx/koi-utf 字符集,文件編碼
- /etc/nginx/win-utf
- /etc/nginx/koi-win
- /etc/nginx/mime.types 關(guān)聯(lián)程序,如網(wǎng)站的文件類(lèi)型和相關(guān)的處理程序
- /etc/nginx/modules 模塊文件夾,第三方模塊等
-
/etc/nginx/nginx.conf主配置文件 -
/usr/lib/systemcd/system/nginx.service.systemctl服務(wù)腳本 -
/usr/sbin/nginx主程序 - /usr/share/doc/nginx-1.12.1 文檔
- /usr/share/man/man8/nginx.8.gz
-
/usr/share/nginx/html/index.html默認(rèn)主頁(yè) - /var/cache/nginx nginx緩存文件
- /var/log/nginx/ 存放nginx日志的目錄
Nginx的基本配置
主配置文件內(nèi)容詳解
user nginx;
worker_processes 1;
#啟動(dòng)的worker進(jìn)程的數(shù)量(CPU數(shù)量一致或auto)
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events{
use epoll;
#事件驅(qū)動(dòng)模型epoll
worker_connections 1024;
#每個(gè)worker進(jìn)程允許處理的最大連接數(shù),通常要改大
}
http{
include /etc/nginx/mime.types;
#文檔和長(zhǎng)須的關(guān)聯(lián)記錄
default_type application/octet-stream;
#字節(jié)流處理方式
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 /var/log/nginx/access.log main;
sendfile on;
#優(yōu)化參數(shù),高效傳輸文件的模式
#tcp_nopush on;
#TCP停止數(shù)據(jù)包推送,等積累到最大的時(shí)候一起推送到Client
keepalive_timeout 65;
#長(zhǎng)連接時(shí)間
#gzip on;
#壓縮
include /etc/nginx/conf.d/*.conf;
#包含自配置目錄
}
虛擬主機(jī)的配置文件/etc/nginx/conf.d/default.conf
server{
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.html;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50.html {
root /usr/share/nginx/html;
}
#proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#location ~ \.php${
#root html;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scipts$fastcgi_script_name;
#include fastcgi_params;
}
#deny access to .htacess files,if Apaches's document root
#concurs with nginx's one
#location ~ /\.ht{
#deny all;
}
}
新建一個(gè)簡(jiǎn)單的虛擬主機(jī)/etc/nginx/conf.d/new.conf
server{
listen 80;
server_name new.com;
location / {
root /new;
index index.html index.htm;
}
}
- 使用時(shí)要配置本地解析
日志配置
簡(jiǎn)介
Nginx 中有非常靈活的日志記錄模式,每個(gè)級(jí)別的配置可以有各自獨(dú)立的訪問(wèn)日志。若要使用日志系統(tǒng)需要加載日志模塊 ngx_http_log_module(默認(rèn)開(kāi)啟)
常見(jiàn)變量意義
log_format
#日志格式
access_log
#訪問(wèn)日志
error_log
#錯(cuò)誤日志
open_log_file_cache
#日志緩存
- 關(guān)于日志緩存的配置
open_log_file_cache max=N[inactive=time] [min_uses=N] [valid=time] | off
#該指令默認(rèn)是禁止的
open_log_file_cache off;
#其中參數(shù)的描述符說(shuō)明如下
#max:緩存中最大的文件的描述符數(shù)量
#inactive:設(shè)置一個(gè)事件,如果在設(shè)置的事件內(nèi)沒(méi)有使用此文件描述符,則自動(dòng)刪除此描述符。
#min_uses:在參數(shù)inactive指定的時(shí)間范圍內(nèi),若文件日志超過(guò)被使用的次數(shù),則將該日志文件的描述符記入緩存。
#valid:設(shè)置多長(zhǎng)時(shí)間檢查一次,檢查內(nèi)容為指定的日志文件路徑與文件名。
#推薦配置如下
open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
日志的格式和命令
主配置文件中的log_format將規(guī)定本機(jī)中全部的Nginx的日志格式的記錄形式。
#基本樣式
log_format conbined '$remote_addr - $remote_user [$time_local]'
'"$request" $statues $body_bytes_sent'
'"$http_referer" "$http_user_agent"';
日志中允許包含的標(biāo)量
$romote_addr,$http_x_forwarded_for
#分別為CIP 和 DIP
$remote_user
#遠(yuǎn)程用戶:記錄客戶端的名稱(chēng)
[$time_local]
#本地時(shí)間
$request
#請(qǐng)求:URL和HTTP協(xié)議
$status
#狀態(tài):記錄請(qǐng)求狀態(tài)
$body_bytes_sent
#發(fā)送給客戶端的字節(jié)數(shù),不包含響應(yīng)頭的大小
$bytes_sent
#發(fā)送給客戶端的總字節(jié)數(shù)
$msec
#日志寫(xiě)入時(shí)間。單位微妙,保留三位
$http_referer
#記錄從哪個(gè)鏈接跳轉(zhuǎn)過(guò)來(lái)的
$http_user_agen
#記錄客戶端瀏覽器的相關(guān)信息
$request_length
#請(qǐng)求的長(zhǎng)度,包括請(qǐng)求行,請(qǐng)求頭和請(qǐng)求正文
$request_time
#請(qǐng)求處理的事件,單位為秒,保留三位。從讀入客戶端的第一個(gè)字節(jié)開(kāi)始到發(fā)送完最后一個(gè)字節(jié)結(jié)束
$time_iso8601
#ISO8601標(biāo)準(zhǔn)格式下的本地時(shí)間
訪問(wèn)日志和錯(cuò)誤日志
即為access_log 和 error_log
日志緩存
- 存在的意義:每次日志寫(xiě)入操作實(shí)際上是一次I/O操作,大量的日志寫(xiě)入會(huì)使得服務(wù)器性能下降。為了能夠提高服務(wù)器的可用性,先將日志緩存下來(lái),之后再批量寫(xiě)入。
- 具體操作見(jiàn)上。
日志輪轉(zhuǎn)和切割
- Nginx的安裝,會(huì)默認(rèn)啟用日志輪轉(zhuǎn)。
- Nginx自身的日志輪轉(zhuǎn)規(guī)則寫(xiě)入到/etc/logrotate.d/nginx
create 0644 nginx nginx #創(chuàng)建愛(ài)你新的日志文件,屬主
daily #天
rotate 10 #保留的份數(shù)
missingok #丟失不提醒
notifempyt #若為空文件則不輪轉(zhuǎn)
compress #壓縮
sharedsripts #輪轉(zhuǎn)后腳本
postrotate
/bin/kill -USR1 `cat/usr/ningx.pid 2>/dev/null || true endsript`
日志分析
- 常用的字段有
$remote_addr $1
$time_local $4
$request $7
$status $9
$body_bytes_sent $10
案例實(shí)際操作
#統(tǒng)計(jì) 2018年9月5日 的PV量
grep '05/Sep/2018' access.log | wc -l
#統(tǒng)計(jì) 2018年9月5日 一天之中訪問(wèn)最多的10個(gè)IP
grep '05/Sep/2018' access.log | awk '{ips[$1]++} END{for(i in ips){print i,ips[i]}}' | sort -k2 -rn | head -n10
#統(tǒng)計(jì) 2018年9月5日 每個(gè)URL訪問(wèn)內(nèi)容的總大小
grep '05/Sep/2018' access.log | awk '{urls[$7]++;size[$7]+=$10}' END{for(i in urls){print urls[i],size[i],i}} | srot -k1 -rn | head -n10
#統(tǒng)計(jì) 2018年9月5日 IP訪問(wèn)碼為404及出現(xiàn)的次數(shù)($status)
grep '05/Sep/2018' error.log | awk '{if($9="404"){ip_code[$1" "$9]++}} END{for(i in ip_code){print i,ip_code[i]}}'
#統(tǒng)計(jì)前一分鐘PV量
date=$(date -d '-1 minute' +%d/%b/%Y:%H:%M);awk -v date=$date '$0 ~ $date{i++} END{print i}' access.log
Nginx WEB模塊
- 連接狀態(tài)
stub_status_module- 目的 展示Nginx的連接狀態(tài)
- 查詢 nginx -V 2>&1 | grep stub_status
- 啟動(dòng)狀態(tài)模塊
server{ location /nginx_status{ stu_status; allow all; } }- 查詢示例
Active connections: 1 #當(dāng)前活動(dòng)的連接數(shù) server accepts handled requests 17 17 24 # 第一個(gè)17:總連接數(shù)(TCP) 第二個(gè)17(TCP) 成功的鏈接數(shù) 24:總共處理的請(qǐng)求數(shù)(HTTP) Reading: 0 Writing: 1 Wating:0 #Reading請(qǐng)求頭 Writing 響應(yīng)頭 Waiting等待的請(qǐng)求數(shù),開(kāi)始了Keepalive - 隨機(jī)主頁(yè)
random_index_module- 目的 將主頁(yè)設(shè)置成隨機(jī)頁(yè)面,是一種微調(diào)更新機(jī)制
- 啟動(dòng)隨機(jī)主頁(yè)模塊
server{ location / { #root /usr/share/nginx/html; #index index.html; root /app; random_index on; #隱藏文件不會(huì)被作為隨機(jī)主頁(yè)顯示 } } - 替換模塊
sub_module- 目的 網(wǎng)頁(yè)內(nèi)容替換 針對(duì)模板生成網(wǎng)頁(yè)時(shí)出現(xiàn)的批量錯(cuò)誤
- 啟動(dòng)替換1
server{ sub-filter nginx'aaa'; sub_filter_once on; location / { ... } } - 文件讀取
ngx_http_core_module- 默認(rèn)啟動(dòng)提升傳輸效率
- 文件壓縮
ngx_http_gzip_module- 語(yǔ)法
Syntax: gzip on|off; Default: gzip off; Context: http,server,location,if in location Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http,server,location #壓縮等級(jí) Syntax: gzip_http_version 1.0 | 1.1; Default: gzip_hjttp_version 1.1 Context: http,server,location- 啟動(dòng)模塊,傳輸前壓縮,提高傳輸效率。
- 啟動(dòng)
http{ gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_static on; } - 瀏覽器緩存
ngx_http_headers_module- 語(yǔ)法
Syntax: expires [modified] time; Default: expires off; Context: http,server,location,if in location- 開(kāi)啟
location / { root /app/aaa.com; index index.html expires 24h; } - 防盜鏈
ngx_http_referer_module- 語(yǔ)法
Syntax: valid_referers none | blocked | server_names| string ...; Default: -- Context: server,location- 啟用
location / { valid_referers none blocked *.a.com; if ($invalid_referer){ return 403; } }