btbytes (owner)

Revisions

gist: 146383 Download_button fork
public
Description:
User-based Website Directories with Nginx
Public Clone URL: git://gist.github.com/146383.git
Embed All Files: show embed
nginx_userdir.conf #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## REF: http://blog.sbf5.com/?p=6
# For requests starting with a tilde, break them into three components:
# 1. The username, everything after the tilde up to the first slash
# 2. The file location, everything after the username up to the last slash
# 3. The trailing slash(es)
# Then, rewrite to go to the f~/ branch.
location /~ {
    if ($request_uri ~ ^/~([^/]*)(/.*[^/]|)(/*)$) {
        set $homedir $1;
        set $filedir $2;
        set $trailingslashes $3;
        rewrite ^/~([^/]*)(/|$)(.*)$ f~/$3;
    }
}
 
# Here, the user-directory components have been parsed. Use an alias to set
# the file directory prefix. But if the file at the requested URI is a
# directory, we jump to the ~/ branch for additional processing.
location f~/ {
    alias /home/$homedir/public_html/;
    if (-d /home/$homedir/public_html$filedir) {
        rewrite ^f~/(.*) ~/$1;
    }
}
 
# Here, the request is for a directory in a user's home directory. We check
# that the request URI contained trailing slashes. If it did not, then we
# add the slashes and send a redirect. This circumvents Nginx's faulty
# internal slash-adding mechanism.
location ~/ {
    autoindex on;
    alias /home/$homedir/public_html/;
    if ($trailingslashes = "") {
        rewrite .* /~$homedir$filedir/ redirect;
    }
}