ÉîÈëÃ÷È·NginxÈÕÖ¾ÆÊÎö¹¤¾ßºÍ¹ýʧ´¦Öóͷ£¼¼ÇÉ
ÉîÈëÃ÷È·NginxÈÕÖ¾ÆÊÎö¹¤¾ßºÍ¹ýʧ´¦Öóͷ£¼¼ÇÉ
Nginx ÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆÕ±éÓ¦ÓÃÓÚ»¥ÁªÍøÁìÓò¡£ÔÚÔËάºÍ¿ª·¢Àú³ÌÖУ¬ÎÒÃǾ³£ÐèÒª¶ÔNginxµÄÈÕÖ¾¾ÙÐÐÆÊÎöÒÔÏàʶЧÀÍÆ÷µÄÔËÐÐ״̬ºÍÐÔÄÜÌåÏÖ¡£±¾ÎĽ«ÉîÈë̽ÌÖNginxÈÕÖ¾ÆÊÎö¹¤¾ß¼°³£ÓõĹýʧ´¦Öóͷ£¼¼ÇÉ£¬²¢ÌṩÏà¹ØµÄ´úÂëʾÀý¡£
Ò»¡¢NginxÈÕÖ¾ÆÊÎö¹¤¾ß
1.1. NginxµÄaccessÈÕÖ¾
NginxµÄaccessÈÕÖ¾¼Í¼ÁËÿ¸öÇëÇóµÄÏêϸÐÅÏ¢£¬°üÀ¨»á¼ûµÄʱ¼ä¡¢¿Í»§¶ËIPµØµã¡¢ÇëÇóµÄURL·¾¶¡¢HTTP״̬ÂëµÈ¡£ÎÒÃÇ¿ÉÒÔ̫ͨ¹ýÎöaccessÈÕÖ¾£¬ÏàʶÓû§µÄ»á¼ûÐÐΪºÍ»á¼ûµÄÐÔÄÜ״̬¡£³£ÓõÄNginxÈÕÖ¾ÆÊÎö¹¤¾ßÓÐGoAccess¡¢AwstatsºÍELKµÈ¡£
1.2. GoAccess
GoAccessÊÇÒ»¿î»ùÓÚÏÂÁîÐеÄʵʱWebÈÕÖ¾ÆÊÎö¹¤¾ß£¬¿ÉÒÔÒÔ¿ÉÊÓ»¯µÄ·½·¨Õ¹Ê¾NginxµÄÈÕÖ¾ÐÅÏ¢¡£Ëü¿ÉÒÔÌìÉúHTMLºÍJSONÃûÌõı¨¸æ£¬Ö§³Ö¶àÖÖͳ¼Æ·½·¨ºÍ¹ýÂËÌõ¼þ¡£ÏÂÃæÊÇÒ»¸öʹÓÃGoAccessÆÊÎöNginx»á¼ûÈÕÖ¾µÄʾÀý£º
$ goaccess -f /path/to/nginx/access.log -a
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁî»áÆÊÎö²¢ÊµÊ±ÏÔʾNginxµÄ»á¼ûÈÕÖ¾¡£Í¨¹ýÔÚä¯ÀÀÆ÷Öлá¼ûhttp://localhost:7890£¬¿ÉÒÔÉó²éʵʱµÄ»á¼û±¨¸æ¡£
1.3. Awstats
AwstatsÊÇÒ»¿î¹¦Ð§Ç¿Ê¢µÄÈÕÖ¾ÆÊÎö¹¤¾ß£¬¿ÉÒÔÌìÉúÏêϸµÄ»á¼û±¨¸æºÍͼ±í¡£ËüÖ§³Ö¶àÖÖÈÕÖ¾ÃûÌ㬰üÀ¨NginxµÄaccessÈÕÖ¾ÃûÌá£ÏÂÃæÊÇÒ»¸öʹÓÃAwstatsÆÊÎöNginx»á¼ûÈÕÖ¾µÄʾÀý£º
$ awstats.pl -config=nginx -LogFile=/path/to/nginx/access.log
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁî»áÌìÉúÒ»·ÝÏêϸµÄHTML±¨¸æ£¬Õ¹Ê¾NginxµÄ»á¼ûͳ¼ÆÐÅÏ¢¡£
1.4. ELK
ELKÊÇÖ¸Elasticsearch¡¢LogstashºÍKibanaÈý¿î¿ªÔ´¹¤¾ßµÄ×éºÏ£¬ÓÃÓÚʵʱÆÊÎöºÍ¿ÉÊÓ»¯ÈÕÖ¾Êý¾Ý¡£ElasticsearchÊÇÒ»¿îÂþÑÜʽËÑË÷ºÍÆÊÎöÒýÇ棬LogstashÊÇÒ»¿îÓÃÓÚÍøÂç¡¢´¦Öóͷ£ºÍ´«ÊäÈÕÖ¾Êý¾ÝµÄ¹¤¾ß£¬KibanaÊÇÒ»¿îÓÃÓÚչʾºÍ¿ÉÊÓ»¯ÈÕÖ¾Êý¾ÝµÄ¹¤¾ß¡£
ͨ¹ý½«NginxÈÕÖ¾µ¼Èëµ½ELKÖУ¬ÎÒÃÇ¿ÉÒÔ½èÖúKibanaÌìÉú¸»ºñµÄÒDZíÅ̺Íͼ±í£¬¿ìËÙ·¢Ã÷Ï¢Õù¾öÎÊÌâ¡£ÏÂÃæÊÇÒ»¸öʹÓÃELKÆÊÎöNginx»á¼ûÈÕÖ¾µÄʾÀý£º
Ê×ÏÈ£¬Í¨¹ýLogstash½«NginxÈÕÖ¾µ¼Èëµ½ElasticsearchÖУ¬ÉèÖÃÎļþÈçÏ£º
input { file { path => "/path/to/nginx/access.log" sincedb_path => "/dev/null" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}" } } } output { elasticsearch { hosts => ["localhost:9200"] index => "nginx-access-%{+YYYY.MM.dd}" } }
µÇ¼ºó¸´ÖÆ
È»ºó£¬Í¨¹ýKibana½¨Éè¿ÉÊÓ»¯ÒDZíÅÌ£¬Æ¾Ö¤ÐèҪѡÔñ²î±ðµÄͼ±íÀàÐͺÍÖ¸±ê¡£
¶þ¡¢Nginx¹ýʧ´¦Öóͷ£¼¼ÇÉ
2.1. ¹ýʧÈÕÖ¾
NginxµÄ¹ýʧÈÕÖ¾¼Í¼ÁËЧÀÍÆ÷µÄÔËÐÐ״̬ºÍ¹ýʧÐÅÏ¢£¬¹ØÓÚ¹ÊÕÏÅŲéºÍÐÔÄÜÓÅ»¯¶¼ºÜÊÇÓÐ×ÊÖú¡£¹ýʧÈÕÖ¾µÄλÖúÍÃûÌÿÉÒÔÔÚNginxµÄÉèÖÃÎļþÖÐÖ¸¶¨¡£ÏÂÃæÊÇÒ»¸ö³£¼ûµÄ¹ýʧÈÕÖ¾ÉèÖÃʾÀý£º
error_log /var/log/nginx/error.log;
µÇ¼ºó¸´ÖÆ
2.2. ×Ô½ç˵¹ýʧҳÃæ
Nginx¿ÉÒÔ×Ô½ç˵¹ýʧҳÃ棬ʹÓû§ÔÚÓöµ½¹ýʧʱÄܹ»¿´µ½ÓѺõÄÌáÐÑÐÅÏ¢¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔ½ç˵404¡¢500µÈ³£¼û¹ýʧҳÃæ¡£ÏÂÃæÊÇÒ»¸ö×Ô½ç˵404¹ýʧҳÃæµÄÉèÖÃʾÀý£º
error_page 404 /404.html; location = /404.html { root /path/to/error/pages; internal; }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬error_pageÖ¸ÁîÖ¸¶¨ÁË·ºÆð404¹ýʧʱ·µ»ØµÄÒ³Ã棬locationÖ¸ÁîÖ¸¶¨Á˹ýʧҳÃæµÄλÖá£
2.3. HTTP״̬Âë
Nginxͨ¹ýHTTP״̬ÂëÀ´ÌåÏÖÇëÇóµÄ´¦Öóͷ£Ð§¹û£¬³£¼ûµÄ״̬ÂëÓÐ200¡¢301¡¢404¡¢500µÈ¡£¹ØÓÚ²î±ðµÄ״̬Â룬¿ÉÒÔͨ¹ýNginxµÄÉèÖÃÎļþ¾ÙÐÐÏìÓ¦µÄ´¦Öóͷ£¡£ÏÂÃæÊÇÒ»¸öÖض¨Ïò301״̬ÂëµÄÉèÖÃʾÀý£º
location /old-path { return 301 /new-path; }
µÇ¼ºó¸´ÖÆ
¸ÃÉèÖûὫËùÓжÔ/old-pathµÄÇëÇóÖض¨Ïòµ½/new-path¡£
2.4. ·´ÏòÊðÀí³¬Ê±´¦Öóͷ£
ÔÚ·´ÏòÊðÀíģʽÏ£¬Nginx×÷Ϊǰ¶ËÊðÀíЧÀÍÆ÷£¬ÊðÀíÓû§ÇëÇó²¢½«Æäת·¢¸øºó¶ËµÄÕæʵЧÀÍÆ÷¡£µ±ºó¶ËЧÀÍÆ÷´¦Öóͷ£ÇëÇóµÄʱ¼ä¹ý³¤Ê±£¬Nginx»á·ºÆð³¬Ê±¹ýʧ¡£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸ÄNginxµÄÉèÖÃÎļþÀ´µ÷½â³¬Ê±Ê±¼ä¡£ÏÂÃæÊÇÒ»¸ö·´ÏòÊðÀí³¬Ê±´¦Öóͷ£µÄÉèÖÃʾÀý£º
location / { proxy_pass http://backend; proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 20s; }
µÇ¼ºó¸´ÖÆ
¸ÃÉèÖûὫÇëÇóת·¢¸øºó¶ËЧÀÍÆ÷£¬²¢ÉèÖÃÅþÁ¬¡¢·¢ËͺͶÁÈ¡³¬Ê±Ê±¼ä¡£
×ܽá
±¾ÎÄÏÈÈÝÁËNginxÈÕÖ¾ÆÊÎö¹¤¾ßºÍ¹ýʧ´¦Öóͷ£¼¼ÇÉ£¬Í¬Ê±ÌṩÁËÏà¹ØµÄ´úÂëʾÀý¡£Í¨¹ýÉîÈëÃ÷È·NginxµÄÈÕÖ¾ÆÊÎö¹¤¾ßºÍ¹ýʧ´¦Öóͷ£¼¼ÇÉ£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØ¼à¿ØºÍά»¤NginxЧÀÍÆ÷µÄÔËÐÐ״̬£¬Ìá¸ßϵͳµÄ¿É¿¿ÐÔºÍÐÔÄÜÌåÏÖ¡£Í¬Ê±£¬±¾ÎÄҲϣÍû¶Ô¶ÁÕßÔÚÔËάºÍ¿ª·¢Àú³ÌÖеÄÊÂÇéÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÉîÈëÃ÷È·NginxÈÕÖ¾ÆÊÎö¹¤¾ßºÍ¹ýʧ´¦Öóͷ£¼¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡