ÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄÈÕÖ¾¼Í¼ºÍÆÊÎö
ÔõÑùʹÓÃnginx¾ÙÐÐhttpÇëÇóµÄÈÕÖ¾¼Í¼ºÍÆÊÎö
СÐò£º
ÔÚÒ»Ñùƽ³£µÄWeb¿ª·¢ÖУ¬ÎÒÃǾ³£ÐèÒª¶ÔHTTPÇëÇó¾ÙÐÐÈÕÖ¾¼Í¼ºÍÆÊÎö£¬ÒÔ±ã¼à²âÍøÕ¾µÄ»á¼ûÇéÐΡ¢ÅÅÅÌÎÊÌâÒÔ¼°ÓÅ»¯ÐÔÄÜ¡£Nginx×÷Ϊһ¿î¹¦Ð§Ç¿Ê¢µÄHTTPЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÌṩÁ˸»ºñµÄÈÕÖ¾¼Í¼¹¦Ð§£¬²¢ÇÒÏà¹ØÓÚÆäËûЧÀÍÆ÷£¬ËüµÄÐÔÄÜÔ½·¢¾«²Ê¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖúÍʹÓÃNginxÀ´¾ÙÐÐHTTPÇëÇóµÄÈÕÖ¾¼Í¼ºÍÆÊÎö¡£
Ò»¡¢ÉèÖÃNginxÈÕÖ¾¼Í¼ÃûÌÃ
ΪÁ˼ͼ¸üÏêϸµÄÐÅÏ¢£¬ÎÒÃÇ¿ÉÒÔ×Ô½ç˵NginxµÄÈÕÖ¾¼Í¼ÃûÌá£ÔÚNginxµÄÉèÖÃÎļþÖУ¬ÕÒµ½”http”ÉÏÏÂÎÄ£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
http { log_format my_log_format '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" "$http_user_agent" ' '"$gzip_ratio"'; access_log /var/log/nginx/access.log my_log_format; }
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃǽç˵ÁËÒ»¸öÃûΪ”my_log_format”µÄÈÕÖ¾¼Í¼ÃûÌ㬰üÀ¨ÁËÔ¶³ÌµØµã¡¢Ô¶³ÌÓû§¡¢»á¼ûʱ¼ä¡¢ÇëÇóÄÚÈÝ¡¢×´Ì¬Âë¡¢·¢Ë͵Ä×Ö½ÚÊý¡¢ÈªÔ´URL¡¢User-Agent¡¢Gzip±ÈÂʵÈÐÅÏ¢¡£È»ºó½«¸ÃÃûÌÃÓ¦Óõ½NginxµÄ»á¼ûÈÕÖ¾ÎļþÖС£
¶þ¡¢ÉèÖÃNginx¾ÙÐÐÈÕÖ¾Ö§½â
Ëæ×ÅÍøÕ¾»á¼ûÁ¿µÄÔöÌí£¬ÈÕÖ¾ÎļþÍùÍù»á±äµÃºÜÊÇÖØ´ó¡£ÎªÁËÀû±ãÖÎÀíºÍÆÊÎöÈÕÖ¾£¬ÎÒÃÇ¿ÉÒÔÉèÖÃNginx¾ÙÐÐÈÕÖ¾Ö§½â¡£ÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
http { access_log /var/log/nginx/access.log my_log_format; error_log /var/log/nginx/error.log; logrotate daily; rotate 7; size 10M; missingok; notifempty; compress; delaycompress; }
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃÇÉèÖÃNginxÌìÌì¾ÙÐÐÈÕÖ¾Ö§½â£¬±£´æ×î½ü7ÌìµÄÈÕÖ¾Îļþ¡£Ã¿¸öÎļþ×î´óΪ10MB£¬µÖ´ïÉÏÏ޺󽫾ÙÐÐѹËõ²¢ÐÂÌìÉúÒ»¸öÈÕÖ¾Îļþ¡£ÁíÍ⣬ÈôÊÇÎļþ²»±£´æÒ²»á¼ÌÐø¾ÙÐÐÈÕÖ¾¼Í¼£¬²¢ÇÒ¿ÕÎļþ²»»á´¥·¢ÈÕÖ¾Ö§½â¡£
Èý¡¢Ê¹ÓÃELK Stack¾ÙÐÐÈÕÖ¾ÆÊÎö
ELK StackÊÇÒ»Ì׿ªÔ´µÄÈÕÖ¾ÆÊÎö½â¾ö¼Æ»®£¬ÓÉElasticsearch¡¢LogstashºÍKibanaÈý¸ö¹¤¾ß×é³É¡£ÎÒÃÇ¿ÉÒÔʹÓÃELK StackÀ´¶ÔNginxµÄÈÕÖ¾¾ÙÐÐÆÊÎöºÍ¿ÉÊÓ»¯¡£ÏÂÃæÊÇÉèÖð취£º
1.×°ÖÃElasticsearch
Ê×ÏÈ£¬ÐèҪװÖúÍÉèÖÃElasticsearch×÷ΪÈÕÖ¾´æ´¢ºÍË÷ÒýµÄÊý¾Ý¿â¡£ÏêϸװÖð취Çë²Î¿¼Elasticsearch¹Ù·½Îĵµ£ºhttps://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
2.×°ÖÃLogstash
Æä´Î£¬×°Öò¢ÉèÖÃLogstash×÷ΪÊý¾ÝÍøÂçºÍ¼Ó¹¤µÄ¹¤¾ß¡£ÏêϸװÖð취Çë²Î¿¼Logstash¹Ù·½Îĵµ£ºhttps://www.elastic.co/guide/en/logstash/current/index.html
3.±àдLogstashÉèÖÃÎļþ
½¨ÉèÒ»¸öеÄÉèÖÃÎļþ£¨ÀýÈçnginx.conf£©£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
input { file { path => "/var/log/nginx/access.log" start_position => "beginning" } } filter { grok { match => { "message" => "%{IPORHOST:clientip} %{HTTPDUSER:ident} %{USER:auth} [%{HTTPDATE:timestamp}] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} %{NUMBER:bytes} "%{URI:referrer}" "%{DATA:useragent}" "%{DATA:gzipratio}"" } } } output { elasticsearch { hosts => "localhost" index => "nginx-logs-%{+YYYY.MM.dd}" } }
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃÇÖ¸¶¨ÁËÈÕÖ¾ÎļþµÄ·¾¶¡¢ÈÕÖ¾ÃûÌúÍElasticsearchµÄµØµã£¬²¢½«ÈÕÖ¾´æ´¢µ½ÒÔÈÕÆÚΪºó׺µÄË÷ÒýÖС£
4.Æô¶¯Logstash
Æô¶¯Logstash²¢¼ÓÔØÉèÖÃÎļþ£º
$ bin/logstash -f nginx.conf
µÇ¼ºó¸´ÖÆ
5.×°ÖÃKibana
×îºó£¬×°ÖúÍÉèÖÃKibana×÷ΪÈÕÖ¾¿ÉÊÓ»¯µÄ¹¤¾ß¡£ÏêϸװÖð취Çë²Î¿¼Kibana¹Ù·½Îĵµ£ºhttps://www.elastic.co/guide/en/kibana/current/index.html
ÉèÖÃÍê³Éºó£¬·¿ªKibanaµÄweb½çÃ棬ͨ¹ýËÑË÷ºÍ¹ýÂ˹¦Ð§£¬¿ÉÒÔʵʱÉó²éºÍÆÊÎöNginxµÄ»á¼ûÈÕÖ¾¡£
½áÂÛ£º
ͨ¹ýÉèÖÃNginx¾ÙÐÐHTTPÇëÇóµÄÈÕÖ¾¼Í¼ºÍÆÊÎö£¬ÎÒÃÇ¿ÉÒÔ¼à¿ØÍøÕ¾µÄ»á¼ûÇéÐΡ¢ÅÅÅÌÎÊÌâÒÔ¼°ÓÅ»¯ÐÔÄÜ¡£Í¬Ê±£¬ÍŽáELK Stack¿ÉÒÔʵÏÖÈÕÖ¾µÄ¼¯Öд洢¡¢ÆÊÎöºÍ¿ÉÊÓ»¯£¬Ìá¸ßÔËάЧÂʺÍÍøÕ¾ÐÔÄÜÆÊÎöµÄ׼ȷÐÔ¡£Ï£Íû±¾ÎĶԸ÷ÈËÄÜÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐHTTPÇëÇóµÄÈÕÖ¾¼Í¼ºÍÆÊÎöµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡