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/(.*)$ {
    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; 1; 1;
Why "geo" and not "map"? Because with geo you can use ranges and networks. 1; 1;

Result for our IP's


Popular posts from this blog

FreeRadius and Google Workspace LDAP