Magic with nginx geo and autoindex on/off
Long story short: Nginx does not support .htaccess, which can be a problem when you want to restrict autoindex to specific IPs.
This configuration is based on a minimal nginx vhost template.
IP-based autoindex
if ($check_ip) {
rewrite ^/(.*)$ /autoindex_redirect/$1 last;
}
location ~ ^/autoindex_redirect/(.*)$ {
internal;
alias /var/www/repo/$1;
autoindex on;
}
location / {
autoindex off;
}
We use a fake location autoindex_redirect only for our IPs, where autoindex is enabled. The internal directive ensures Nginx denies requests from outside. To make this work, define a geo block outside any server {}, anywhere inside http {}:
geo $check_ip {
default 0;
111.111.111.111 1;
111.111.222.111 1;
}
Why geo and not map? Because geo allows using IP ranges and networks:
111.111.111.0/24 1;
111.111.222.123-111.111.222.152 1;
Result
Autoindex will be shown only for our IPs:
Human Logic, AI Syntax...
Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.

Comments
Post a Comment