Nginx userdir list

Posted by Drakonen on February 29, 2008 in Programming, Tech | Short Link

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)

Facebook Twitter Email

Tags: , , , ,

5 Comments

  • Beate says:

    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

  • phil says:

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

  • Drakonen says:

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

  • [...] 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). [...]

  • charles says:

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Copyright © 2005-2012 Draakwired All rights reserved.
The Shades theme, version 1.7, is a BuyNowShop.com creation.

Social links powered by Ecreative Internet Marketing