简体   繁体   中英

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

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

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/ ...;
                 ^^^^^
}
  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.

why nginx if you want you can use symbolic link

usage : ln -s /path/to/file /path/to/symlink

ln -s /home/forge/laravel58/public/files site/files with full path

server {
    listen 80;
    listen 443 ssl;
    server_name www.old-name.com;
    return 301 $scheme://www.new-name.com$request_uri;
}

SOURCE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM