I have an api running on localhost. To allow external access to the API under a specific domain path I've set a reverse proxy. This part works fine. Now I'm trying to filter access and allow only a single IP to connect to the API, in other words, deny all IP's connections except from a specific one.
With the configuration bellow all IPs are being blocked successfully, but it's also blocking the one IP I want to allow. I've researched and tried several fixes and I suspect I need to get the real_IP under the reverse proxy, but haven't manage to make it work for my specific situation. All help is appreciated. Here's the code for my nginx config file inside sites-available:
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name foo.com www.foo.com;
location / {
allow XX.XX.XX.XX;
#allow 127.0.0.1;
deny all;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/foo.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/foo.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
XX.XX.XX.XX is the ip I want to allow which is actually the server's actual IP. But I don't think it makes a difference. I've also tried adding the following inside "location /{ }" scope, but no luck:
set_real_ip_from XX.XX.XX.XX;
real_ip_header X-Forwarded-For;
real_ip_recursive on;