4

I am trying to set up nginx so that a certain url produces the directory index of a certain directory on my server. Currently this is what my default.conf looks like.

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /files/ {
        root        /home/myfiles/Downloads;
        autoindex   on;
    }

However when I try to go to mysite.com/files/ or mysite.com/files I get a 403 Forbidden error.

Looking into the error log I saw

2012/01/08 16:23:18 [error] 17619#0: *1 "/home/myfiles/Downloads/files/index.html" is forbidden (13: Permission denied), client: some.ip.addr.ess, server: _, request: "GET /files/ HTTP/1.1",

I don't want it to search for files/index.html, I just want the directory index of Downloads. What do I need to change to make things work this way?

Varun Madiath
  • 143
  • 1
  • 1
  • 4

4 Answers4

11

In this case you need to use the alias directive instead of root.

When using root, a request to mysite.com/files/ will look in the local directory /home/myfiles/Downloads/files/ for an index file, and if not found will automatically generate a directory listing (since you have specified the autoindex option). Note how nginx appends /files/ to the root directory you specified.

Since for your case you want /files/ to be a synonym for your download directory, you need to use alias /home/myfiles/Downloads/ in the location block. Then, any request to mysite.com/files/ will be translated to the directory /home/myfiles/Downloads/ (e.g. mysite.com/files/bigfile.gz will become /home/myfiles/Downloads/bigfile.gz). Note that the trailing / on the alias is necessary.

See the documentation http://wiki.nginx.org/HttpCoreModule#alias for more details on alias.

Damien
  • 111
  • 2
7

Check whether Nginx has execute permissions for the all the directories in the parent directory tree. In your case, Nginx should have execute permissions for /, /home, /home/myfiles, /home/myfiles/Downloads or else Nginx cannot chdir to those directories.

An easy way to check this is to run namei -l /home/myfiles/Downloads.

Ellimist
  • 119
  • 3
  • Thanks, I didn't know it needed execute permissions for the entire heirachy. I had only set the execute permissions for the Downloads directory. – Varun Madiath Jan 09 '12 at 10:14
1

hi i also have autoindex error. BTW: I install nginx use a third party repo.

First i just check autoindex

/opt/tengine/sbin/nginx -V 2>&1 | grep auto

[root@sec002 tengine]# /opt/tengine/sbin/nginx -V 2>&1 | grep auto
            ngx_http_autoindex_module (static)

My server config

server {
    listen 8082;
    server_name sec002.xxx.com;
    root /tmp;
    autoindex on;
    location / {
        root /tmp/php/;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    }
}

i browser sec002.xxx.com:8082 ,error below:

2016/01/07 16:09:26 [error] 4205#0: *1 "/tmp/php/index.html" is not found (2: No such file or directory), client: 192.168.2.13, server: sec002.xxx.com, request: "GET / HTTP/1.1", host: "sec002.xxx.com:8082"

I ensure execute permission and nginx user are right it's werid

Finally, i give up and rebuild nginx by tar package with default. then it's worked

0

According to the nginx documentation:
"The request only reaches the ngx_http_autoindex_module when the ngx_http_index_module did not find an index file."

If an index is not defined for a location, it defaults to these values: index index.html.

Remove the index.html from /home/myfiles/Downloads/files/ and it should display the files in a directory listing

Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95
  • No index.html file exists in that location. Yet I still get the error. The files directory is owned by the group nginx, and has 750 permissions. All files in the director have the same owner and permissions are the files directory. – Varun Madiath Jan 09 '12 at 08:19