Set up nginx to serve files from subdirectories

Nginx does not have the right to read the users files. And it's a very bad idea to put all your users files available on the Web.

A better idea is to only serve a dedicated directory in users home directory.

To serve the www folder in each user folder when accessing /<USER>, use the following location:

location ~ ^/(.+?)(/.*)?$ {
  alias /home/$1/www$2;
  index  index.html index.htm;
  autoindex on;
}

You should also allow Nginx to access this directory.

$ chmod 0755 /home/$USER/www

Many times 403 errors are due to permissions problems. Files in web directories should be world readable (chmod 644 or 664) and directories should be world readable and executable (chmod 755 or 775).


All the answers above give valid explanations. What has not been mentioned so far, is that error 403 might persist, even after setting the correct permissions, due to caching, depending on your cache settings.

To make sure this is not the case, reset cache:

rm -r /path/to/nginx/cache/*
systemctl restart nginx