The client address sits in every access log line nginx writes by default, and under the GDPR it counts as personal data. That leaves an operator with a choice none of the usual answers make comfortable: keep the logs and keep the addresses, shorten the retention until the logs stop being useful for debugging, or pipe everything through a filter after the fact and hope it never falls over.
This module offers a fourth: write something else in the first place. It provides the client address with its host part cleared, and a one way pseudonym of it, as ordinary nginx variables. What you do with them is up to the log format and the limit keys you write.
The smallest configuration that works
http {
ip_anon_v4_prefix 24; # the default
ip_anon_v6_prefix 48; # the default
log_format anonymised '$remote_addr_anon - - [$time_local] "$request" '
'$status $body_bytes_sent';
server {
access_log /var/log/nginx/access.log anonymised;
}
}
Both directives are shown at their defaults, so the work is in the log format. That is the part worth saying out loud: the module adds variables, it does not take anything away. A server left on the stock combined format keeps writing full addresses no matter how carefully the prefixes are set. Nothing warns you about it, because from nginx’s point of view nothing is wrong.
What the masking does
Clear the bits below the prefix, keep the format nginx uses for $remote_addr. Measured against the defaults:
| client address | $remote_addr_anon |
|---|---|
| 192.0.2.137 | 192.0.2.0 |
| 203.0.113.44 | 203.0.113.0 |
| 2001:db8:dead:beef::1 | 2001:db8:dead:: |
| 2001:db8:1:2:3:4:5:6 | 2001:db8:1:: |
The prefixes are settable per location, which is more useful than it sounds: a login endpoint can keep more of the address than a static file does. The edges behave: 0 drops the address entirely and yields 0.0.0.0, 32 keeps it untouched, and a prefix wider than its address family is refused when the configuration is read rather than quietly clamped.
The key you should not reuse
There is a second variable, $binary_remote_addr_anon, in the form limit_req_zone and limit_conn_zone want. Using it looks like the obvious next step, and it is the one thing in this module worth stopping over.
limit_req_zone $binary_remote_addr_anon zone=bymask:1m rate=1r/s;
A masked address is not one client. With the default prefix it is up to 254 of them, and the zone counts them as one. Measured, two clients in the same /24 against rate=1r/s burst=3 nodelay:
key = $binary_remote_addr_anon
192.0.2.10 uses up the burst: 200 200 200 200 503 503
192.0.2.99 from the same /24: 503
198.51.100.7 from elsewhere: 200
key = $remote_addr_hash
192.0.2.10 uses up the burst: 200 200 200 200 503 503
192.0.2.99 from the same /24: 200
198.51.100.7 from elsewhere: 200

One client locked out its neighbours, and nothing in the configuration said it would. Behind carrier grade NAT or a corporate egress, where a /24 is not a street but a city, that is an outage waiting for the first impatient visitor. The masked address belongs in the log, where grouping a network together is exactly what you wanted. It does not belong in a limit key unless limiting whole networks is the stated intent.
The pseudonym
$remote_addr_hash is the way out of that bind: a keyed one way token of the full address, so two clients in one network stay apart while the token leads nowhere. It is opt in, absent until a secret is set:
ip_anon_hash_secret "something only this host knows";
log_format pseudonymous '$remote_addr_hash - - [$time_local] "$request" '
'$status $body_bytes_sent';
limit_req_zone $remote_addr_hash zone=perclient:10m rate=10r/s;
Measured, the same address twice and its neighbour in between:
192.0.2.137 -> 159945cb75ac0fa4
192.0.2.138 -> de77a2d110ef33d4
192.0.2.137 -> 159945cb75ac0fa4
Stable for one client, different for the next, which is what a unique visitor count and a per client rate limit both need. The secret is what makes it a pseudonym rather than a checksum anyone could recompute from a guessed address, so it is worth treating like a key: the same one across your hosts keeps the tokens comparable, and changing it changes every token at once.
ip_anon_hash_length decides how much of the digest is written, sixteen hex characters by default. Shortening it shortens the log, and it is tempting. The arithmetic is less tempting: at six characters the token holds 24 bits, and ten thousand distinct visitors in one window give a 95 percent chance that two of them share a token. At the default sixteen the same ten thousand visitors will not collide in any reasonable lifetime of the log. Shorten it only if a collision is genuinely harmless for what you are counting.
Making the pseudonym forget
A stable token is still a thread that runs through a year of logs. ip_anon_hash_period cuts it by mixing the current time window into the hash:
ip_anon_hash_period 24h;
Within the window a client keeps its token, so the day’s log still counts visitors and the rate limit still works. Across the window boundary the token changes and the two cannot be joined. Measured with a five second period, the same client before and after:
aadfb9b7c804db7a
af6198a358c84526
The window is wall clock, not per client, so every token changes at the same moment. A visitor who arrives a minute before midnight appears as two visitors. That is the price of the cut, and it is the reason to pick a period that matches what you actually count rather than the shortest one that sounds responsible.
Behind a proxy
What the module masks is the address of the connection. Behind a load balancer or a CDN that is the proxy, and masking it accomplishes nothing at all. Measured, a request carrying X-Forwarded-For: 192.0.2.137 to a server without set_real_ip_from:
$remote_addr 127.0.0.1
$remote_addr_anon 127.0.0.0
A tidy looking masked address, and the client’s real one is still sitting untouched in the header. If that header is in the log format, or reaches an upstream that logs it, the whole exercise has moved the address rather than removed it. ngx_http_realip_module has to be built in and configured first:
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
Then $remote_addr is the client, and everything above applies to it. Worth checking rather than assuming: realip is a standard module but not a default one, and an nginx -V without --with-http_realip_module will reject set_real_ip_from as an unknown directive at startup.
On a dual stack listener
A listener opened as listen [::]:80 ipv6only=off; hands IPv4 connections to nginx in their IPv4-mapped form, ::ffff:192.0.2.137. That form is an IPv6 address holding four IPv4 bytes at the very end, so an IPv6 prefix counting bits from the front does not cut anywhere useful on it. The module recognises the form and applies the IPv4 prefix to those four bytes instead.
The effect is that the same client gets the same treatment whichever listener it reaches. Measured, one client against both:
listen 127.0.0.1 127.0.0.1 127.0.0.0 c763229a7da89564
listen [::] ipv6only=off ::ffff:127.0.0.1 ::ffff:127.0.0.0 c763229a7da89564
The same bits are cleared either way, and the pseudonym comes out identical, because it is computed over the four IPv4 bytes in both cases. Only the notation differs, which matters if something downstream parses the log field as an address: it will see ::ffff: prefixes it may not have expected.
What it does not do
It does not make your logs anonymous in the legal sense, and treating it as though it did is the way to get this wrong. A /24 in a densely populated consumer network is a crowd; the same /24 in a small business range can be one building. A pseudonym remains personal data for as long as the secret exists, because the secret is what would let someone test a guessed address against a token. Shortening retention and limiting who can read the logs are still the same work they were before.
It touches the client address and nothing else. A X-Forwarded-For in the log format, a session cookie, a user id from an upstream, a referrer carrying a search term: all untouched, and any one of them can identify a visitor more precisely than the address would have.
On a connection with no address at all, a unix socket for instance, both variables fall back to what $remote_addr and $binary_remote_addr report there, which is unix: rather than nothing. That is deliberate: a limit key that came out empty would silently exempt those connections from every limit. The pseudonym has no value there at all, so a limit keyed on it would do the same thing, which is worth knowing if anything reaches you over a socket.
On FreeBSD
The sources ship as www/nginx-module-ip-anon, and the option IP_ANON in www/nginx, www/nginx-devel and www/freenginx builds them in as a loadable module:
make -C /usr/ports/www/nginx config # tick IP_ANON
make -C /usr/ports/www/nginx install clean
load_module modules/ngx_http_ip_anon_module.so;
Everything above was measured against version 0.2.0 on nginx 1.30.5 on FreeBSD 15.1, with client addresses supplied through realip so that each measurement names the address it is about.
Where it is
The sources, the full directive reference and the test suite: github.com/sysadmin-labs/nginx-ip-anon-module
The FreeBSD port, with its package state, its CVE status and the branches it is in: www/nginx-module-ip-anon on portstatus
BSD 2-Clause.