<div dir="ltr">Hi,<div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 13, 2018 at 5:46 PM, Tom <span dir="ltr"><<a href="mailto:tom@keepschtum.win" target="_blank">tom@keepschtum.win</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>Hi,</div><div> </div><div>I'm wondering if anyone has successfully masked ip addresses in nginx before they are written to a log file.</div><div> </div><div>I understand there are reasons why you would and would not do this.</div><div> </div><div>Anyway, my config so far, which I believe works for ipv4 addresses, but probably on only a few formats of ipv6 addresses. I've used secondary map directives to append text to the short ip address as I couldn't work out how to concatenate the variable with text, so concatenated two variables instead. (Hope that makes sense).</div><div> </div><div> </div><div><div>log_format ipmask '$remote_addr $ip_anon';</div><div> </div><div>map $remote_addr $ip_anon {</div><div>  default $remote_addr;<br>  "~^(?P<ipv4>[0-9]{1,3}\.[0-9]{<wbr>1,3}.)(?P<junkv4>.*)" $ipv4$ipv4suffix;<br>  "~^(?P<ipv6>[^:]+:[^:]+)(?P<<wbr>junkv6>.*$)" '$ipv6 $junkv6';<br>}</div><div> </div><div>map - $ipv4suffix{<br> default 0.0;<br>}<br>map - $ipv6suffix{<br>  default XX;<br>}</div><div>server {<br>  listen 8080;<br>  listen [::]:8080;<br>  server_name _;<br>  access_log /tmp/ngn-ip.log ipmask;<br>  allow all;<br>}</div><div> </div><div> </div><div>Anyone got any thoughts on this?</div><div>Thanks</div></div></blockquote><div><br></div><div>I suspect it might be a bit more efficient to do this with a simple module than trying to play around with more variables, maps, and regular expressions. I hacked together a quick module to do this: <a href="https://github.com/p0pr0ck5/ngx_http_ip_mask_module">https://github.com/p0pr0ck5/ngx_http_ip_mask_module</a>. You could also do the same thing with a little bit of Lua scripting (simply AND-ing off the unwanted bits). I'd guess extending out the same logic for IPv6 wouldn't be too hard, but that's left as an exercise for the reader :p</div><div><br></div><div><br></div></div></div></div>