Thursday, January 29, 2009

Debugging Apache rewrite rules using a local environment

On a production site, I got an error when accessing Zope/Plone through Apache. Without Apache in front, it worked just fine. When reading Zope's instance log, it showed IndexError: string index out of range. Probably something to do with a rewrite rule. Apache rewrite rules with the VirtualHostMonster can be a mystery.

A colleague showed me how to set up a local environment for testing. This allows us to test a rewrite rule without risking downtime on the production environment.

Edit: See the Rewrite rule witch for a quick start with rewrite rules.

Modify your hosts file

  • Add test-rewrite to your /etc/hosts file

Set up apache

  • Install Apache locally, if you hadn't already. :)

  • Add an Apache config file test to /etc/apache2/sites-available, which looks like
    <VirtualHost *>

    ServerName test-rewrite

    RewriteEngine On

    # The rewrite rule under scrutiny
    # Assumes your Zope is running on port 8080
    RewriteRule ^/(.*)$1 [P,L,NC]

    RewriteLog /var/log/apache2/rewrite.log
    RewriteLogLevel 9

    CustomLog /var/log/apache2/test-rewriterule.log combined


  • Enable modules proxy, rewrite, proxy-http (using a2enmod)

  • In proxy.conf, set "Allow from all"

  • Enable the new config: a2ensite test

  • Modify /etc/apache2/sites-available/default so it begins with
    NameVirtualHost *
    <VirtualHost *>

Test it!

  • Run apache2ctl -t and apache2ctl -S

  • Run a tail -f on /var/log/apache2/rewrite.log

  • Tinker on your rewrite rule, and restart apache (/etc/init.d/apache2 restart) after modifications.

  • Go to http://test-rewrite and watch the log to see what happens

No comments: