0

I've recently enabled ipv6 on my site, and while I don't often have a need to manually look at an access_log file, on the occasion that I do it's now very jarring that my first (IP) column is sometimes 7 characters long and sometimes 39 characters.

Is there any way of padding the first column to be always (say) 39 characters wide?

I've googled and looked at the nginx docs and I can't find anything, but figured someone on here might know a trick...

I assume if nothing else it'll be possible with map somehow, but I only know to use regexp with that and can't think of a way to play with length or padding...

Codemonkey
  • 1,086
  • 4
  • 19
  • 41

1 Answers1

0

OK, since writing it I've come up with my best (read: only) solution:

map $remote_addr $padded_ip { "~^(.{39})" "$1";
                              "~^(.{38})" "$1 ";
                              "~^(.{37})" "$1  ";
                              "~^(.{36})" "$1   ";
                              "~^(.{35})" "$1    ";
                              "~^(.{34})" "$1     ";
                              "~^(.{33})" "$1      ";
                              "~^(.{32})" "$1       ";
                              "~^(.{31})" "$1        ";
                              "~^(.{30})" "$1         ";
                              "~^(.{29})" "$1          ";
                              "~^(.{28})" "$1           ";
                              "~^(.{27})" "$1            ";
                              "~^(.{26})" "$1             ";
                              "~^(.{25})" "$1              ";
                              "~^(.{24})" "$1               ";
                              "~^(.{23})" "$1                ";
                              "~^(.{22})" "$1                 ";
                              "~^(.{21})" "$1                  ";
                              "~^(.{20})" "$1                   ";
                              "~^(.{19})" "$1                    ";
                              "~^(.{18})" "$1                     ";
                              "~^(.{17})" "$1                      ";
                              "~^(.{16})" "$1                       ";
                              "~^(.{15})" "$1                        ";
                              "~^(.{14})" "$1                         ";
                              "~^(.{13})" "$1                          ";
                              "~^(.{12})" "$1                           ";
                              "~^(.{11})" "$1                            ";
                              "~^(.{10})" "$1                             ";
                              "~^(.{9})"  "$1                              ";
                              "~^(.{8})"  "$1                               ";
                              "~^(.{7})"  "$1                                "; }

Isn't it beautiful?

I assume the performance impact of this is minimal? Yes it's hideous and a horrible hack, but it'll be minimal CPU to execute this on each request, I assume... but I've learned never to assume anything about nginx!

Codemonkey
  • 1,086
  • 4
  • 19
  • 41
  • Interesting! But so huge construction usually a flag that this way is wrong. I suppose it will be better to write bash-script (or any comfortable language) which will be parse log in the way You want. – Sergey Serov May 17 '22 at 14:55