{"id":1142,"date":"2015-06-01T12:00:45","date_gmt":"2015-06-01T10:00:45","guid":{"rendered":"https:\/\/www.my-it-brain.de\/wordpress\/?p=1142"},"modified":"2015-05-31T18:06:19","modified_gmt":"2015-05-31T16:06:19","slug":"logging-und-logrotate-mit-nginx","status":"publish","type":"post","link":"https:\/\/www.my-it-brain.de\/wordpress\/logging-und-logrotate-mit-nginx\/","title":{"rendered":"Logging und logrotate mit NGINX"},"content":{"rendered":"<p>In diesem Artikel wird beschrieben, wie ich das Logging und <code>logrotate<\/code> meines <a href=\"https:\/\/de.wikipedia.org\/wiki\/Nginx\" target=\"_blank\">NGINX<\/a>-Servers konfiguriert habe. Dabei gehe ich kurz auf die beiden verwendeten Direktiven <code>error_log<\/code> und <code>ngx_http_log_module<\/code> ein.<\/p>\n<p>Damit dient dieser Artikel meiner Dokumentation und evtl. euch als Anregung, ein eigenes Logging zu konfigurieren.<\/p>\n<h2 id=\"logging\">Logging<\/h2>\n<p>Informationen zum Logging findet man in der offiziellen NGINX-Dokumentation.[1. <a href=\"http:\/\/nginx.org\/en\/docs\/\" target=\"_blank\">NGINX-Dokumentation<\/a>] Im folgenden werden die Direktiven <code>error_log<\/code>[2. <a href=\"http:\/\/nginx.org\/en\/docs\/ngx_core_module.html#error_log\" target=\"_blank\">NGINX core module error_log<\/a>] und <code>ngx_http_log_module<\/code>[3. <a href=\"http:\/\/nginx.org\/en\/docs\/http\/ngx_http_log_module.html\" target=\"_blank\">NGINX &#8211; ngx_http_log_module<\/a>] verwendet.<\/p>\n<p>Mein Server liefert mehrere Webseiten aus. Ich m\u00f6chte gern f\u00fcr jede Webanwendung ein separates Error-Log und Access-Log schreiben. Dabei wird folgendes Muster verwendet:<\/p>\n<ul>\n<li>Log-Verzeichnis: <em>\/var\/www\/\/logs<\/em><\/li>\n<li>Name f\u00fcr <code>error_log<\/code>: <em>_error.log<\/em><\/li>\n<li>Name f\u00fcr <code>access_log<\/code>: <em>_access.log<\/em><\/li>\n<\/ul>\n<h3 id=\"error_log\">Konfiguration des Error-Log<\/h3>\n<p>Die Error_log-Syntax ist denkbar einfach:<\/p>\n<pre>error_log log_file [ log_level ]\r\n<\/pre>\n<p><code>log_file<\/code> gibt den Pfad zur Log-Datei an. Mit <code>log_level<\/code> wird bestimmt, wie viele Informationen protokolliert werden sollen.<\/p>\n<h4 id=\"logging_level\">Log-Level[4. <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-configure-logging-and-log-rotation-in-nginx-on-an-ubuntu-vps\" target=\"_blank\">How To Configure Logging and Log Rotation in Nginx on an Ubuntu VPS<\/a>]<\/h4>\n<ul>\n<li><strong>emerg:<\/strong> Notfall, in dem sich das System in einem nicht nutzbaren Zustand befindet<\/li>\n<li><strong>alert:<\/strong> Ernste St\u00f6rung. Sofortiger Eingriff ist erforderlich<\/li>\n<li><strong>crit:<\/strong> Kritische Probleme, um die man sich k\u00fcmmern sollte<\/li>\n<li><strong>error:<\/strong> Ein Fehler ist aufgetreten. Hier funktioniert etwas nicht<\/li>\n<li><strong>warn:<\/strong> Ein ungew\u00f6hnliches Ereignis ist aufgetreten. Dies ist jedoch kein Grund zur Sorge<\/li>\n<li><strong>notice:<\/strong> Normale Vorg\u00e4nge werden ebenfalls protokolliert<\/li>\n<li><strong>info:<\/strong> Unn\u00fctzes Wissen &#8211; Nice to know<\/li>\n<li><strong>debug:<\/strong> Debugging-Informationen, welche helfen, ein Problem n\u00e4her zu analysieren<\/li>\n<\/ul>\n<p>Die Log-Level sind nach Priorit\u00e4t angeordnet. Wird das Level auf &#8222;error&#8220; gesetzt, so werden alle Events der Level error, crit, alert und emerg protokolliert.<\/p>\n<p>M\u00f6chte man rein gar nichts protokollieren, muss das Log nach <em>\/dev\/null<\/em> umgeleitet werden.<\/p>\n<pre>error_log \/dev\/null crit;\r\n<\/pre>\n<h3 id=\"access_log\">Konfiguration des Access-Log<\/h3>\n<p>Das Modul <code>ngx_http_log_module<\/code> besteht aus den Direktiven <code>access_log<\/code>, <code>log_format<\/code> und <code>open_log_file_cache<\/code>, von denen ich hier nur die ersten beiden verwenden werde.<\/p>\n<p>Mit der Direktive <code>log_format<\/code> kann das Format der Log-Dateien konfiguriert werden. Die einzelnen Formate werden \u00fcber einen Bezeichner ausgew\u00e4hlt. Dies kann z.B. wie folgt aussehen:<\/p>\n<pre>log_format compression '$remote_addr - $remote_user [$time_local] '\r\n                       '\"$request\" $status $bytes_sent '\r\n                       '\"$http_referer\" \"$http_user_agent\" \"$gzip_ratio\"';\r\n\r\naccess_log \/spool\/logs\/nginx-access.log compression buffer=32k;\r\n<\/pre>\n<p>Eine detaillierte Beschreibung aller verf\u00fcgbaren Parameter kann der offiziellen Dokumentation entnommen werden.[5. <a href=\"http:\/\/nginx.org\/en\/docs\/http\/ngx_http_log_module.html#log_format\" target=\"_blank\">NGINX -log_format<\/a>]<\/p>\n<p>Ich selbst verwende aktuell ausschlie\u00dflich das Format <em>combined<\/em>. Dieses ist bereits in der Standardinstallation enthalten. Es sieht wie folgt aus:<\/p>\n<pre>log_format combined '$remote_addr - $remote_user [$time_local]  '\r\n\t\t    '\"$request\" $status $body_bytes_sent '\r\n\t\t    '\"$http_referer\" \"$http_user_agent\"';\r\n<\/pre>\n<p>F\u00fcr die Protokollierung meiner Webanwendungen wird daher folgendes in die jeweiligen Server-Direktiven eingetragen:<\/p>\n<pre>server {\r\n...\r\naccess_log \/var\/www\/\/logs\/_access.log combined;\r\n...\r\n}\r\n<\/pre>\n<p>Falls man das Access-Log deaktivieren m\u00f6chte, kann man dies durch den folgenden Eintrag erreichen:<\/p>\n<pre>access_log off;\r\n<\/pre>\n<p>Nun werden schon mal alle Log-Dateien nach Webanwendungen getrennt in das Verzeichnis <em>\/var\/www\/\/logs<\/em> geschrieben.<\/p>\n<p>Im n\u00e4chsten Abschnitt gehe ich darauf ein, wie man verhindert, dass die Festplatte mit Log-Dateien vollgeschrieben wird.<\/p>\n<h2 id=\"rotate-nginx-logs\">Rotation der NGINX Log-Dateien<\/h2>\n<p>Zum Rotieren der Logs verwende ich die Anwendung <code>logrotate<\/code>. Diese ist bei Ubuntu bereits in der Standardinstallation enthalten.<\/p>\n<p>Es wird ein Skript im Verzeichnis <em>\/etc\/logrotate.d<\/em> erstellt und folgender Inhalt eingef\u00fcgt.<\/p>\n<pre>\/var\/www\/\/logs\/*.log {\r\n        daily\r\n        missingok\r\n        rotate 31\r\n        compress\r\n        delaycompress\r\n        notifempty\r\n        sharedscripts\r\n        postrotate\r\n                [ -f \/var\/run\/nginx.pid ] &amp;&amp; kill -USR1 `cat \/var\/run\/nginx.pid`\r\n        endscript\r\n}\r\n<\/pre>\n<p>Mit diesem Skript wird <code>logrotate<\/code> angewiesen, die Log-Dateien im Verzeichnis <em>\/var\/www\/\/logs\/<\/em> t\u00e4glich zu rotieren und die letzten 31 Log-Dateien zu behalten. Die Log-Datei wird nicht rotiert, falls sie leer ist, also keine Eintr\u00e4ge enth\u00e4lt. Die \u00e4lteren Dateien werden dabei komprimiert, um Speicherplatz zu sparen.<\/p>\n<p>Die generelle Beschreibung von <code>logrotate<\/code> w\u00fcrde den Rahmen dieses Artikels sprengen. Weitere Informationen sind in der Manpage zu finden.[6. <a href=\"http:\/\/linux.die.net\/man\/8\/logrotate\" target=\"_blank\">logrotate(8) &#8211; Linux man page<\/a>]<\/p>\n<p>Damit ist der versp\u00e4tete Fr\u00fchjahrsputz auf diesem Server beendet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In diesem Artikel wird beschrieben, wie ich das Logging und logrotate meines NGINX-Servers konfiguriert habe. Dabei gehe ich kurz auf die beiden verwendeten Direktiven error_log und ngx_http_log_module ein. Damit dient dieser Artikel meiner Dokumentation und evtl. euch als Anregung, ein eigenes Logging zu konfigurieren. Logging Informationen zum Logging findet man in der offiziellen NGINX-Dokumentation.[1. NGINX-Dokumentation]<span class=\"continue-reading\"> <a href=\"https:\/\/www.my-it-brain.de\/wordpress\/logging-und-logrotate-mit-nginx\/\">[Weiterlesen&#8230;]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_metis_text_type":"","_metis_text_length":0,"_post_count":0,"footnotes":""},"categories":[51],"tags":[323,322,58,321,304,324,305,60,303],"class_list":["post-1142","post","type-post","status-publish","format-standard","hentry","category-linux","tag-access_log","tag-error_log","tag-linux","tag-logrotate","tag-nginx","tag-ngx_http_log_module","tag-planet","tag-ubuntu","tag-webserver"],"_links":{"self":[{"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/posts\/1142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/comments?post=1142"}],"version-history":[{"count":10,"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/posts\/1142\/revisions"}],"predecessor-version":[{"id":1153,"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/posts\/1142\/revisions\/1153"}],"wp:attachment":[{"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/media?parent=1142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/categories?post=1142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.my-it-brain.de\/wordpress\/wp-json\/wp\/v2\/tags?post=1142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}