Musings from Bruderstein

Alien in Berlin.

Nginx With Kohana in a Subdirectory

I found out if you actually read the docs, then it’s a lot easier to get working. None of the posts on the web about using Kohana under nginx were particularly helpful (due to the kohana site being located under a subdirectory), and as I took a while to get working I thought I’d post this as my findings in case it helps anyone else.

The critical part was understanding the phases that nginx uses as it processes a request.

The Notepad++ Plugin Admin System lives under /npp/pm/admin, which maps to a separate path /npp/pm/pluginadmin/ in the physical filesystem. Requests to /npp/pm/pluginadmin are all denied. The admin system uses the Kohana Framework, which has an app/ and a www/ directory. The www/ directory contains the main index.php file, which is the start point for every URL, and the app/ directory contains all the controllers, models and views for the site.

Configuration nginx-site.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
server {
  root /www/brotherstone

  location / {
      try_files $uri $uri/;
  }


  location ^~ /npp/pm/admin {

                rewrite /npp/pm/admin$ http://www.brotherstone.co.uk/npp/pm/admin/welcome permanent;

                rewrite /npp/pm/admin/(.*)$ /npp/pm/pluginadmin/www/$1;

        }



        location ^~ /npp/pm/pluginadmin/ {
                internal;


                if (-f $request_filename) {
                        break;
                }

                try_files $uri $uri/ /npp/pm/pluginadmin/www/index.php?kohana_uri=$uri;

                location ~ \.php$ {
                        fastcgi_pass    unix:/tmp/fcgi.sock;
                        fastcgi_index   index.php;
                        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include         fastcgi_params;
                }
  }
}

Line 9 starts the main location for URLs starting with (~ is not a regular expression, but a /starts with/ marker) /npp/pm/admin. One thing I’m still not terribly happy with is that should really be ^~ /npp/pm/admin/ - i.e. with the trailing slash.

Line 11 deals with exactly this case, that user requested exactly /npp/pm/admin without the trailing slash, and sends a redirect back to the user. This is the only thing that worked for me, Kohana seems to have a problem if the REQUEST_URI is not in it’s root, even if if it it’s root without the slash. Huh.

Line 13 does the main internal redirection, to /npp/pm/pluginadmin/www/. nginx will then re-evaluate which location entry to use after performing the rewrite. If I’d have added a break; on the end, it would have ended the cycle of rewrite - location selection - rewrite. A last on the end would mean that further rewrite rules in this location should not be performed, but the cycle should start again. nginx has an automatic protection of 10 cycles to stop infinite loops. Adding rewrite_log on; and setting the error_log level to notice writes the rewritten URIs to the error log, and can be (and was) very useful for debugging.

Inside the new location block, line 20 states that this location is internal, and can only be used from internally rewritten requests.

Line 23 checks that if the (rewritten) requested URL exists as a file itself, such as a CSS, javascript or image file. If it does, I don’t want to perform any further processing, just give them the file.

Line 27 try_files checks the file existance in order. Theorically, this should mean the if() block isn’t necessary. However, it doesn’t seem to work without the if block. The last option is effectively a rewrite to the kohana index.php script, without doing a further rewrite. The try_files command is recommended for speed, and should be used whenever possible.

The last embedded location just sends all .php files to the FastCGI server (I’m using spawn-fcgi).

If anyone has any ideas about getting rid of the first rewrite, or any other improvements, shout.

Comments