Skip to content

Nginx userdir list

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)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit

{ 4 } Comments

  1. Beate | May 28, 2008 at 12:56 | Permalink

    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

  2. phil | July 9, 2008 at 00:32 | Permalink

    what if someone does site.com/~lusers/../
    they’d see a list of users on the system, no?

  3. Drakonen | July 15, 2008 at 21:11 | Permalink

    Does not work, for me at least it redirects to the root dir.

  4. charles | November 3, 2008 at 01:00 | Permalink

    Here’s a solution that works when you omit the trailing slash after directories:

    http://blog.sbf5.com/?p=6

{ 1 } Trackback

  1. [...] 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

Your email is never published nor shared. Required fields are marked *