NginxÈÕÖ¾ÇиîÉèÖÃÆÊÎö£¬ÖÎÀíÍøÕ¾ÈÕÖ¾´æ´¢
nginxÈÕÖ¾ÇиîÉèÖÃÆÊÎö£¬ÖÎÀíÍøÕ¾ÈÕÖ¾´æ´¢
ÔÚÒ»¸öÍøÕ¾µÄÔËÐÐÀú³ÌÖУ¬ÈÕÖ¾ÊǺÜÊÇÖ÷ÒªµÄ¡£Ëü¿ÉÒÔΪÍøÕ¾µÄÔËÐÐ״̬ÌṩÏêϸµÄ¼Í¼£¬×ÊÖú¿ª·¢ÕߺÍÖÎÀíÔ±ÆÊÎöÎÊÌ⣬ÓÅ»¯ÐÔÄÜ¡£È»¶ø£¬Ëæ×ÅÍøÕ¾µÄÒ»Ö±Éú³¤£¬ÈÕÖ¾Îļþ»áÔ½À´Ô½´ó£¬Õâ»á¶ÔЧÀÍÆ÷µÄ´æ´¢¿Õ¼äºÍÐÔÄܱ¬·¢¼ç¸º¡£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔʹÓÃNginxµÄÈÕÖ¾ÇиЧ£¬½«ÈÕÖ¾Îļþ׼ʱ¼ä»ò¾Þϸ¾ÙÐвð·Ö£¬´Ó¶øʵÏÖÈÕÖ¾µÄÓÐÓÃÖÎÀíÓë´æ´¢¡£
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬²¢ÇÒ¿ÉÒÔͨ¹ýÉèÖÃÎļþÎÞаµØµ÷½âÆ书ЧºÍÐÐΪ¡£ÏÂÃ棬ÎÒÃǽ«Ê¹ÓÃÒ»¸ö¼òÆÓµÄÀý×ÓÀ´ÑÝʾÔõÑùÉèÖÃNginxʵÏÖÈÕÖ¾µÄÇи
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÖ¸¶¨ÈÕÖ¾µÄÃûÌúʹ洢·¾¶¡£ÔÚNginxµÄhttpÄ£¿éÖУ¬¿ÉÒÔͨ¹ýÌí¼ÓÒÔÏ´úÂëÀ´½ç˵ÈÕÖ¾ÃûÌãº
http { ... log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬log_formatÖ¸Áî½ç˵ÁËÒ»¸öÃûΪaccessµÄÈÕÖ¾ÃûÌã¬ÆäÖаüÀ¨ÁËһЩ³£ÓõÄÈÕÖ¾±äÁ¿£¬Èç¿Í»§¶ËIPµØµã¡¢»á¼ûʱ¼ä¡¢ÇëÇóURL¡¢·µ»Ø״̬ÂëµÈ¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½«Õâ¸öÈÕÖ¾ÃûÌÃÓ¦Óõ½ÏêϸµÄ»á¼ûÈÕÖ¾ÖС£ÔÚNginxµÄserver¿éÖУ¬¿ÉÒÔͨ¹ýÌí¼ÓÒÔÏ´úÂëÀ´Ö¸¶¨»á¼ûÈÕÖ¾µÄ´æ´¢Â·¾¶ºÍÃûÌãº
server { ... access_log /var/log/nginx/access.log access; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬access_logÖ¸ÁîÖ¸¶¨ÁË»á¼ûÈÕÖ¾µÄ´æ´¢Â·¾¶ºÍÃûÌá£ÆäÖУ¬/var/log/nginx/access.logΪÈÕÖ¾ÎļþµÄ´æ´¢Â·¾¶£¬accessΪ֮ǰ½ç˵µÄÈÕÖ¾ÃûÌÃÃû³Æ¡£
ĬÈÏÇéÐÎÏ£¬Nginx»á°ÑËùÓеĻá¼ûÈÕ־дÈ뵽ͳһ¸öÎļþÖС£¿ÉÊÇ£¬µ±Õâ¸öÎļþ±äµÃºÜÊÇ´óʱ£¬ÎÒÃÇ¿ÉÄÜ»áÏ£Íû½«Ëü²ð·Ö³É¶à¸öСÎļþ¡£ÎªÁËʵÏÖÕâ¸ö¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʹÓÃNginxÌṩµÄlogrotate¹¤¾ß¡£
logrotateÊÇÒ»¸ö³£ÓõÄÈÕÖ¾Çи¾ß£¬¿ÉÒÔƾָ֤¶¨µÄ¹æÔòÀ´²ð·ÖÈÕÖ¾Îļþ¡£ÎÒÃÇ¿ÉÒÔ±àдһ¸öÃûΪnginxµÄÉèÖÃÎļþ£¬ÆäÖнç˵ÁËNginx»á¼ûÈÕÖ¾µÄ²ð·Ö¹æÔò¡£ÏÂÃæÊÇÒ»¸öʾÀý£º
/var/log/nginx/access.log { daily rotate 7 missingok notifempty compress postrotate /usr/sbin/nginx -s reopen endscript }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬/var/log/nginx/access.logΪÐèÒª±»²ð·ÖµÄÈÕÖ¾Îļþ·¾¶¡£dailyÖ¸¶¨ÁË°´Ìì²ð·Ö£¬rotate 7ÌåÏÖ±£´æ7ÌìµÄÈÕÖ¾Îļþ¡£missingokÌåÏÖÈôÊÇÈÕÖ¾Îļþ²»±£´æ£¬Ò²²»±¨´í¡£notifemptyÌåÏÖÈôÊÇÈÕÖ¾ÎļþΪ¿Õ£¬Ò²²»±¨´í¡£compressÌåÏÖ¶ÔÐÂÌìÉúµÄÈÕÖ¾Îļþ¾ÙÐÐѹËõ¡£postrotateºÍendscriptÖ®¼äµÄ´úÂë»áÔÚÈÕÖ¾ÎļþÇиîÍê³ÉºóÖ´ÐУ¬ÕâÀïʹÓÃ/usr/sbin/nginx -s reopenÀ´Í¨ÖªNginxÖØз¿ªÈÕÖ¾Îļþ¡£
×îºó£¬ÎÒÃÇÐèÒª½«Õâ¸önginxÉèÖÃÎļþ°²Åŵ½/etc/logrotate.d/Ŀ¼Ï¡£logrotate»á°´ÆÚɨÃèÕâ¸öĿ¼£¬È»ºóƾ֤ÆäÖеÄÉèÖÃÎļþ¾ÙÐÐÈÕÖ¾Çи
ÒÔÉϾÍÊÇʹÓÃNginxʵÏÖÈÕÖ¾ÇиîµÄ¼òÆÓʾÀý¡£Í¨¹ýºÏÀíÉèÖÃNginxµÄÈÕÖ¾ÃûÌúÍÇиî¹æÔò£¬ÎÒÃÇ¿ÉÒÔÓÐÓõØÖÎÀíºÍ´æ´¢ÍøÕ¾µÄ»á¼ûÈÕÖ¾¡£Õâ²»µ«¿ÉÒÔ½ÚÔ¼´æ´¢¿Õ¼ä£¬»¹ÄÜÌá¸ßЧÀÍÆ÷µÄÕûÌåÐÔÄÜ¡£Ï£Íû±¾ÎĶÔÄúÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNginxÈÕÖ¾ÇиîÉèÖÃÆÊÎö£¬ÖÎÀíÍøÕ¾ÈÕÖ¾´æ´¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡