{"id":118749,"date":"2018-09-07T08:07:46","date_gmt":"2018-09-07T00:07:46","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=118749"},"modified":"2018-08-31T09:08:56","modified_gmt":"2018-08-31T01:08:56","slug":"nginx-log-jianjie","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-log-jianjie.html","title":{"rendered":"nginx access_log\u65e5\u5fd7\u7b80\u4ecb"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nnginx \u65e5\u5fd7\u4e3b\u8981\u6709\u4e24\u6761\u6307\u4ee4\uff1a1\uff09log_format\uff1a\u7528\u6765\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f\uff1b2\uff09access_log\uff1a\u7528\u6765\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u5b58\u653e\u8def\u5f84\u3001\u683c\u5f0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
log_format \u65e5\u5fd7\u683c\u5f0f<\/div>\n
1\u3001\u8bed\u6cd5\uff1a<\/strong><\/span><\/div>\n

log_format name\uff08\u683c\u5f0f\u540d\u5b57\uff09 \u683c\u5f0f\u6837\u5f0f\uff08\u5373\u60f3\u8981\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u65e5\u5fd7\u5185\u5bb9\uff09
\n\u793a\u4f8b\uff1a<\/p>\n

log_format main\r\n'$remote_addr - $remote_user [$time_local] \"$request\" '\r\n'$status $body_bytes_s ent \"$http_referer\" '\r\n'\"$http_user_agent\" \"$http_x_forwarded_for\"'\r\n<\/pre>\n
2\u3001\u5177\u4f53\u53c2\u6570\u683c\u5f0f<\/strong><\/span><\/div>\n

\"\"<\/p>\n

3\u3001x_forwarded_for\uff1a<\/strong><\/span><\/div>\n

\u901a\u5e38web\u670d\u52a1\u5668\u653e\u5728\u53cd\u5411\u4ee3\u7406\u7684\u540e\u9762\uff0c\u8fd9\u6837\u5c31\u4e0d\u80fd\u83b7\u53d6\u5230\u5ba2\u6237\u7684IP\u5730\u5740\u4e86\uff0c\u901a\u8fc7$remote_addr\u62ff\u5230\u7684IP\u5730\u5740\u662f\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u7684iP\u5730\u5740\u3002\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u5728\u8f6c\u53d1\u8bf7\u6c42\u7684http\u5934\u4fe1\u606f\u4e2d\uff0c\u53ef\u4ee5\u589e\u52a0x_forwarded_for\u4fe1\u606f\uff0c\u7528\u4ee5\u8bb0\u5f55\u539f\u6709\u5ba2\u6237\u7aef\u7684IP\u5730\u5740\u548c\u539f\u6765\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u7684\u670d\u52a1\u5668\u5730\u5740\u3002<\/p>\n

\u6ce8<\/em>\uff1a\u5728server\u4e2d\u8bbe\u7f6ex_forwarded_for<\/p>\n

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/pre>\n
access_log<\/span><\/div>\n

\u7528\u4e86log_format \u6307\u4ee4\u8bbe\u7f6e\u4e86\u65e5\u5fd7\u683c\u5f0f\u4e4b\u540e\uff0c\u9700\u8981\u7528access_log\u6307\u4ee4\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u7684\u5b58\u653e\u8def\u5f84\uff1b<\/p>\n

1\u3001\u8bed\u6cd5\uff1a<\/strong><\/span><\/div>\n

access_log path(\u5b58\u653e\u8def\u5f84) format (\u81ea\u5b9a\u4e49\u65e5\u5fd7\u540d\u79f0)
\n\u793a\u4f8b:<\/p>\n

access_log logs\/access.log main;<\/pre>\n
2\u3001\u8bbe\u7f6e\u5237\u76d8\u7b56\u7565\uff1a<\/strong><\/span><\/div>\n
access_log \/data\/logs\/nginx-access.log buffer=32k flush=5s;<\/pre>\n

buffer \u6ee1 32k \u624d\u5237\u76d8\uff1b\u5047\u5982 buffer \u4e0d\u6ee1 5s \u949f\u5f3a\u5236\u5237\u76d8\u3002<\/p>\n

\u6ce8<\/em>\uff1a\u4e00\u822clog_format\u5728\u5168\u5c40\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a\u3002access_log \u53ef\u4ee5\u5728\u5168\u5c40\u8bbe\u7f6e\uff0c\u4f46\u5f80\u5f80\u662f\u5b9a\u4e49\u5728\u865a\u62df\u4e3b\u673a\uff08server\uff09\u4e2d\u7684location\u4e2d\u3002
\n\u4f8b\u5982\uff1a<\/p>\n

http {\r\nlog_format main '$remote_addr - $remote_user [$time_local] \"$request\" '\r\n'\"$status\" $body_bytes_sent \"$http_referer\" '\r\n'\"$http_user_agent\" \"$http_x_forwarded_for\" '\r\n'\"$gzip_ratio\" $request_time $bytes_sent $request_length';\r\nlog_format srcache_log '$remote_addr - $remote_user [$time_local] \"$request\" '\r\n'\"$status\" $body_bytes_sent $request_time $bytes_sent $request_length '\r\n'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';\r\nopen_log_file_cache max=1000 inactive=60s;\r\nserver {\r\nserver_name ~^(www\\.)?(.+)$;\r\naccess_log logs\/$2-access.log main;\r\nerror_log logs\/$2-error.log;\r\nlocation \/srcache {\r\naccess_log logs\/access-srcache.log srcache_log;\r\n}\r\n}\r\n}<\/pre>\n
3\u3001\u5176\u4ed6\uff1a<\/strong><\/span><\/div>\n

1\uff09error_log\uff1a<\/p>\n

\u914d\u7f6e\u9519\u8bef\u65e5\u5fd7\uff0c\u4f8b\u5982\u4e0a\u4f8b\u3002<\/p>\n

2\uff09open_log_file_cache\uff1a<\/p>\n

\u5bf9\u4e8e\u6bcf\u4e00\u6761\u65e5\u5fd7\u8bb0\u5f55\uff0c\u90fd\u5c06\u662f\u5148\u6253\u5f00\u6587\u4ef6\uff0c\u518d\u5199\u5165\u65e5\u5fd7\uff0c\u7136\u540e\u5173\u95ed\u3002\u53ef\u4ee5\u4f7f\u7528open_log_file_cache\u6765\u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7f13\u5b58(\u9ed8\u8ba4\u662foff)\u3002
\n\u8bed\u6cd5:<\/p>\n

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];<\/pre>\n

\u53c2\u6570\u6ce8\u91ca\u5982\u4e0b\uff1a<\/p>\n