Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
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 9 '12 at 10:14
add comment

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.

share|improve this answer
    
helped a lot! thanks –  Totty Oct 10 '12 at 14:30
add comment

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

share|improve this answer
    
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 9 '12 at 8:19
add comment

Your Answer

 
discard

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.