  
<div id="snippet_247" class="snippet">
  <h2><a href="/snippets/247-Logging-nginx-to-remote-loghost-with-syslog-ng-" title="Logging nginx to remote loghost with syslog-ng. - Shell Script (Bash) - nginx, loghost, remote logging, syslog-ng">Logging nginx to remote loghost with syslog-ng.</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/131-Shell-Script-Bash-">
      Shell Script (Bash)</a> posted about 1 year ago by marko
          
  </div>

  <div class="body">
    <p>Nginx does not support syslog by default, so you have to patch it with a third party module. This snippet relies on that you have configured xstow. See <a href="http://snippets.aktagon.com/snippets/188-Installing-git-without-getting-screwed-over-when-it-s-time-to-uninstall-upgrade-or-install-package-maintainer-s-version">this</a> snippet for instructions on xstow configuration.
<pre class="active4d"><span class="line-numbers">   1 </span> cd /tmp
<span class="line-numbers">   2 </span> wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
<span class="line-numbers">   3 </span> tar zxvf nginx-0.6.32.tar.gz
<span class="line-numbers">   4 </span> mv nginx-0.6.32 src-0.6.32-orig
<span class="line-numbers">   5 </span> wget <span class="String"><span class="String">&quot;</span>http://wiki.codemongers.com/NginxModules?action=AttachFile&amp;do=get&amp;target=syslog.patch<span class="String">&quot;</span></span> -O syslog.patch
<span class="line-numbers">   6 </span> patch -p0 <span class="Operator">&lt;</span> syslog.patch
<span class="line-numbers">   7 </span> cd src-0.6.32-orig
<span class="line-numbers">   8 </span> ./configure --prefix=/usr/local/stow/nginx --with-syslog
<span class="line-numbers">   9 </span> make
<span class="line-numbers">  10 </span> sudo make install
<span class="line-numbers">  11 </span> cd /usr/local/stow
<span class="line-numbers">  12 </span> sudo xstow nginx
</pre></p>


	<p>I-am-a-noob-at-syslog-disclaimer: This might be a totally wrong way to configure the server and client(s), so it is subject for refinement. In my experience it works though.</p>


	<p>Configuring the <strong>client</strong>. Add the following lines to the end of /etc/syslog-ng/syslog-ng.conf and restart syslog-ng with /etc/init.d/syslog-ng restart. Nginx logs in facility local5 and the hostname of the loghost is &#8220;loghost&#8221;. You could just as well use the IP of the loghost.
<pre class="active4d"><span class="line-numbers">   1 </span> filter f_local5 { facility(local5)<span class="Operator">;</span> }<span class="Operator">;</span>
<span class="line-numbers">   2 </span> destination d_loghost {tcp(<span class="String"><span class="String">&quot;</span>loghost<span class="String">&quot;</span></span> port(514))<span class="Operator">;</span>}<span class="Operator">;</span>
<span class="line-numbers">   3 </span> log { source(s_all)<span class="Operator">;</span> filter(f_local5)<span class="Operator">;</span> destination(d_loghost)<span class="Operator">;</span> }<span class="Operator">;</span>
</pre></p>


	<p>Configuring the <strong>server</strong>. Add the following lines to the end of /etc/syslog-ng/syslog-ng.conf and restart syslog-ng with /etc/init.d/syslog-ng restart. Also if you run a cluster of nginx servers it might be wise to put all the output in one file, instead of separate files per host.
<pre class="active4d"><span class="line-numbers">   1 </span> source s_remote { <span class="FunctionName">tcp</span>()<span class="Operator">;</span> }<span class="Operator">;</span>
<span class="line-numbers">   2 </span> destination d_clients { file(<span class="String"><span class="String">&quot;</span>/var/log/HOSTS/nginx.<span class="Variable"><span class="Variable">$</span>HOST</span><span class="String">&quot;</span></span>)<span class="Operator">;</span> }<span class="Operator">;</span>
<span class="line-numbers">   3 </span> log { source(s_remote)<span class="Operator">;</span> destination(d_clients)<span class="Operator">;</span> }<span class="Operator">;</span>
</pre></p>


	<p>Test the logging by running this from the client.
<pre class="active4d"><span class="line-numbers">   1 </span> logger -p local5.info Hubbabubba
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/143-nginx">nginx</a>, <a href="/tags/563-loghost">loghost</a>, <a href="/tags/564-remote-logging">remote logging</a>, <a href="/tags/565-syslog-ng">syslog-ng</a>
    
    
  </div>
</div>






