Archive for January, 2012

Elgg Community 1.8.2 rewrite rules for Nginx

Today i’m trying out the Elgg Community in version 1.8.2
The Elgg Community is an OpenSource Community Software written in PHP/MySQL.

Because i’m using the Nginx Webserver instead of the Apache Webserver, i have to convert the rewrite-rules from htaccess to the Nginx rewrite-rules.
The most difficult part was to convert these condition:

# Everything else that isn't a file gets routed through the page handler
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_\-]+)$ engine/handlers/page_handler.php?handler=$1 [QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/page_handler.php?handler=$1&page=$2 [QSA]

 
This conditions can be solved with the following Nginx rules:

if (!-d $request_filename){
	set $rule_11 1$rule_11;
}
if (!-f $request_filename){
	set $rule_11 2$rule_11;
}
if ($rule_11 = "21"){
	rewrite ^/([A-Za-z0-9\_\-]+)$ /engine/handlers/page_handler.php?handler=$1;
}
if (!-d $request_filename){
	set $rule_12 1$rule_12;
}
if (!-f $request_filename){
	set $rule_12 2$rule_12;
}
if ($rule_12 = "21"){
	rewrite ^/([A-Za-z0-9\_\-]+)\/(.*)$ /engine/handlers/page_handler.php?handler=$1&page=$2;
}

 
The complete nginx.conf could look like this:

server {
	listen	    80;
	server_name	elgg.domain.com;
	root        /var/www-nginx/elgg/htdocs/;

	error_log	/var/log/nginx/error.log;
	access_log	/var/log/nginx/access.log;

	index         index.php index.html;
	fastcgi_index index.php;
	
	client_max_body_size      8M;
	client_body_buffer_size 256K;

	rewrite ^/pg\/([A-Za-z0-9\_\-]+)$ /engine/handlers/page_handler.php?handler=$1&$args;
	rewrite ^/pg\/([A-Za-z0-9\_\-]+)\/(.*)$ /engine/handlers/page_handler.php?handler=$1&page=$2&$args;
	rewrite ^/tag\/(.+)\/?$ /engine/handlers/page_handler.php?handler=search&page=$1;
	rewrite ^/action\/([A-Za-z0-9\_\-\/]+)$ /engine/handlers/action_handler.php?action=$1&$args;
	rewrite ^/cache\/(.*)$ /engine/handlers/cache_handler.php?request=$1&$args;
	rewrite ^/services\/api\/([A-Za-z0-9\_\-]+)\/(.*)$ /engine/handlers/service_handler.php?handler=$1&request=$2&$args;
	rewrite ^/export\/([A-Za-z]+)\/([0-9]+)\/?$ /engine/handlers/export_handler.php?view=$1&guid=$2;
	rewrite ^/export\/([A-Za-z]+)\/([0-9]+)\/([A-Za-z]+)\/([A-Za-z0-9\_]+)\/$ /engine/handlers/export_handler.php?view=$1&guid=$2&type=$3&idname=$4;
	rewrite /xml-rpc.php /engine/handlers/xml-rpc_handler.php;
	rewrite /mt/mt-xmlrpc.cgi /engine/handlers/xml-rpc_handler.php;
	rewrite ^/rewrite.php$ /install.php;
	if (!-d $request_filename){
		set $rule_11 1$rule_11;
	}
	if (!-f $request_filename){
		set $rule_11 2$rule_11;
	}
	if ($rule_11 = "21"){
		rewrite ^/([A-Za-z0-9\_\-]+)$ /engine/handlers/page_handler.php?handler=$1;
	}
	if (!-d $request_filename){
		set $rule_12 1$rule_12;
	}
	if (!-f $request_filename){
		set $rule_12 2$rule_12;
	}
	if ($rule_12 = "21"){
		rewrite ^/([A-Za-z0-9\_\-]+)\/(.*)$ /engine/handlers/page_handler.php?handler=$1&page=$2;
	}
	
	location ~ \.php$ {
		include fastcgi_params;

		# Assuming php-fastcgi running on localhost port 9000
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

		fastcgi_connect_timeout 60;
		fastcgi_send_timeout 180;
		fastcgi_read_timeout 180;
		fastcgi_buffer_size 128k;
		fastcgi_buffers 4 256k;
		fastcgi_busy_buffers_size 256k;
		fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;
	}
	
	# Do not put CSS there or it will break simplecache
	location ~* \.(bmp|js|gif|ico|jpg|jpeg|png)$ {
		expires max;
		# log_not_found off;
	}

}