Nginx常用功能配置及优化
規范優化Nginx配置文件:
----------------------------------------------------------------------------------------
Nginx的主配置文件為nginx.conf,主配置文件包含所有虛擬主機的子配置文件同一放到extra目錄中。
虛擬主機的配置文件按照網站的域名或功能取名。
使用include包含到Nginx主配置文件。
優化后示例:
mkdir /application/nginx/extra? ? ?#創建虛擬主機配置文件目錄
vim /application/nginx/conf/nginx.conf? ? ? #Nginx主配置文件
worker_processes? 1;
events {
? ? worker_connections? 1024;
}
http {
? ? include? ? ? ?mime.types;
? ? default_type? application/octet-stream;
? ? sendfile? ? ? ? on;
? ? keepalive_timeout? 65;
? ? include ../extra/www.conf;? ? ? ? ? ? ?#包含虛擬主機配置文件
? ? include ../extra/bbs.conf;? ? ? ? ? ? ?#包含虛擬主機配置文件
? ? include ../extra/pan.conf;? ? ? ? ? ? ?#包含虛擬主機配置文件
}
vim /application/nginx/extra/www.conf? ? ? #虛擬主機配置文件
server {
? ? ? ? listen? ? ? ?192.168.30.3;
? ? ? ? server_name? www.smartbro.com;
? ? ? ? location / {
? ? ? ? ? ? root? ?html/www;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
}
vim /application/nginx/extra/bbs.conf? ? ? #虛擬主機配置文件
server {
? ? ? ? listen? ? ? ?192.168.20.3;
? ? ? ? server_name? bbs.smartbro.com;
? ? ? ? location / {
? ? ? ? ? ? root? ?html/bbs;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
}
vim /application/nginx/extra/pan.conf? ? ? #虛擬主機配置文件
server {
? ? ? ? listen? ? ? ?192.168.10.3;
? ? ? ? server_name? pan.smartbro.com;
? ? ? ? location / {
? ? ? ? ? ? root? ?html/pan;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
}
/application/nginx/sbin/nginx -t? ? ? ? ? ?#檢查配置文件
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload? ? #平滑重啟Nginx
----------------------------------------------------------------------------------------
Nginx虛擬主機配置文件:
----------------------------------------------------------------------------------------
所謂虛擬主機別名,就是為虛擬主機設置除了主域名以外的一個或多個域名名字,這樣就能實現用戶訪問的多個域名對應同一個主機。
vim /application/nginx/extra/www.conf
server {
? ? ? ? listen? ? ? ?192.168.30.3;
? ? ? ? server_name? www.smartbro.com smart.com;? ? ? #設置別名
? ? ? ? location / {
? ? ? ? ? ? root? ?html/www;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
}
----------------------------------------------------------------------------------------
Nginx狀態信息功能實戰:
----------------------------------------------------------------------------------------
Nginx 軟件的功能模塊有一個ngx_http_stub_status_module模塊,這個模塊的主要功能就是記錄Nginx的基本訪問狀態信息的。
要是用該模塊的功能必須編譯安裝增加http_stub_status_module模塊。
/application/nginx/sbin/nginx -V? ? ? #查看編譯安裝的參數
nginx version: nginx/1.13.4
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.13.4/ --with-http_stub_status_module --with-http_ssl_module
配置Nginx status:
(1)生成狀態配置,并增加狀態配置參數:
vim /application/nginx/extra/status.conf? ? ?#創建新的配置文件
server {
? ? listen 192.168.10.3:8088;
? ? server_name status.smartbro.com;
? ? location / {
? ? ? ? stub_status on;? ? ? ? ? ? ? ? ? ? ? #打開狀態開關
? ? ? ? access_log? off;
? ? }
}
vim /application/nginx/conf/nginx.conf? ? ? ?#修改Nginx主配置文件
include ../extra/status.conf;? ? ? ? ? ? ? ? #將狀態配置文件包含
/application/nginx/sbin/nginx -t? ? ? ? ? ? ?#檢查配置文件
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx? ? ? ? ? ? ? ? #啟動Nginx
curl http://192.168.10.3:8088? ? ? ? ? ? ? ? #測試服務
Active connections: 1? ? ? ? ? ? ? ? ? ? ? ? #表示Nginx正處理的活動連接數
server accepts handled requests? ? ? ? ? ? ? #表示到現在總共創建的握手次數
?2 2 2
Reading: 0 Writing: 1 Waiting: 0
#Reading是Nginx讀取客戶端的Header信息次數
#Writing是Nginx返回給客戶端的Header信息次數
#Waiting是Nginx已經處理完正在等候下一次請求指令的駐留連接
#在開啟Keepalived的情況下,Waiting的值等于active - (reading - writing)
----------------------------------------------------------------------------------------
Nginx錯誤日志:
----------------------------------------------------------------------------------------
屬于核心模塊(ngx_core_module)的參數,參數名是error_log,可以放到Main區塊的全局配置,也可以放到不同的虛擬主機中。
語法格式:
error_log? ? ? file? ? ?level;
關鍵字? ? ? ? ?日志文件 等級
日志級別常見有:debug info notice warn error crit alert emerg
生產場景一般是:warn error crit
使用debug或info時會帶來巨大的磁盤I/O消耗
error_log的默認配置是:
error_log logs/error.log error;
可以放置的標簽段:
main http server location
配置Nginx錯誤日志:
vim /application/nginx/conf/nginx.conf
error_log logs/error.log error;
/application/nginx/sbin/nginx -t? ? ? ? ? ?#檢查配置文件語法
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload? ? #平滑重啟Nginx
----------------------------------------------------------------------------------------
Nginx訪問日志:
----------------------------------------------------------------------------------------
功能由ngx_http_log_module模塊負責。
log_format:用來定義記錄日志的格式
access_log:用來指定日志文件的路徑和使用日志格式記錄文件
默認的格式:
log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
訪問日志配置說明:
(1)日志格式的定義說明
log_format是關鍵字
main用來指定日志格式的標簽
Nginx日志文件的變量說明:
+------------------------------------------------------------------------------------------------------+
|$remote_addr? ? ? ? ? ? ? ? ? |記錄訪問網站的客戶端地址? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+------------------------------------------------------------------------------------------------------+
|$http_x_forwarded_for|當前端有代理服務器的時候,設置Web節點記錄客戶端得我配置,此參數生? |
|? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|效的前提是代理服務器上也進行相關的x_forwarded_for設置? ? ? ? ? ? ? ? ? ? ? ?|
+------------------------------------------------------------------------------------------------------+
|$remote_user? ? ? ? ? ? ? ? |遠程客戶端用戶名稱? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+------------------------------------------------------------------------------------------------------+
|$request? ? ? ? ? ? ? ? ? ? ? ?|用戶的HTTP請求起始行信息? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+------------------------------------------------------------------------------------------------------+
|$status? ? ? ? ? ? ? ? ? ? ? ? ?|HTTP狀態碼,記錄請求返回的狀態? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+------------------------------------------------------------------------------------------------------+
|$body_bytes_sent? ? ? ?|服務器發送給客戶端的響應body字節數? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+------------------------------------------------------------------------------------------------------+
|$http_referer? ? ? ? ? ? ? ?|記錄此次請求是從哪個鏈接訪問過來的,可以根據referer進行防盜鏈設置? ? ?|
+------------------------------------------------------------------------------------------------------+
|$http_user_agent? ? ? ?|記錄客戶端訪問信息? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+------------------------------------------------------------------------------------------------------+
在沒有特殊情況的前提下,采用默認配置即可。
記錄日志的access_log參數說明:
access_log path [format [buffer=size] [flush=time] [if=condition]];
access_log path format gzip=[level] [buffer=size] [flush=time] [if=condition];
access_log syslog:server=address [,parameter=value] [format [if=condition]];
buffer=size是存放訪問日志的緩沖區大小
flush=time是講緩沖區的日志刷新到磁盤的時間
gzip[=level]是壓縮級別
[if=condition]表示其他條件
一般情況下,這些參數都無需配置,極端優化時才會考慮這些參數。
默認配置:
access_log logs/access.log combined;
放置位置:
http server? location? if in location? limit_eccept
訪問日志配置實戰:
vim /application/nginx/conf/nginx.conf? ? #修改主配置文件
worker_processes? 1;
error_log logs/error.log error;
events {
? ? worker_connections? 1024;
}
http {
? ? include? ? ? ?mime.types;
? ? default_type? application/octet-stream;
? ? sendfile? ? ? ? on;
? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '? ? ?#使用默認的配置
? ? ? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
? ? keepalive_timeout? 65;
? ? include ../extra/www.conf;
? ? include ../extra/bbs.conf;
? ? include ../extra/pan.conf;
? ? include ../extra/status.conf;
}
vim /application/nginx/extra/www.conf? ? ? #修改各個虛擬主機的配置文件
server {
? ? ? ? listen? ? ? ?192.168.30.3;
? ? ? ? server_name? www.smartbro.com smart.com;
? ? ? ? location / {
? ? ? ? ? ? root? ?html/www;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
? ? ? ? access_log logs/access_www.log main;? ? ?#開啟日志,默認使用combined格式距離日志
}
vim /application/nginx/extra/pan.conf? ? ? #修改各個虛擬主機的配置文件
server {
? ? ? ? listen? ? ? ?192.168.10.3;
? ? ? ? server_name? pan.smartbro.com;
? ? ? ? location / {
? ? ? ? ? ? root? ?html/pan;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
? ? ? ? access_log logs/access_pan.log main;? ? ?#開啟日志,默認使用combined格式距離日志
}
vim /application/nginx/extra/bbs.conf? ? ? #修改各個虛擬主機的配置文件
server {
? ? ? ? listen? ? ? ?192.168.20.3;
? ? ? ? server_name? bbs.smartbro.com;
? ? ? ? location / {
? ? ? ? ? ? root? ?html/bbs;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
? ? ? ? access_log logs/access_bbs.log main;? ? ?#開啟日志,默認使用combined格式距離日志
}
/application/nginx/sbin/nginx -t? ? ? ? ? #檢查配置文件語法
nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload? ?#平滑重啟Nginx
使用谷歌瀏覽器訪問,出現日志:
192.168.10.1 - - [10/Aug/2017:23:38:48 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3159.5 Safari/537.36" "-"
可以在記錄日志參數中加入buffer和flush選項,這樣可以在高并發場景下提升網站訪問性能。
----------------------------------------------------------------------------------------
Nginx訪問日志輪詢切割:
----------------------------------------------------------------------------------------
默認情況Nginx會把所有的日志生成到一個指定的文件,時間一長日志的個頭很大,不利于日志的分析。
使用腳本加上計劃任務進行定時切割:
mkdir -p /server/scripts/? ? ? #創建專門存放系統腳本的目錄
vim /server/scripts/cut_log_nginx.sh
#!/bin/bash
DateFormat=`date +%Y-%m-%d`
BaseDir="/application/nginx"
NginxDir="$BaseDir/logs/"
LogName="access_www"
[ -d $NginxDir ] && cd $NginxDir || exit 1
[ -f ${LogName}.log ] || exit 1
/bin/mv ${LogName}.log ${DateFormat}_${LogName}.log
$BaseDir/sbin/nginx -s reload
vim /var/spool/cron/root? ? ? ?#編輯計劃任務文件
00 00 * * * /bin/bash /server/scrpits/cut_log_nginx.sh > /dev/null 2>&1
常見的日志分析工具有:
rsyslog? ? awstats? ? flume? ? ELK? ? storm
?本文轉自 棋帥小七 51CTO博客,原文鏈接:http://blog.51cto.com/xvjunjie/1955309
總結
以上是生活随笔為你收集整理的Nginx常用功能配置及优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iptables之xtables_add
- 下一篇: Nginx的location区块的作用及