nginx/mod_userdir: take 2

My previous attempt is pretty much a fail. It works but it shouldn’t be done that way.

Here be updated version to imitate mod_userdir functionality in nginx – pointing /~username to correct path. Complete with php(-cgi) handler.

upstream php-cgi {
        server 127.0.0.1:9000;
}
server {
listen 80;
        server mahleetserver.com;
        index index.php;
        client_max_body_size 3m;

        if ($uri ~ ^/~edho/blog) {
                set $err404 /~edho/blog/index.php;
                set $is_custom404 1;
        }

        if ($is_custom404 != 1) {
                set $err404 /404.html;
        }
        error_page 404 $err404;

        #username should only contain these letters, right?
        location ~ /~([A-Za-zd-_s]+)(.*)$ {
                alias /export/home/$1/public_html$2;
                location ~ .php {
                        if ($fastcgi_script_name ~ /~([A-Za-zd-_s]+)(.*)$ {
                                set $ud_name $1; set $ud_uri $2;
                        }
                        root /export/home/$ud_name/public_html;
                        try_files $ud_uri @404;
                        fastcgi_pass php-cgi;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$ud_uri;
                        include fastcgi_params;
                }
        }

        location / {
                root /var/www/data;
                rewrite ^/wiki/([^?]*)(?:?(.*))? /w/index.php?title=$1&$2;
                rewrite ^/wiki /w/index.php;
                rewrite ^/edogawaconan/signature.jpg /edogawaconan/signature.php;
        }
        location ~ .php$ {
                root /var/www/data;
                try_files $uri @404;
                fastcgi_pass php-cgi;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        location ~ /.ht {
                deny all;
        }
        location @404 {
                return 404;
        }

}

Key changes here is everything is done in simplest possible way I can think of. No more location hopping.

If you need custom 404 just add if ($uri) block. In the sample above I have my blog at /~edho/blog.

I use upstream block to prepare migration to god (from php-fpm).

Up next: god config for php-cgi. (aka say goodbye to spawn-fcgi / php-fpm)

Update: cleanup

2 thoughts on “nginx/mod_userdir: take 2

  1. Pingback: animeBSD · nginx/mod_userdir: take 3

Leave a Reply

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