Basic Authentication con IP Wishlist en Nginx detrás de un Load Balancer

Basic Authentication con IP Wishlist en Nginx detrás de un Load Balancer

Usar basic authentication dentrás de un "Load Balancer/Reverse Proxy" con un wishlist de IPs para que puedan acceder directamente saltanto la autenticación.

server {
............................

auth_basic_user_file /etc/nginx/conf.d/.htpasswd;   // ruta donde se encuentra el .htpasswd
set_real_ip_from 172.18.0.0/24;      // Rango de red donde se encuentra el Load Balancer
real_ip_header X-Forwarded-For;

location /location/to/protect {
satisfy  any;
set $allow false;

if ($http_x_forwarded_for ~ " ?XXX\.XXX\.XXX\.XXX?$") {      // IP Whishlist
set $allow true;
set $auth_basic off;
}

if ($allow = false) {
set $auth_basic on;
}

auth_basic $auth_basic; 

}  // fin de "server" tag

Gist: https://gist.github.com/cloudopsTK/27bc677ce9bd761f8fd5484f8df8d12a

Posted on