Monthly Archives: December 2008

squid.

So I’ve been messing around with Squid recently. One of the features I had planned to add was the ability to fall back to Coral Cache if the web site that Squid is trying to cache goes down.

So I wrote this little redirector script in PHP:

Stuff this in /etc/squid/coralcache.redirect and make it executable (chmod a+rwx /etc/squid/coralcache.redirect):

#!/usr/bin/php
<?
        /* Coral Cache on 404/No Server */

        $t=array();
        while($input=fgets(STDIN)){
                ob_start();
                $t=split(' ',$input);
                $url = $t[0];
                $ch = curl_init($url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);
                curl_setopt($ch,CURLOPT_VERBOSE,false);
                $ci = @curl_exec($ch);
                $status = @curl_getinfo($ch);
                ob_end_clean();
                if($status["http_code"] > 400){
                        $uriparts = explode('/',$url);
                        $uriparts[2] = rtrim($uriparts[2]) . ".nyud.net";
                        echo "302:" . implode('/',$uriparts) . "n";
                }
        }

Then, at the top of your squid.conf;

redirect_program /etc/squid/coralcache.redirect

Restart squid, and all unavailable pages should now redirect to Coral Cache. =)