ÔõÑùʵÏÖNginxµÄÈÕÖ¾¼Í¼ÉèÖÃ
ÔõÑùʵÏÖNginxµÄÈÕÖ¾¼Í¼ÉèÖÃ
NginxÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬¾³£±»ÓÃÓڴ´óÐÍÍøÕ¾ºÍÓ¦ÓÃЧÀÍ¡£ÔÚ¿ª·¢ºÍʹÓÃNginxʱ£¬ÈÕÖ¾¼Í¼ÊǺÜÊÇÖ÷ÒªµÄ£¬Ëü¿ÉÒÔÓÃÓÚ¼à¿ØÍøÕ¾Á÷Á¿¡¢ÅÅÅÌÎÊÌâºÍÆÊÎöÓû§ÐÐΪ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginxµÄÈÕÖ¾¼Í¼£¬²¢¸ø³öһЩÏêϸµÄ´úÂëʾÀý¡£
NginxµÄÈÕÖ¾¼Í¼ÉèÖÃÊÇͨ¹ýÉèÖÃÎļþnginx.confÀ´Íê³ÉµÄ¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý£º
http { # È«¾ÖÉèÖà log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # ½ç˵accessÈÕÖ¾µÄÊä³ö·¾¶ºÍÃûÌà access_log /var/log/nginx/access.log main; server { # ÏêϸµÄЧÀÍÆ÷ÉèÖà listen 80; server_name example.com; location / { root /var/www/html; index index.html; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖУ¬log_formatÖ¸Áî½ç˵ÁËÈÕÖ¾¼Í¼µÄÃûÌã¬mainÊÇÒ»¸ö×Ô½ç˵µÄÃûÌÃÃû³Æ£¬¿ÉÒÔƾ֤ÐèÇó×ÔÐнç˵¡£log_formatÖ¸ÁîÖаüÀ¨ÁËһЩ±äÁ¿£¬Èç$remote_addrÌåÏÖ¿Í»§¶ËµÄIPµØµã£¬$remote_userÌåÏÖ¿Í»§¶ËµÄÓû§Ãû£¬$time_localÌåÏÖÇëÇóµÄʱ¼ä£¬ $requestÌåÏÖÓû§ÌᳫµÄÇëÇó£¬$statusÌåÏÖЧÀÍÆ÷µÄÏìӦ״̬Â룬$body_bytes_sentÌåÏÖЧÀÍÆ÷·¢Ë͵ÄÏìÓ¦Êý¾Ý×Ö½ÚÊý£¬$http_refererÌåÏÖÇëÇóµÄȪԴҳÃ棬$http_user_agentÌåÏÖÓû§µÄä¯ÀÀÆ÷ÊðÀíÐÅÏ¢£¬$http_x_forwarded_forÌåÏÖÇëÇóµÄÕæʵ¿Í»§¶ËIPµØµã£¨ÈôÊÇʹÓÃÁËÊðÀíµÄ»°£©¡£Óû§¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐ×Ô½ç˵ÃûÌá£
access_logÖ¸ÁîÓÃÓÚÉèÖÃNginxµÄaccessÈÕÖ¾µÄÊä³ö·¾¶ºÍÃûÌã¬ÉÏÊöʾÀýÖеÄ·¾¶Îª/var/log/nginx/access.log£¬ÃûÌÃΪmain¡£Óû§¿ÉÒÔƾ֤ÏêϸÐèÇó¸ü¸Ä·¾¶ºÍÃûÌá£
ΪÁ˲âÊÔÉèÖÃÊÇ·ñÉúЧ£¬¿ÉÒÔʹÓÃÏÂÁînginx -t¾ÙÐмì²é¡£ÈôÊÇÉèÖÃÎļþûÓйýʧ£¬ÏÂÁî»á·µ»Ønginx: configuration file /etc/nginx/nginx.conf test is successful¡£
ÁíÍ⣬Nginx»¹Ö§³Ö¹ýʧÈÕÖ¾µÄ¼Í¼¡£¹ýʧÈÕÖ¾¿ÉÒÔÓÃÓڼͼһЩЧÀÍÆ÷¶ËµÄ¹ýʧÐÅÏ¢£¬ÈçÅþÁ¬³¬Ê±¡¢ÇëÇóÎÞЧµÈ¡£ÏÂÃæÊÇÒ»¸ö¹ýʧÈÕÖ¾µÄʾÀý£º
http { # È«¾ÖÉèÖà error_log /var/log/nginx/error.log; server { # ÏêϸµÄЧÀÍÆ÷ÉèÖà listen 80; server_name example.com; location / { root /var/www/html; index index.html; } } }
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖУ¬error_logÖ¸ÁîÉèÖÃÁ˹ýʧÈÕÖ¾µÄÊä³ö·¾¶£¬Â·¾¶Îª/var/log/nginx/error.log¡£Í¬Ñù£¬Óû§¿ÉÒÔƾ֤ÏêϸÐèÇó¸ü¸Ä·¾¶¡£
ͨ¹ýÉÏÊöµÄÉèÖã¬ÎÒÃÇ¿ÉÒÔʵÏÖNginxµÄÈÕÖ¾¼Í¼¹¦Ð§¡£ÔÚÏÖʵʹÓÃÖУ¬¿ÉÒÔÍŽáÈÕÖ¾ÆÊÎö¹¤¾ßʹÓã¬ÈçELK Stack¡¢SplunkµÈ£¬ÒÔ±ã¸üºÃµØÆÊÎöºÍ¼à¿ØÍøÕ¾Á÷Á¿ºÍÓû§ÐÐΪ¡£
×ܽáÆðÀ´£¬ÉèÖÃNginxµÄÈÕÖ¾¼Í¼ÓÐÒÔϼ¸¸ö°ì·¨£º
ÔÚnginx.confÖнç˵ÈÕÖ¾¼Í¼µÄÃûÌã¬Ê¹ÓÃlog_formatÖ¸Á¿ÉÒÔ×Ô½ç˵ÃûÌá£
ʹÓÃaccess_logÖ¸ÁîÉèÖÃaccessÈÕÖ¾µÄÊä³ö·¾¶ºÍÃûÌá£
ʹÓÃerror_logÖ¸ÁîÉèÖùýʧÈÕÖ¾µÄÊä³ö·¾¶¡£
ÒÔÉϾÍÊǹØÓÚÔõÑùʵÏÖNginxµÄÈÕÖ¾¼Í¼ÉèÖõÄÏÈÈÝ£¬Ï£Íû¶Ô¸÷ÈËÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʵÏÖNginxµÄÈÕÖ¾¼Í¼ÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡