Nginx 403 error: directory index of [folder] is forbidden
If you have directory indexing off, and is having this problem, it's probably because the try_files you are using has a directory option:
location / {
try_files $uri $uri/ /index.html index.php;
} ^ that is the issue
Remove it and it should work:
location / {
try_files $uri /index.html index.php;
}
Why this happens
TL;DR: This is caused because nginx will try to index the directory, and be blocked by itself. Throwing the error mentioned by OP.
try_files $uri $uri/
means, from the root directory, try the file pointed by the uri
, if that does not exists, try a directory instead (hence the /
). When nginx access a directory, it tries to index it and return the list of files inside it to the browser/client, however by default directory indexing is disabled, and so it returns the error "Nginx 403 error: directory index of [folder] is forbidden".
Directory indexing is controlled by the autoindex
option: https://nginx.org/en/docs/http/ngx_http_autoindex_module.html
If you're simply trying to list directory contents use autoindex on;
like:
location /somedir {
autoindex on;
}
server {
listen 80;
server_name example.com www.example.com;
access_log /var/...........................;
root /path/to/root;
location / {
index index.php index.html index.htm;
}
location /somedir {
autoindex on;
}
}
Here is the config that works:
server {
server_name www.mysite2.name;
return 301 $scheme://mysite2.name$request_uri;
}
server {
#This config is based on https://github.com/daylerees/laravel-website-configs/blob/6db24701073dbe34d2d58fea3a3c6b3c0cd5685b/nginx.conf
server_name mysite2.name;
# The location of our project's public directory.
root /usr/share/nginx/mysite2/live/public/;
# Point index to the Laravel front controller.
index index.php;
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Then the only output in the browser was a Laravel error: “Whoops, looks like something went wrong.”
Do NOT run chmod -R 777 app/storage
(note). Making something world-writable is bad security.
chmod -R 755 app/storage
works and is more secure.