1

I'm trying to set up a sort of shared hosting with nginx where each user has a public_html directory. Requests to /~username are aliased to /home/domain-users/username/public_html. Problem is, I also want autoindexing enabled. With the config below added into a server block, requests to specific files like /~username/test.txt work fine; they are aliased to /home/domain-users/username/public_html/test.txt. However, when trying to request /~username or ~/username/, I get a 404 and /var/log/nginx/error.log reveals that autoindex is for some reason attempting to list files in /home/domain-users/username/public_htm (note the missing "l": the path is truncated.)

Since being able to access /~username without a trailing slash as a directory is not vital, I tried removing $public_html_path/ from the try_files directive. Then, even when requesting /~username/, autoindex is not called. Perhaps a literal trailing slash is required in try_files for autoindex to function.

# For requests to "/~username" without a trailing slash set $public_html_path ""; # Lazy quantifier at the end allows processing requests to "~/username". We add the trailing slash later in try_files location ~ ^\/\~(?<user_home_folder>[^\\n\/]+)(?<public_html_path>\/.*)?$ { alias /home/domain-users/$user_home_folder/public_html; autoindex on; try_files $public_html_path $public_html_path/ =404; } 

My research has yielded scant results, the closest being this StackOverflow question. Since this appears to be a bug in NGINX that's unlikely to be fixed (the last comment was well over a year ago) I'm looking for a workaround. Using the root directive instead of alias would be perfect, except then autoindex removes the critical /~username/ part of the URI, thinking that the files it lists are in the web root since, in a way, they are. Even hacky workarounds will be very much appreciated... Thanks!

1 Answer 1

0

When using alias with a regular expression location, you need to reconstruct the entire pathname in the alias value. See this document for details.

location ~ ^/~(?<user_home_folder>[^/]+)(?<public_html_path>/.*)?$ { alias /home/domain-users/$user_home_folder/public_html$public_html_path; autoindex on; } 

Using try_files and alias together can cause complications due to this issue. But, the default behaviour is virtually identical to the try_files statement in your question.

1
  • Amazing! I feel kinda dumb now... Commented Nov 17, 2017 at 12:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.