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;
	}

}
  1. You can get rid of the if by using try_files. Also, you want to put all your rewrite rules under a single location. All of it is documented in the Nginx wiki.

      • tdeuling
      • January 3rd, 2012

      Thanks for your tip. Do you mean something like that:

      # Check cache/css/js requests first, as we know the files won't exist
      location ~ ^/cache/ {
      rewrite ^/cache\/(.*)$ /engine/handlers/cache_handler.php?request=$1&$args;
      }
      location ~* ^/pg/(css|js) {
      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;
      }
      # For all other requests, try the file, or rewrite if it's PHP
      location / {
      try_files $uri $uri/ @rewrite;
      }

      location @rewrite {

      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 ^/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;
      }

      }

      I didn’t test it. Is it possible to run the rewriteTester after completing the install routine?

  2. Yes, something like it. I’m not sure I understand your last question.

  3. Thomas, I stop monitoring this page for updates. Please contact me whenever you want.

    • tdeuling
    • January 9th, 2012

    Sorry, for the late answer.
    I just want to know, if there was a possibility to run the /install/ElggRewriteTester.php without coding an own script.
    But it all seems to work 🙂

    Thanks for your help.

  1. January 17th, 2012
    Trackback from : URL
  2. January 20th, 2012