nginx/mod_userdir: take 3

After my first, and second attempt, which works but not quite right, here’s my latest attempt at creating mod_userdir-like on nginx. I finally give up in using unified .php block and decided to create separate file to handle .php:

Update: better version here.

php_params_basic:

fastcgi_pass 127.0.0.1:8000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

php_params_realpath:

fastcgi_param  SCRIPT_NAME        $realpath;
fastcgi_param  DOCUMENT_URI       $realpath;

php_params_classic:

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  DOCUMENT_URI       $document_uri;

And here’s the nginx.conf:

user http;
worker_processes 8;

# for supervisord
daemon off;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  gzip on;
  server {
    listen 80;
    server_name 10.1.1.10;
    root /srv/http/root;
    index index.html index.htm index.php;

    # example wordpress installation
    location /blog {
      try_files $uri /blog/index.php;
    }

    # example wordpress installation in user folder
    location ~ /~edogawaconan/blagz(/.*.php)$ {
      root /home/edogawaconan/public_html/blagz;
      set $realpath /~edogawaconan/blagz$1;
      try_files $1 /~edogawaconan/blagz/index.php;
      include php_params_basic;
      include php_params_realpath;
    }
    location ~ /~edogawaconan/blagz(/.*)$ {
      alias /home/edogawaconan/public_html/blagz$1;
      error_page 404 /~edogawaconan/blagz/index.php;
    }

    # userdir part
    location ~ /~([^/]+)(.*.php)$ {
      root /home/$1/public_html;
      set $realpath /~$1$2;
      try_files $2 /~edogawaconan/zzz.html;
      include php_params_basic;
      include php_params_realpath;
    }
    # note: don't use try_files here as you can't use index.php
    # with ability to fallback to 404 too as index.php would be served
    # as static file ("try_files $2 $2/index.php @404;" won't work).
    location ~ /~([^/]+)(.*)$ {
      alias /home/$1/public_html$2;
      error_page 404 /404.html;
    }

    location ~ .php$ {
      try_files $uri @404;
      include php_params_basic;
      include php_params_classic;
    }
    location @404 { return 404; }
    location ~ /.ht {
      deny all;
    }
    error_page 404 /404.html;
  }
}

I think/hope this one actually works.

Update: cleanup

Leave a Reply

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