Update on nginx and Piwik

In my previous post I summoned some hackish magic to fix something that I didn’t really understand. That’s usually not good practice. Most experienced developers have a well-tuned b.s. detection system and will return to unnecessarily hackish code almost as a matter of necessity. That was the case here, and what I found was that the error was in my nginx conf file. For whatever reason I had nginx misconfigured and content JavaScript, CSS, and SWF files were being served up by fast-cgi instead of being handled by nginx directly. Here’s a relevant section of my updated conf file:

# Stop Image and Document Hijacking
# Also have to handle these file types for piwik before they get processed by PHP below
location ~* (\.jpg|\.png|\.css|\.gif|\.jpeg|\.js|\.swf)$ {

   if ($http_referer ~* ^(http://my.domain.com) ) {
      expires max;

   return 444;


location / {

   root   /home/public_html/my.domain.com/public/;
   index  index.php index.html;

   # this serves static files that exist without running other rewrite tests
   if (-f $request_filename) {
      expires max;

   fastcgi_index index.php;
   include /usr/local/nginx/conf/fastcgi_params;
   fastcgi_param SCRIPT_FILENAME /home/public_html/my.domain.com/public/$fastcgi_script_name;
   include fastcgi_params;

Everything got better after that. Not sure who I thought I was fooling with my first solution, but it didn’t even work on myself.

Be Sociable, Share!

2 Comments so far

  1. Dave on August 27th, 2009
  2. vdibart on August 27th, 2009

    Thanks for the tip.

Leave a Reply