What I’m trying to do:
I have previously successfully implemented Rate limiting in an NGINX reverse proxy - excerpt:
geo $limit {
default 0; # Unknown users
123.456.78.999 1; # Known user IP
...
}
map $limit $unknown_user {
0 $binary_remote_addr;
1 "";
}
map $limit $known_user {
0 "";
1 $binary_remote_addr;
}
I have now had a Web Application Firewall (WAF) inserted in front of NGINX, the user’s IP addresses are now stored in $http_x_forwarded_for
variable.
Accordingly I must change the config to use that variable as follows:
geo $http_x_forwarded_for $limit {
default 0; # Unknown users
123.456.78.999 1; # Known user IP
...
}
map $limit $unknown_user {
0 $http_x_forwarded_for;
1 "";
}
map $limit $known_user {
0 "";
1 $http_x_forwarded_for;
}
Where I’m stuck:
The original value $binary_remote_addr
is, obviously BINARY - and is always 4 bytes for IPv4 addresses - which is good for performance reasons.
However, $http_x_forwarded_for
is STRING version - so occupies many more bytes.
I’d like to convert $http_x_forwarded_for
to its binary equivalent - e.g. $binary_x_forwarded_for
.
Does anyone know if such a String → Binary conversion can be easily done; and can provide sample code for insertion into the virtual host config file in sites-enabled
?
What I’ve already tried:
I’ve Googled & looked through NGINX documentation… but found nothing.