Issues with nginx autoindex

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?


Solution 1:

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.

Solution 2:

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.

Solution 3:

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