WHMCS Rewrite Rules for Nginx

nginx_logo
WHMCS is a very popular client management package with a slew of billing, support, and product management options. We use it here and have just recently moved our WHMCS site over to an nginx powered web server. The rewrites have all been worked out and since there’s nothing to be found on the subject via google we decided to share. :)

server {
            listen   80;
            server_name  my.uncorrupted.net;
            rewrite ^/(.*) https://my.uncorrupted.net/$1 permanent;
       }

server {
            listen   443;
            server_name my.uncorrupted.net;
            access_log /xxx/xxx/xxx/logs/access.log;
            error_log /xxx/xxx/xxx/logs/error.log;
	    	ssl on;
	    	ssl_certificate /xxx/xxx/xxx/.ssl/cert.pem;
	    	ssl_certificate_key /xxx/xxx/xxx/.ssl/key.pem;

            location /  {
                        root   /xxx/xxx/xxx/public_html/;
                        index  index.html index.php;
		    }

			location = /announcements {
        				rewrite ^  /announcements.php  last;
    		}

    		location /announcements/ {
        				rewrite  ^/announcements/([0-9]+)/[A-Za-z0-9_-]+\.html$ /announcements.php?id=$1 last;
    		}

    		location = /downloads {
        				rewrite ^  /downloads.php  last;
    		}

    		location /downloads/ {
        				rewrite ^/downloads/([0-9]+)/([^/]*)$ /downloads.php?action=displaycat&catid=$1 last;
    		}
			location = /knowledgebase {
       					rewrite ^  /knowledgebase.php  last;
    		}
			location /knowledgebase/ {
        				rewrite  ^/knowledgebase/([0-9]+)/[A-Za-z0-9_-]+\.html$ /knowledgebase.php?action=displayarticle&id=$1 last;
        				rewrite  ^/knowledgebase/([0-9]+)/([^/]*)$ /knowledgebase.php?action=displaycat&catid=$1 last;
    		}

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            location ~ \.php$ {
            			fastcgi_pass 127.0.0.1:9000;
            			fastcgi_index index.php;
            			include /usr/local/nginx/conf/fastcgi_params;
            			fastcgi_param SCRIPT_FILENAME /xxx/xxx/xxx/public_html/$fastcgi_script_name;
            }
}

Gest of luck – if you run into any issues write us a comment and we’ll see if we can help you work it out.

Tags: , ,

3 Comments

Leave a comment
  1. Niels 26. Oct, 2009 at 8:14 am #

    Hi, thanks for sharing, saves me some time. Could you also share your fastcgi_params? Mine may be missing something.

    E.g. my WHMCS links to ?a=view where it should link to cart.php?a-view, etc.

  2. Michael.Terence 26. Oct, 2009 at 8:27 am #

    Certainly,

    They’re the standard params included in: /usr/local/nginx/conf/fastcgi_params

    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;

    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_param SERVER_PROTOCOL $server_protocol;

    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;

    # PHP only, required if PHP was built with –enable-force-cgi-redirect
    fastcgi_param REDIRECT_STATUS 200;

  3. Niels 26. Oct, 2009 at 12:25 pm #

    Thanks. Unfortunately that’s what I have. Time to dig a little deeper…

Leave a Reply

Spam Protection by WP-SpamFree