Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hey guys, i'm trying to make the autoindex feature only run for my ip address with this code:

server{ ... autoindex off; ... if ($remote_addr ~ ..*.*) { autoindex on; } ... } But it doesn't work. It gives my a 403 :/ Can someone help me :) Btw, i'm using Debian Lenny and Nginx 0.6 :)

EDIT: Here's my full configuration:

server {
    listen   80;
    server_name_in_redirect off;
    client_max_body_size 4M;
    server_tokens off;
#   log_subrequest on;  
    autoindex off;
#   expires max;
    error_page   500 502 503 504 /var/www/nginx-default/50x.html;

#   error_page  404  /404.html;

    set $myhome /bla/bla;
    set $myroot $myhome/public;
    set $mysubd $myhome/subdomains;

    log_format  new_log
    '$remote_addr - $remote_user [$time_local] $request '
            '"$status" "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';

    # Star nginx :@
    access_log /bla/bla/logs/access.log new_log;
    error_log /bla/bla/logs/error.log;

    if ($remote_addr ~ {
        autoindex on;

    # Subdomains
    if ($host ~* (.*)\.site\.org$) {
        set $myroot $mysubd/$1;

    # Static files
#   location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
#       access_log        off;
#       expires           30d;
#   }

    location / {
        root  $myroot;
        index  index.php index.html index.htm;

    # PHP
    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $myroot$fastcgi_script_name;
        include fastcgi_params;

    # .Htaccess
    location ~ /\.ht {
        deny  all;


I forgot to mention that when i add the code to remove static files from my access log, the static files cannot be accessed. I don't know if it's relevant :)

share|improve this question

It's possible to do this in a fairly hacky way by using a version of the technique described here by Endre Szabó.

Essentially it's to enable autoindex unconditionally (I don't believe there is currently any other way to enable it) and then to forbid access to directories where there is no index.html present, unless the requesting IP address matches the specific one defined in the configuration.

server {
    autoindex on;

    location / {
        root   /your/htdocs;

        # If the requested filename is not a directory,
        # stop processing further conditions.
        if (!-d $request_filename) {

        # If index.html exists in the requested directory,
        # stop processing any further conditions.
        if (-f $request_filename/index.html) {

        # If the requesting IP address is not our special one,
        # forbid the request, inhibiting autoindex.
        if ($remote_addr != "") {
            return 403;
share|improve this answer

Try doing it like this:

if ($remote_addr = "") {
    autoindex on;
share|improve this answer
I did an edit of my original post :) – Pockata Jul 14 '10 at 23:15
When i test the configuration, it says that i can't use autoindex in there :/ Is this feature available in the 0.8 version ? If it's available, i'll switch to it immediatelly! – Pockata Jul 15 '10 at 11:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.