nginx and userdir

Here’s how I did it. And this one is actually work (on 0.7.41, that is). With PHP support no less.

Still need some cleanup though.

What it do: enables Apache-like mod_userdir with PHP support and per-user rewrite rules.

UPDATE: here’s the better version.

server {
       listen 80;
       server_name domain.tld;
       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;

4 thoughts on “nginx and userdir

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

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

  3. Pingback: nginx/mod_userdir: take 3 | animeBSD

  4. Pingback: nginx/mod_userdir: take 2 | animeBSD

Leave a Reply

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