Nginx訪問日志(access_log)配置及信息詳解
通過訪問日志,可以知曉用戶的地址,網站的哪些部分最受歡迎,用戶的瀏覽時間,對大多數用戶用的的瀏覽器做出針對性優化。
Nginx訪問日志(access_log)介紹
Nginx會把每個用戶訪問往咱的日志信息記錄到指定的日志文件里,供網站管理員分析用戶瀏覽行為等,此功能由ngx_http_log_module模塊負責。
訪問日志參數
Nginx訪問日志主要有兩個參數控制
log_format#用來定義記錄日志的格式(可以定義多種日志格式,取不同名字即可)
access_log#用來指定日至文件的路徑及使用的何種日志格式記錄日志
lof_format的默認值:
#??? 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的默認值:
#access_log? logs/access.log? main;
log_format語法格式及參數語法說明如下:
log_format;
????關鍵字??????? ?格式標簽? ?日志格式
????關鍵字:其中關鍵字error_log不能改變
????格式標簽:格式標簽是給一套日志格式設置一個獨特的名字
????日志格式:給日志設置格式
log_format格式變量:
??? $remote_addr? #記錄訪問網站的客戶端地址
??? $remote_user? #遠程客戶端用戶名
??? $time_local? #記錄訪問時間與時區
??? $request? #用戶的http請求起始行信息
??? $status? #http狀態碼,記錄請求返回的狀態碼,例如:200、301、404等
??? $body_bytes_sent? #服務器發送給客戶端的響應body字節數
??? $http_referer? #記錄此次請求是從哪個連接訪問過來的,可以根據該參數進行防盜鏈設置。
??? $http_user_agent? #記錄客戶端訪問信息,例如:瀏覽器、手機客戶端等
$http_x_forwarded_for?#當前端有代理服務器時,設置web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器也要進行相關的x_forwarded_for設置
access_log語法格式及參數語法說明如下:
access_log;
????關鍵字??????? ?日志文件? ?格式標簽
????關鍵字:其中關鍵字error_log不能改變
????日志文件:可以指定任意存放日志的目錄
????格式標簽:給日志文件套用指定的日志格式
其他語法:
??? access_log??? off;? #關閉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]? #表示其他條件
一般場景這些參數都無需配置,極端優化才有可能會考慮這些參數。
lof_format參數的標簽段位置:
http
access_log參數的標簽段位置:
http, server, location, if in location, limit_except
參考資料:http://nginx.org/en/docs/http/ngx_http_log_module.html
Nginx配置訪問日志過程介紹
(1)創建log_format語句
vi conf/nginx.conf
#vi編輯nginx主配置文件,添加標簽為main的log_format格式(http標簽內,在所有的server標簽內可以調用)
文件內容:
worker_processes? 1;
error_log logs/error.log error;
events {
??? worker_connections? 1024;
}
http {
??? include status.conf;
??? include?????? mime.types;
??? default_type? application/octet-stream;
??? sendfile??????? on;
??? keepalive_timeout? 65;
????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? logs/access.log? main;
??? server {
??????? listen?????? 80;
??????? server_name? localhost;
??????????????? rewrite ^/.* http://www.abc.com permanent;
??? }
??? include vhost/*.conf;
}
(2)插入access_log語句
vi conf/vhost/www.abc.com.conf
#vi編輯虛擬主機配置文件
文件內容:
server {
?access_log /data/log/www;
??????? listen 80;
??????? server_name abc.com www.abc.com;
??????? location / {
??????????????? root /data/www/www;
??????????????? index index.html index.htm;
??????? }
??????? error_log??? logs/error_www.abc.com.log??? error;
????????access_log??? logs/access_www.abc.com.log??? main;
??????? #新增內容↑
}
(3)重啟服務
確認無誤便可重啟,操作如下:
nginx -t
#結果顯示ok和success沒問題便可重啟
nginx -s reload
(4)查看訪問日志文件
ll logs/access_www.abc.com.log
-rw-r--r-- 1 root root 2305 Jun 13 18:25 logs/access_www.abc.com.log
#nginx進程,一般設置為和cpu核數一樣
worker_processes 4;?????????????????????? ?
#錯誤日志存放目錄?
error_log? /data1/logs/error.log? crit;??
#運行用戶,默認即是nginx,可不設置
user nginx????? ?
#進程pid存放位置
pid??????? /application/nginx/nginx.pid;?????? ?
#Specifies the value for maximum file descriptors that can be opened by this process.?
#最大文件打開數(連接),可設置為系統優化后的ulimit -HSn的結果
worker_rlimit_nofile 51200;
cpu親和力配置,讓不同的進程使用不同的cpu
worker_cpu_affinity 0001 0010 0100 1000 0001 00100100 1000;
#工作模式及連接數上限
events
{
use epoll;? ??? ?#epoll是多路復用IO(I/O Multiplexing)中的一種方式,但是僅用于linux2.6以上內核,可以大大提高nginx的性能
worker_connections 1024;? #;單個后臺worker process進程的最大并發鏈接數
}
###################################################
http
{
include mime.types; #文件擴展名與類型映射表
default_type application/octet-stream; #默認文件類型
#limit模塊,可防范一定量的DDOS攻擊
#用來存儲session會話的狀態,如下是為session分配一個名為one的10M的內存存儲區,限制了每秒只接受一個ip的一次請求 1r/s
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
include?????? mime.types;
default_type? application/octet-stream;
#第三方模塊lua防火墻
lua_need_request_body on;
#lua_shared_dict limit 50m;
lua_package_path "/application/nginx/conf/waf/?.lua";
init_by_lua_file "/application/nginx/conf/waf/init.lua";
access_by_lua_file "/application/nginx/conf/waf/access.lua";
#設定請求緩存?
server_names_hash_bucket_size 128;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
client_max_body_size 100m;
??#隱藏響應header和錯誤通知中的版本號
server_tokens off;
??#開啟高效傳輸模式? ?
sendfile on;
-------------------------------------------------------------------------------------------------
#激活tcp_nopush參數可以允許把httpresponse header和文件的開始放在一個文件里發布,
?積極的作用是減少網絡報文段的數量
tcp_nopush???? on;
#激活tcp_nodelay,內核會等待將更多的字節組成一個數據包,從而提高I/O性能
tcp_nodelay on;
#FastCGI相關參數:為了改善網站性能:減少資源占用,提高訪問速度
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
----------------------------------------------
#連接超時時間,單位是秒
keepalive_timeout 60;
? #開啟gzip壓縮功能
gzip on;
#設置允許壓縮的頁面最小字節數,頁面字節數從header頭的Content-Length中獲取。默認值是0,表示不管頁面多大都進行壓縮。建議設置成大于1K。如果小于1K可能會越壓越大。
gzip_min_length? 1k;
#壓縮緩沖區大小。表示申請4個單位為16K的內存作為壓縮結果流緩存,默認值是申請與原始數據大小相同的內存空間來存儲gzip壓縮結果。
gzip_buffers???? 4 16k;
#壓縮版本(默認1.1,前端為squid2.5時使用1.0)用于設置識別HTTP協議版本,默認是1.1,目前大部分瀏覽器已經支持GZIP解壓,使用默認即可。
gzip_http_version 1.0;
#壓縮比率。用來指定GZIP壓縮比,1壓縮比最小,處理速度最快;9壓縮比最大,傳輸速度快,但處理最慢,也比較消耗cpu資源。
gzip_comp_level 9;
#用來指定壓縮的類型,“text/html”類型總是會被壓縮
gzip_types?????? text/plain application/x-javascript text/css application/xml;
#vary header支持。該選項可以讓前端的緩存服務器緩存經過GZIP壓縮的頁面,例如用
Squid緩存經過Nginx壓縮的數據。
gzip_vary off;
#開啟ssi支持,默認是off
ssi on;
ssi_silent_errors on;
#設置日志模式
log_format? access? '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#反向代理負載均衡設定部分
#upstream表示負載服務器池,定義名字為backend_server的服務器池
upstream backend_server {
server?? 10.254.244.20:81 weight=1 max_fails=2 fail_timeout=30s;
server?? 10.254.242.40:81 weight=1 max_fails=2 fail_timeout=30s;
server?? 10.254.245.19:81 weight=1 max_fails=2 fail_timeout=30s;
server?? 10.254.243.39:81 weight=1 max_fails=2 fail_timeout=30s;
#設置由 fail_timeout 定義的時間段內連接該主機的失敗次數,以此來斷定 fail_timeout 定義的時間段內該主機是否可用。默認情況下這個數值設置為 1。零值的話禁用這個數量的嘗試。
設置在指定時間內連接到主機的失敗次數,超過該次數該主機被認為不可用。
#這里是在30s內嘗試2次失敗即認為主機不可用!
}
###################
#基于域名的虛擬主機
server
{
#監聽端口
listen?????? 80;
server_name? www.abc.com abc.com;
index index.html index.htm index.php;#首頁排序
root? /data0/abc;#站點根目錄,即網站程序存放目錄
error_page 500 502 404 /templates/kumi/phpcms/404.html;#錯誤頁面
#偽靜態 ? 將www.abc.com/list....html的文件轉發到index.php
#rewrite ^/list-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /index.php?m=content&c=index&a=lists&catid=$1&types=$2&country=$3&language=$4&age=$5&startDate=$6&typeLetter=$7&type=$8&page=$9 last;
#location 標簽,根目錄下的.svn目錄禁止訪問
location ~ /.svn/ {
deny all;
}
location ~ \.php$
{#符合php擴展名的請求調度到fcgi server??
fastcgi_pass? 127.0.0.1:9000;#拋給本機的9000端口
fastcgi_index index.php;??? #設定動態首頁
include fcgi.conf;
}
allow?? 219.237.222.30 ;#允許訪問的ip
allow?? 219.237.222.31 ;
allow?? 219.237.222.32 ;
allow?? 219.237.222.33 ;
allow?? 219.237.222.34 ;
allow?? 219.237.222.35 ;
allow?? 219.237.222.61 ;
allow?? 219.237.222.28 ;
deny??? all;#禁止其他ip訪問
}
location ~ ^/admin.php
{
location ~ \.php$
{
fastcgi_pass? 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
allow?? 219.237.222.30 ;
allow?? 219.237.222.31 ;
allow?? 219.237.222.32 ;
allow?? 219.237.222.33 ;
allow?? 219.237.222.34 ;
allow?? 219.237.222.35 ;
allow?? 219.237.222.61;
allow?? 219.237.222.28;
deny??? all;
}
#將符合js,css文件的等設定expries緩存參數,要求瀏覽器緩存。
location~ .*\.(js|css)?$ {
expires????? 30d;#客戶端緩存上述js,css數據30天
??? }
##add by 20140321#######nginx防sql注入##########
###start####
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 444;
}
if ($query_string? ~* ".*(insert|select|delete|update|count|\*|%|master|truncate|declare|\'|\;|and|or|\(|\)|exec).* ")
{
return 444;
}
if ($request_uri ~* "(cost\()|(concat\()") {
return 444;
}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
return 444;
}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
return 444;
}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
return 444;
}
set $block_file_injections 0;
if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") {
set $block_file_injections 1;
}
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
set $block_file_injections 1;
}
if ($block_file_injections = 1) {
return 448;
}
set $block_common_exploits 0;
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
set $block_common_exploits 1;
}
if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "proc/self/environ") {
set $block_common_exploits 1;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
set $block_common_exploits 1;
}
if ($query_string ~ "base64_(en|de)code\(.*\)") {
set $block_common_exploits 1;
}
if ($block_common_exploits = 1) {
return 444;
}
set $block_spam 0;
if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {
set $block_spam 1;
}
if ($block_spam = 1) {
return 444;
}
set $block_user_agents 0;
if ($http_user_agent ~ "Wget") {
set $block_user_agents 1;
}
# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
set $block_user_agents 1;
}
# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetRight") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetWeb!") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go!Zilla") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Download Demon") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "TurnitinBot") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GrabNet") {
set $block_user_agents 1;
}
if ($block_user_agents = 1) {
return 444;
}
###end####
location ~ ^/list {
#如果后端的服務器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一臺服務器,實現故障轉移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#對不同的HTTP狀態碼設置不同的緩存時間
proxy_cache_valid? 200 301 302 304 1d;
#proxy_cache_valid? any 1d;
#以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host? $host;
proxy_set_header X-Forwarded-For? $remote_addr;
proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
#proxy_ignore_headers Set-Cookie;
#proxy_hide_header Set-Cookie;
proxy_pass http://backend_server;
add_header????? Nginx-Cache???? "$upstream_cache_status? from? km";
expires????? 1d;
}
access_log? /data1/logs/abc.com.log access;?#nginx訪問日志
}
-----------------------ssl(https)相關------------------------------------
server {
listen 13820; #監聽端口
server_name localhost;
charset utf-8;?#gbk,utf-8,gb2312,gb18030 可以實現多種編碼識別
ssl on; #開啟ssl
ssl_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/server.crt; #服務的證書
ssl_certificate_key /ls/app/nginx/conf/mgmtxiangqiankeys/server.key; #服務端key
ssl_client_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/ca.crt; #客戶端證書
ssl_session_timeout 5m; #session超時時間
ssl_verify_client on; # 開戶客戶端證書驗證
ssl_protocols SSLv2 SSLv3 TLSv1;?#允許SSL協議?
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #加密算法
ssl_prefer_server_ciphers on; #啟動加密算法
access_log /lw/logs/nginx/dataadmin.test.com.ssl.access.log access ; #日志格式及日志存放路徑
error_log /lw/logs/nginx/dataadmin.test.com.ssl.error.log; #錯誤日志存放路徑
}
-------------------------------------------------------------------------
}
user nginx nginx ;??Nginx用戶及組:用戶 組。window下不指定
worker_processes 8;?工作進程:數目。根據硬件調整,通常等于CPU數量或者2倍于CPU。
error_log ?logs/error.log;
error_log ?logs/error.log ?notice;
error_log ?logs/error.log ?info;錯誤日志:存放路徑。
pid logs/nginx.pid;pid(進程標識符):存放路徑。
worker_rlimit_nofile 204800;?指定進程可以打開的最大描述符:數目。
這個指令是指當一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,但是nginx分配請求并不是那么均勻,所以最好與ulimit -n 的值保持一致。
現在在linux 2.6內核下開啟文件打開數為65535,worker_rlimit_nofile就相應應該填寫65535。
這是因為nginx調度時分配請求到進程并不是那么的均衡,所以假如填寫10240,總并發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。
events
{
use epoll;
使用epoll的I/O 模型。linux建議epoll,FreeBSD建議采用kqueue,window下不指定。
補充說明:
與apache相類,nginx針對不同的操作系統,有不同的事件模型
A)標準事件模型
Select、poll屬于標準事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或poll
B)高效事件模型
Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成內核崩潰。
Epoll:使用于Linux內核2.6版本及以后的系統。
/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
Eventport:使用于Solaris 10。 為了防止出現內核崩潰的問題, 有必要安裝安全補丁。
worker_connections 204800;
沒個工作進程的最大連接數量。根據硬件調整,和前面工作進程配合起來用,盡量大,但是別把cpu跑到100%就行。
每個進程允許的最多連接數,理論上每臺nginx服務器的最大連接數為worker_processes*worker_connections
keepalive_timeout 60; ? keepalive超時時間。
client_header_buffer_size 4k;
客戶端請求頭部的緩沖區大小。這個可以根據你的系統分頁大小來設置,一般一個請求頭的大小不會超過1k,不過由于一般系統分頁都要大于1k,所以這里設置為分頁大小。
分頁大小可以用命令getconf PAGESIZE 取得。
[root@web001 ~]#?getconf PAGESIZE
4096
但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設置為“系統分頁大小”的整倍數。
open_file_cache max=65535 inactive=60s;
這個將為打開文件指定緩存,默認是沒有啟用的,max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求后刪除緩存。
open_file_cache_valid 80s;
這個是指多長時間檢查一次緩存的有效信息。
open_file_cache_min_uses 1;
open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例,如果有一個文件在inactive時間內一次沒被使用,它將被移除。
}
##設定http服務器,利用它的反向代理功能提供負載均衡支持
http
{
include mime.types;
設定mime類型,類型由mime.type文件定義
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format?log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';
日志格式設置。
$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;
$remote_user:用來記錄客戶端用戶名稱;
$time_local: 用來記錄訪問時間與時區;
$request:?用來記錄請求的url與http協議;
$status: 用來記錄請求狀態;成功是200,
$body_bytes_sent :記錄發送給客戶端文件主體內容大小;
$http_referer:用來記錄從那個頁面鏈接訪問過來的;
$http_user_agent:記錄客戶瀏覽器的相關信息;
通常web服務器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。
反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。
access_log ?logs/host.access.log ?main;
access_log ?logs/host.access.404.log ?log404;
用了log_format指令設置了日志格式之后,需要用access_log指令指定日志文件的存放路徑;
server_names_hash_bucket_size 128;
#保存服務器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。
參數hash bucket size總是等于hash表的大小,并且是一路處理器緩存大小的倍數。在減少了在內存中的存取次數后,使在處理器中加速查找hash表鍵值成為可能。如果hash bucket size等于一路處理器緩存的大小,那么在查找鍵的時候,最壞的情況下在內存中查找的次數為2。第一次是確定存儲單元的地址,第二次是在存儲單元中查找鍵 值。因此,如果Nginx給出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一個參數的大小.
client_header_buffer_size 4k;
客戶端請求頭部的緩沖區大小。這個可以根據你的系統分頁大小來設置,一般一個請求的頭部大小不會超過1k,不過由于一般系統分頁都要大于1k,所以這里設置為分頁大小。分頁大小可以用命令getconf PAGESIZE取得。
large_client_header_buffers 8 128k;
客戶請求頭緩沖大小。nginx默認會用client_header_buffer_size這個buffer來讀取header值,如果
header過大,它會使用large_client_header_buffers來讀取。
open_file_cache max=102400 inactive=20s;
這個指令指定緩存是否啟用。
例: open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
open_file_cache_errors
語法:open_file_cache_errors on | off 默認值:open_file_cache_errors off 使用字段:http, server, location 這個指令指定是否在搜索一個文件是記錄cache錯誤.
open_file_cache_min_uses
語法:open_file_cache_min_uses number 默認值:open_file_cache_min_uses 1 使用字段:http, server, location 這個指令指定了在open_file_cache指令無效的參數中一定的時間范圍內可以使用的最小文件數,如果使用更大的值,文件描述符在cache中總是打開狀態.
open_file_cache_valid
語法:open_file_cache_valid time 默認值:open_file_cache_valid 60 使用字段:http, server, location 這個指令指定了何時需要檢查open_file_cache中緩存項目的有效信息.
client_max_body_size 300m;?設定通過nginx上傳文件的大小
sendfile on;
sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對于普通應用,必須設為on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡IO處理速度,降低系統uptime。
tcp_nopush on; 此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
proxy_connect_timeout 90;
后端服務器連接的超時時間_發起握手等候響應超時時間
proxy_read_timeout 180;
連接成功后_等候后端服務器響應時間_其實已經進入后端的排隊之中等候處理(也可以說是后端服務器處理請求的時間)
proxy_send_timeout 180;
后端服務器數據回傳時間_就是在規定時間之內后端服務器必須傳完所有的數據
proxy_buffer_size 256k;
設置從被代理服務器讀取的第一部分應答的緩沖區大小,通常情況下這部分應答中包含一個小的應答頭,默認情況下這個值的大小為指令proxy_buffers中指定的一個緩沖區的大小,不過可以將其設置為更小
proxy_buffers 4 256k;
設置用于讀取應答(來自被代理服務器)的緩沖區數目和大小,默認情況也為分頁大小,根據操作系統的不同可能是4k或者8k
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
設置在寫入proxy_temp_path時數據的大小,預防一個工作進程在傳遞文件時阻塞太長
proxy_temp_path /data0/proxy_temp_dir;
proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#設置內存緩存空間大小為200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小為30GB。
keepalive_timeout 120;
keepalive超時時間。
tcp_nodelay on;
client_body_buffer_size 512k;
如果把它設置為比較大的數值,例如256k,那么,無論使用firefox還是IE瀏覽器,來提交任意小于256k的圖片,都很正常。如果注釋該指令,使用默認的client_body_buffer_size設置,也就是操作系統頁面大小的兩倍,8k或者16k,問題就出現了。
無論使用firefox4.0還是IE8.0,提交一個比較大,200k左右的圖片,都返回500 Internal Server Error錯誤
proxy_intercept_errors on;
表示使nginx阻止HTTP應答代碼為400或者更高的應答。
upstream bakend {
server 127.0.0.1:8027;
server 127.0.0.1:8028;
server 127.0.0.1:8029;
hash $request_uri;
}
nginx的upstream目前支持4種方式的分配
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
2、weight
指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream backend {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
tips:
upstream bakend{#定義負載均衡設備的Ip及設備狀態}
{
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用負載均衡的server中增加
proxy_pass http://bakend/;
每個設備的狀態設置為:
1.down表示單前的server暫時不參與負載
2.weight為weight越大,負載的權重就越大。
3.max_fails:允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
client_body_in_file_only設置為On 可以講client post過來的數據記錄到文件中用來做debug
client_body_temp_path設置記錄文件的目錄 可以設置最多3層目錄
location對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
##配置虛擬機
server
{
listen 80;
配置監聽端口
server_name image.***.com;
配置訪問域名
location ~* \.(mp3|exe)$ {
對以“mp3或exe”結尾的地址進行負載均衡
proxy_pass http://img_relay$request_uri;
設置被代理服務器的端口或套接字,以及URL
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
以上三行,目的是將代理服務器收到的用戶的信息傳到真實服務器上
}
location /face {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
error_page 404 502 = @fetch;
}
location @fetch {
access_log /data/logs/face.log log404;
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
location /image {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
error_page 404 502 = @fetch;
}
location @fetch {
access_log /data/logs/image.log log404;
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
}
##其他舉例
server
{
listen 80;
server_name *.***.com *.***.cn;
location ~* \.(mp3|exe)$ {
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#error_page 404 http://i1.***img.com/help/noimg.gif;
error_page 404 502 = @fetch;
}
location @fetch {
access_log /data/logs/baijiaqi.log log404;
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
}
server
{
listen 80;
server_name *.***img.com;
location ~* \.(mp3|exe)$ {
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#error_page 404 http://i1.***img.com/help/noimg.gif;
error_page 404 = @fetch;
}
#access_log off;
location @fetch {
access_log /data/logs/baijiaqi.log log404;
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
}
server
{
listen 8080;
server_name ngx-ha.***img.com;
location / {
stub_status on;
access_log off;
}
}
server {
listen 80;
server_name imgsrc1.***.net;
root html;
}
server {
listen 80;
server_name ***.com w.***.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.***.com/ ;
}
}
server {
listen 80;
server_name *******.com w.*******.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.*******.com/;
}
}
server {
listen 80;
server_name ******.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.******.com/;
}
}
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#設定查看Nginx狀態的地址
location ~ /\.ht {
deny all;
}
#禁止訪問.htxxx文件
}
注釋:變量
Ngx_http_core_module模塊支持內置變量,他們的名字和apache的內置變量是一致的。
首先是說明客戶請求title中的行,例如$http_user_agent,$http_cookie等等。
此外還有其它的一些變量
$args此變量與請求行中的參數相等
$content_length等于請求行的“Content_Length”的值。
$content_type等同與請求頭部的”Content_Type”的值
$document_root等同于當前請求的root指令指定的值
$document_uri與$uri一樣
$host與請求頭部中“Host”行指定的值或是request到達的server的名字(沒有Host行)一樣
$limit_rate允許限制的連接速率
$request_method等同于request的method,通常是“GET”或“POST”
$remote_addr客戶端ip
$remote_port客戶端port
$remote_user等同于用戶名,由ngx_http_auth_basic_module認證
$request_filename當前請求的文件的路徑名,由root或alias和URI request組合而成
$request_body_file
$request_uri含有參數的完整的初始URI
$query_string與$args一樣
$sheeme http模式(http,https)盡在要求是評估例如
Rewrite ^(.+)$ $sheme://example.com$; Redirect;
$server_protocol等同于request的協議,使用“HTTP/或“HTTP/
$server_addr request到達的server的ip,一般獲得此變量的值的目的是進行系統調用。為了避免系統調用,有必要在listen指令中指明ip,并使用bind參數。
$server_name請求到達的服務器名
$server_port請求到達的服務器的端口號
$uri等同于當前request中的URI,可不同于初始值,例如內部重定向時或使用index