I have an Nginx reverse proxy on Fly, directing traffic to another Fly app.
I’m getting hit by a spam attack. I have been able to block by user-agent, but I need to be able to block IP addresses.
Everything I’ve read says I should be able to use nginx deny directives and a blocklist.
However, when I try this by attempting to block my own IP from hitting the specific endpoint, the IP deny rule never matches. I know the regex is matching, because deny all; does return 403. And I know I have the IP address right, because I can see my IPV4 as the leftmost value in X-Forwarded-For in my logs.
It seems I don’t understand something about how Fly + Nginx handles actual visitor IPs, and how to match them against deny rules. Any insight would be greatly appreciated!
This is the relevant part of my nginx.conf
http {
# ... snip
set_real_ip_from 172.16.0.0/12;
# I have also tried:
# set_real_ip_from 172.17.0.0/16;
# and:
# set_real_ip_from 0.0.0.0/0;
# and:
# set_real_ip_from MY-APP-IPV6;
# set_real_ip_from MY-APP-IPV4;
real_ip_header X-Forwarded-For; # also tried: Fly-Client-IP
real_ip_recursive on;
# ... snip
server {
location ~* members/api/send-magic-link { # have tried this in, and beside, main location / { } block
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Fly-Client-IP $remote_addr;
deny XXX.XX.XX.XXX; # this never matches, even when I can see this IP in X-Forwarded-For in my logs
allow all;
# deny all; # this works
# return 500; # this also works
# proxy_pass $origin_url;
}
# ... snip
}
@mayailurus Aha! Thank you for your helpful reply. Given that the other directives are (I think) things I have tried, I suspect that the module is missing.
I checked that deny IP works by default, but did not verify whether the Docker image supports real_ip out of the box (I assumed it would warn if it didn’t).