The Kohana manual (or for 3.0) gives the following configuration for doing clean url rewriting with Nginx:
location / {
index index.php index.html index.htm;
try_files $uri index.php;
}
location = index.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
I hit two problems with this.
Firstly, I had to change it to location = /index.php to get it to work.
Once I had done that it seemed to work fine.
However, I then realised that query string parameters were not working correctly. For example, if I had a URL of: http://mydomain.com/controller/param1/param2?myvar=123. Under Apache, that would work fine and $_GET['myvar'] would be set to 123. Under Nginx however, $_GET['myvar'] wouldn’t be set.
I fixed this by using: try_files $uri /index.php?$query_string;
My final, working config is therefore as follows:
location / {
try_files $uri /index.php?$query_string;
}
location = /index.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
This seems to be working fine on an Ubuntu 10.10 box with: nginx-full 0.8.54-4ppa14~maverick and php5-fpm 5.3.3-1ubuntu9.3.
It’s worth noting that the above won’t let you put any other .php files in your web root. If you want to be able to do that, you’ll want to use:
location ~ \.php$ {
instead of:
location = /index.php {
Note though that there is a bit of a security problem to be aware of with doing \.php$ – i’ll blog about this soon.