2

So I was trying to setup directory listing on my server with nginx, I followed the instructions step-by-step but nothing worked out -- always showing either 403 or 404 errors while permissions are all set to 755...

When I enable autoindex on the root location it worked fine, but when I put it on the "dl/" location, it either shows a 404 when requesting /dl or 403 when requesting /dl/

After i followed @Bryce Larson's steps...403 is gone now only 404 is there...which is still not okay...


# pwd

/root/Downloads/dl

# ls -lha

total 12K
drwxr-xr-x 2 nginx root 4.0K Nov 25 20:01 .
drwxr-xr-x 4 root  root 4.0K Nov 26 09:11 ..
-rwxr-xr-x 1 nginx root   26 Nov 25 20:01 blah.txt

Here's the nginx.conf:
https://0bin.net/paste/he2oIb2OFou4G9Fd#v5qt5M7scM8jlSRkl9B+GepP+PoInAHrfZrJNJ7Ch9U


And yeah I've restarted nginx a hundred times just to make sure it takes the new config...so what's wrong now?

Otherwise, how would you configure the nginx server for this purpose?

0

3 Answers 3

2

I eventually found the solution myself!

So to specify the location with root, you're going to have to specify the parent directory instead of the directory that you're planning to serve.

So as in my example I should specify /root/Downloads with the root parameter, then the server will try to find a directory named dl within it.
I did recall trying this way but the permission issues as mentioned by @Bryce Larson could be the reason why it didn't succeed.

Here's the final nginx.conf in case people are interested. The relevant lines are:

        location /dl/ {
            root      /root/Downloads;
            autoindex on;
        }

Conclusion:

The correct way to setup directory listing in nginx is to:

  • specify the parent directory of the directory that you're trying to serve.
  • make sure the permissions from / all the way to the directory that you're trying to serve is set executable, i.e chmod +X -R .
0

If you're serving it from root's home directory make sure the directories have execute permission for every directory from root (/) up to the directory you are serving. By default, root usually has 700 permissions on it's home directory. Execute permissions on directories are required to 'cd' to that directory, which nginx can't do if the execute permission isn't allowed on the directories for the user nginx runs under.

also:

chmod +X -R .

is quite useful as it only changes directory permissions and not files, see: https://www.franzoni.eu/chmod-and-the-capital-x/

3
  • I tried that by chmod -R +x /root and now it's just saying 404 :-(
    – Samm
    Nov 26, 2016 at 4:20
  • Are you trying to make /root/Downloads/dl served up, or /root/Downloads/pub as shown in your pwd? Nov 26, 2016 at 18:10
  • Ahh...i changed the name to dl..
    – Samm
    Nov 27, 2016 at 2:12
0

For anyone stumbling across this thread because they also get a 404 (like me), but couldn't resolve it with the solution above:

It appears that the autoindex on; doesn't work when the specified root directory contains spaces, even if they are escaped correctly.

In my test the following did NOT work:

server {
    listen 80;
    servername downloads.myserver.com;

    location / {
        # notice escaped space character here:
        root /mnt/mydrive/test\ test/;
        autoindex on;
    }
}

But this did work:

server {
    listen 80;
    servername downloads.myserver.com;

    location / {
        # notice no escaped space character here:
        root /mnt/mydrive/testtest/;
        autoindex on;
    }
}

If anyone knows why nginx doesn't like spaces in the root directory, I'd be happy to learn why.

1
  • Did you try root '/mnt/mydrive/test test/';?
    – Paul
    Jun 26, 2021 at 3:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .