Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I index files only from specific folder of my public/ - Nginx?

Tags:

php

nginx

laravel

I have 2 txt files that I placed at /home/forge/laravel58/public/files; I want to index those 2 txt files when I goto my site/files

I've tried

location /files {
    #auth_basic "Restricted";
    #auth_basic_user_file /home/forge/laravel58/.htpasswd;
    alias /home/forge/laravel58/public/files;
    autoindex on;
}

Go to : site/files, and see

403 Forbidden Nginx

like image 977
code-8 Avatar asked Apr 11 '19 18:04

code-8


People also ask

What is auto index in nginx?

Definition of Nginx Autoindex. Nginx autoindex is used the ngx http autoindex module to process the requests which was ending with the “/” slash character and it will produce the listing directory. Basically, the request is passed by using the ngx http autoindex module when the specified module will not find the index.


2 Answers

The trailing slash is essential for autoindex to work, it should be:

location /files/ {
  alias /home/forge/laravel58/public/files/;
  autoindex on;
}

Next, check that nginx has execute permissions (+x) on every folder in the path.

After that remove any index file from this folder, by default it's index.html.

And finally, check that your location / directive has attempt to try directories:

location / {
  ...
  try_files $uri $uri/ ...;
                 ^^^^^
}
like image 188
Styx Avatar answered Oct 09 '22 23:10

Styx


  1. The other answer about trailing slash being "essential for autoindex to work" is 100% incorrect: trailing slash is not required here, although, it is, in actuality, the preferred paradigm, because otherwise you make it possible to access both regular files and directories like /filesSECRET, one level up from /files/, opening yourself to potential security issues.

  2. In your situation, where /files is the suffix of both the location, as well as alias, it is preferred to use root instead of alias. See http://nginx.org/r/alias and http://nginx.org/r/root.

  3. In order for http://nginx.org/r/autoindex to work, the UNIX user under which the nginx process is running must have "read" permission on the final directory of the path, as well as "execute" permissions for every part of the path.

    You can use stat(1), or ls -l, to examine permissions, and chmod(1) to change the permissions. You'd probably want o+rx on /home/forge/laravel58/public/files, as well as o+x on every single directory that's leading to the above one.

like image 45
cnst Avatar answered Oct 09 '22 22:10

cnst