Kohana with Nginx – VirtualHost configuration

juin 10th, 2010
by Axel

In order to improve performances, I chose to switch web server for one of my websites. I switched from Apache 2 to Nginx, extremely fast and powerful. It’s not easy to configure though: it took me a really long time to configure Nginx for Kohana 2. Here is my configuration file:

server {
	listen 80;
	server_name www.xxxx.com;
	root /var/www/xxxx/prod/public;
	index index.php;

	# ROUTING TO KOHANA IF REQUIRED
	location / {
		try_files $uri $uri/ @kohana;
	}

	# BLOCKS ACCESS TO . FILES (.svn, .htaccess, ...)
	location ~ /\. {
		deny  all;
	}

	# FOR PHP FILES
	location ~* \.php$ {
	 	# PHP FILES MIGHT BE TO HANDLED BY KOHANA
		try_files $uri $uri/ @kohana;

		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}


	# HANDLES THE REWRITTEN URLS TO KOHANA CONTROLLER
	location @kohana
	{
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		include fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $document_root/index.php;
	}


	# CACHE CONTROL FOR STATIC FILES
	location ~* \.css|\.js|\.jpg|\.jpeg|\.png|\.gif|\.swf|\.svg|\.tiff|\.pdf$ {
		expires 30d;
	}

	# REDIRECTING MEDIAS TO STATIC
	location ^~ /medias/ {
		rewrite ^/medias/(.*) http://static.xxxx.com/$1 permanent;
		break;
	}

}



server {
	listen 80;
	server_name static.xxxx.com;
	root /var/www/xxxx/medias/;    
	expires 90d;

	location /videos/ {
		keepalive_timeout     200 190;

		#limit_conn   videos  2;
		mp4;
		limit_rate_after 512k;
		limit_rate 512k;

		error_page 404 = /videos/video_not_found.png;
	}
}

My homepage now loads in 1,5s instead of 7s before. Thanks to Nginx ;-)

Tags: , , , ,
Posted in System, Technology | Comments (2)

2 Responses to “Kohana with Nginx – VirtualHost configuration”

  1. Brennan Says:

    Thanks! Helped a lot in my deployment of Ushahidi on Nginx.

  2. Andrew Says:

    Lovely, worked like a charm. Thanks for putting this together.

Leave a Reply