Our Nginx server was without a proper ~user listing. While this is default in most apache configs, its not in Nginx. My friend Dominiek made a hack with a ruby script. This would have worked fine if had some more work but memory usage was pretty bad (especially for big files).
So, I started on a proper Nginx way to solve this and harness it’s power. By diving into the config I figured it out:
# the [a-zA-Z0-9] is for the greedy .
location ~ /~([a-zA-Z0-9]*)/(.*) {
root /home/;
autoindex on;
index index.html;
rewrite ^/~([a-zA-Z0-9]*)/(.*)$ /$1/www/$2 break;
}
Put this small snippet into the server config where you want userdirs enabled and enjoy your apache like mod_userdir.
Edit: made it work with subdirs
Edit: took advice of beate (see comments)
{ 4 } Comments
Hi, maybe i found a bug?
According to the nginx rewrite examples, “last” in the rewrite rule must be replaced by “break” if the location is given as above. Actually that change lead to a woring setup in my installation. Example (yes, i additionally adhered to the traditional public_html convention) :
location ~ /~([a-zA-Z0-9]*)/(.*) {
# the [a-zA-Z0-9] is for the greedy .
root /home/;
### autoindex on;
index index.html;
rewrite ^/~([a-zA-Z0-9]*)/(.*)$ /$1/public_html/$2 break;
}
maybe You fix this for the other readers?
THX
Beate
what if someone does site.com/~lusers/../
they’d see a list of users on the system, no?
Does not work, for me at least it redirects to the root dir.
Here’s a solution that works when you omit the trailing slash after directories:
http://blog.sbf5.com/?p=6
{ 1 } Trackback
[...] One website recommended using Nginx’s rewrite rules to emulate user-based website directories. This worked fairly well, except for one problem. Nginx has this internal behavior of fixing URL requests for directories so that they have a trailing slash. But when a rewrite rule was applied first, the fixed URL, presented to the web browser, was the rewritten location (with the trailing slash). [...]
Post a Comment