need help cleaning up user directory and php rules

Edho P Arief edhoprima at
Sat Mar 14 20:59:07 MSK 2009

...and 10 hours later I fixed it by myself. Tested with most common
cases, also ensured basic logic. Can be cleaned up better (or perhaps
squash more bugs, if any) but I'm too sleepy right now.

Also probably rather slow. My own connection is currently slow and I
only tried this on remote system.

server {
        listen 80;
        index index.php;
        #change /usr/local/etc/nginx/ to default nginx root path
        if ($request_filename ~
^/usr/local/etc/nginx//~([a-zA-Z0-9]*)(.*[^/]|)(/*)$) {
                #$org_uri is old, unused variable but might have some
use for improvement (or deletion)
                set $org_uri $request_uri;
                set $is_userdir 1;
                set $newroot /home/$1/public_html;
                set $homedir $1;
                set $filedir $2;
                set $slashes $3;
                rewrite ^/~.* f~/;
        if ($is_userdir != 1) {
                set $newroot /usr/local/www/data;
                #set to random string if not userdir (see above).
                set $org_uri zxzz123;

        root $newroot;

        location f~/ {
                #$is_ok: check wheter to apply rewrite or not at this
and next location
                set $is_ok 1;
                if (-d /home/$homedir/public_html$filedir) {
                        set $is_ok 2;
                        rewrite ^f~/$ d~/;
                if ($is_ok = 1) {
                        rewrite ^f~/$ finaldest~/;

        location d~/ {
                #add slash for directory (if there's none yet - to
avoid unwanted rewrite by nginx)
                if ($slashes = "") {rewrite ^d~/$ /~$homedir$filedir/
redirect; set $is_ok 1; }
                if ($is_ok = 2) {rewrite ^d~/$ finaldest~/; set $is_ok 1; }

        location finaldest~/ {
                #extra: user-specific rewrite
                if ($homedir = sampleuser) {rewrite ^finaldest~/$ sampleuser~/;}
                #final destination (if there's no user-specific rewrite)
                rewrite ^finaldest~/$ $filedir$slashes;

        #user-specific rewrite block
        location sampleuser~/ {
                #or sampleuser~/$filedir$slashes; to get subfolder
location ruling
                rewrite ^sampleuser~/$ $filedir$slashes;
                #wordpress. haven't able to make use of try_files or
other modern mechanics yet
                if (!-e $request_filename) {
                        rewrite  ^(.*)$  /blog/index.php?q=$1  last;

        #just a standard php block.
        location ~ \.php$ {
                if (!-f $document_root$fastcgi_script_name) { return
404; break; }
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME
                include fastcgi_params;

O< ascii ribbon campaign - stop html mail -

More information about the nginx mailing list