Nginx»á¼ûÈÕÖ¾ÉèÖÃÖ¸ÄÏ£¬¼à¿ØÍøÕ¾»á¼ûÐÐΪ
nginx»á¼ûÈÕÖ¾ÉèÖÃÖ¸ÄÏ£¬¼à¿ØÍøÕ¾»á¼ûÐÐΪ
NginxÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬ÆÕ±éÓÃÓڴ¾²Ì¬»ò¶¯Ì¬ÍøÕ¾¡£ÔڴÍøÕ¾µÄÀú³ÌÖУ¬ÎÒÃǾ³£ÐèÒª¶ÔÍøÕ¾µÄ»á¼ûÐÐΪ¾ÙÐмà¿ØºÍÆÊÎö¡£NginxÌṩÁËÇ¿Ê¢µÄ»á¼ûÈÕÖ¾ÉèÖù¦Ð§£¬Äܹ»×ÊÖúÎÒÃÇʵÏÖ¶ÔÍøÕ¾µÄ»á¼ûÐÐΪµÄ¼à¿ØºÍÆÊÎö¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginxµÄ»á¼ûÈÕÖ¾£¬ÒÔ¼°ÔõÑùʹÓÃÈÕÖ¾ÆÊÎö¹¤¾ß¾ÙÐÐÍøÕ¾»á¼ûÐÐΪµÄ¼à¿Ø¡£
ÔõÑùÉèÖÃNginxµÄ»á¼ûÈÕÖ¾£¿
NginxµÄ»á¼ûÈÕÖ¾ÉèÖúÜÊǼòÆÓ£¬Ö»ÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒ»ÐÐÈÕÖ¾ÉèÖü´¿É¡£Í¨³£ÇéÐÎÏ£¬NginxµÄÉèÖÃÎļþλÓÚ/etc/nginx/nginx.conf¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĻá¼ûÈÕÖ¾ÉèÖõÄʾÀý£º
http { ... log_format access '$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 access; ... }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖýç˵ÁËÒ»¸öÃûΪaccessµÄÈÕÖ¾ÃûÌ㬲¢½«accessÈÕÖ¾Êä³öµ½/var/log/nginx/access.logÎļþ¡£Í¨¹ýÕâ¸öÈÕÖ¾ÉèÖã¬ÎÒÃÇ¿ÉÒÔ»ñÈ¡µ½¿Í»§¶ËµÄIPµØµã¡¢»á¼ûʱ¼ä¡¢ÇëÇóµÄURL¡¢HTTP״̬Âë¡¢·¢Ë͵ÄÊý¾ÝÁ¿¡¢ÈªÔ´Ò³ÃæURL¡¢¿Í»§¶ËµÄUser-AgentÐÅÏ¢ºÍÊðÀíЧÀÍÆ÷µÄIPµØµã¡£
ÔõÑùʹÓÃÈÕÖ¾ÆÊÎö¹¤¾ß¾ÙÐÐÍøÕ¾»á¼ûÐÐΪµÄ¼à¿Ø£¿
ÔÚÉèÖÃÁË»á¼ûÈÕÖ¾Ö®ºó£¬ÎÒÃǾͿÉÒÔʹÓÃÈÕÖ¾ÆÊÎö¹¤¾ß¶ÔÍøÕ¾µÄ»á¼ûÐÐΪ¾ÙÐмà¿ØºÍÆÊÎö¡£ÏÂÃæÏÈÈÝÁ½ÖÖ³£ÓõÄÈÕÖ¾ÆÊÎö¹¤¾ß£ºAWStatsºÍELK Stack¡£
AWStatsÊÇÒ»¸öÃâ·ÑµÄÈÕÖ¾ÆÊÎö¹¤¾ß£¬¿ÉÒÔÌìÉúÏêϸµÄͳ¼Æ±¨¸æ¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃAWStats²¢¶ÔÆä¾ÙÐÐÉèÖá£ÔÚUbuntuϵͳÉÏ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî×°ÖÃAWStats£º
sudo apt-get install awstats
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÐèÒª¶ÔAWStats¾ÙÐÐÉèÖá£ÔÚAWStatsÉèÖÃÎļþÖУ¬ÐèÒªÖ¸¶¨Nginx»á¼ûÈÕÖ¾µÄλÖúÍÎļþÃûÌá£È·±£ÉèÖÃÎļþÖÐ׼ȷÉèÖÃÁËLogFormat¡¢LogFileºÍSiteDomain²ÎÊý¡£ÉúÑÄÉèÖÃÎļþ²¢Í˳ö¡£
È»ºó£¬ÎÒÃÇÐèҪΪÿ¸öÍøÕ¾½¨ÉèÒ»¸öÉèÖÃÎļþ¡£ÔÚAWStatsµÄÉèÖÃÎļþĿ¼£¨Ä¬ÒÔΪ/etc/awstats£©ÖУ¬¸´ÖÆawstats.model.confÎļþ£¬²¢½«ÆäÖØÃüÃûΪÍøÕ¾ÓòÃûµÄÉèÖÃÎļþ£¨ºÃ±Èawstats.example.com.conf£©¡£ÔÚÕâ¸öÎļþÖУ¬ÉèÖÃLogFile²ÎÊýΪNginx»á¼ûÈÕÖ¾µÄλÖá£
×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÏÂÃæµÄÏÂÁîÌìÉúͳ¼Æ±¨¸æ£º
sudo /usr/lib/cgi-bin/awstats.pl -config=example.com -update
µÇ¼ºó¸´ÖÆ
ÆäÖÐexample.comÐèÒªÌ滻ΪÏÖʵµÄÍøÕ¾ÓòÃû¡£
ELK StackÊÇÒ»Ì×ÓÃÓÚÈÕÖ¾ÆÊÎöºÍËÑË÷µÄ¹¤¾ß×éºÏ£¬°üÀ¨Elasticsearch¡¢LogstashºÍKibana¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖúÍÉèÖÃElasticsearchºÍKibana£¬¿ÉÒԲο¼¹Ù·½Îĵµ¾ÙÐÐ×°ÖúÍÉèÖá£È»ºó£¬ÎÒÃÇÐèÒªÉèÖÃLogstashÀ´´¦Öóͷ£Nginx»á¼ûÈÕÖ¾£¬²¢½«Æä·¢Ë͵½Elasticsearch¡£
ÔÚLogstashµÄÉèÖÃÎļþÖУ¬ÎÒÃÇÐèÒª½ç˵ÊäÈëºÍÊä³ö¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄLogstashÉèÖÃÎļþʾÀý£º
input { file { path => "/var/log/nginx/access.log" start_position => "beginning" sincedb_path => "/dev/null" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}" } } } output { elasticsearch { hosts => ["localhost:9200"] index => "nginx-access-%{+YYYY.MM.dd}" } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖý«Nginx»á¼ûÈÕÖ¾×÷ΪÊäÈ룬ʹÓÃGrokģʽƥÅäÈÕÖ¾ÃûÌã¬È»ºó½«´¦Öóͷ£ºóµÄÈÕÖ¾·¢Ë͵½ElasticsearchÖС£
ÖØÆôLogstashÒÔʹÉèÖÃÉúЧ£¬²¢Æô¶¯ElasticsearchºÍKibana¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔÔÚKibanaÖн¨ÉèÒDZí°åÀ´Õ¹Ê¾ÍøÕ¾µÄ»á¼ûÐÐΪͳ¼ÆÐÅÏ¢¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùÉèÖÃNginxµÄ»á¼ûÈÕÖ¾ÒÔ¼°ÔõÑùʹÓÃAWStatsºÍELK StackµÈÈÕÖ¾ÆÊÎö¹¤¾ß¾ÙÐÐÍøÕ¾»á¼ûÐÐΪµÄ¼à¿Ø¡£Í¨¹ý¶ÔÍøÕ¾»á¼ûÈÕÖ¾µÄÆÊÎö£¬ÎÒÃÇ¿ÉÒÔÏàʶÍøÕ¾µÄ»á¼ûÇéÐκÍÓû§ÐÐΪ£¬ÎªÓÅ»¯ÍøÕ¾ÐÔÄܺÍË¢ÐÂÓû§ÌåÑéÌṩÖ÷Òª²Î¿¼¡£Ï£Íû±¾ÎĶÔÄúÔÚ¼à¿ØÍøÕ¾»á¼ûÐÐΪ·½ÃæµÄÊÂÇéÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginx»á¼ûÈÕÖ¾ÉèÖÃÖ¸ÄÏ£¬¼à¿ØÍøÕ¾»á¼ûÐÐΪµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡