How To Restrict Multiple Directories With Nginx But Still Access Files?

June 8, 2014 906 views
I would like to know how to let people access files with a direct link on my server but restrict them from accessing all directories? Exemple: I want them to see I don't want people to be able to access either nor I have tried; command: sudo nano /etc/nginx/sites-available/ ... location ~ /(usa) { deny all; return 404; } ... But after that I can't access the rest myself... even with a direct link: Thank you for your help, Octav
2 Answers
Problem is solved!

So, by default Nginx has an option called autoindex set to off!
This command line: autoindex on; will activate the ability to see your directories (to everyone).

In my case, I wanted to restricted people from wondering around my server just by typing the and be able to see what is inside it without specifying the direct link to it example:

There is a command also that is important that sets a priority of file searching;

location / {
try_files $uri $uri/ =404;

This command means it will try to access the file 1st then the folder then give you an error 404 if nothing works.
More info:

**Also, my configuration has index.html, index.php, index.htm set as default. So if you don't want people to access other folders without typing in the whole path change either you default settings or change the name of your files ex: index1.html ...**
Have another answer? Share your knowledge.