Magic with nginx geo and autoindex on/off

Long story short: nginx not support ".htaccess" and some times is it a problem. We have directory and we need to show index only for specific IP's.

Nginx config based on small nginx config.
  if ($check_ip) {
    rewrite ^/(.*)$ /autoindex_redirect/$1 last;
  }

  location ~ ^/autoindex_redirect/(.*)$ {
    internal;
    alias /var/www/repo/$1;
    autoindex on;
  }

  location / {
    autoindex off;
  }

We have fake location "autoindex_redirect" only for our IP's where autoindex is enabled and this location protected by "internal". This tells nginx not allow any request from the outside. And out of "server {}" directive, in any place inside "http {}", we need create geo list
  geo $check_ip {
    default 0;
    111.111.111.111 1;
    111.111.222.111 1;
  }
Why "geo" and not "map"? Because with geo you can use ranges and networks.
    111.111.111.0/24 1;
    111.111.222.123-111.111.222.152 1;

Result for our IP's

Comments

Popular posts from this blog

FreeRadius and Google Workspace LDAP

pssh (parallel-ssh) problems on Debian 10 with Python 3.7