Hosting Piwik in a non-root location

Hello. I’m using Piwik 2.4.0 from a subsite, e.g. example.com/piwik

It works well, except that there is no explicitly defined base HREF, so every resource with a relative path expects the target is in the root of the domain, e.g. example.com/plugins/Morpheus/

How can I most efficiently declare a base HREF for everything?

I’ve discovered urlnormalizer.js and I’ve been playing around with that, but to no avail. It seems like this would work, but perhaps it isn’t called to normalize URLs to resources?

I’ll keep digging, but any insight would be greatly appreciated.

I ended up solving this by thinking my nginx configuration through a bit more. I’m in the process of cleaning up the conf files and then I’ll post them in full, but the short answer is to setup Piwik as a server on e.g. localhost:9000, create a location directive with proxy_pass information, and (critically) include rewrites at the base location / to for the URL to physical path. E.g.:


rewrite /analytics(.*/piwik$1;)

Another rewrite must be written at the proxy location (/analytics in my example) to redirect resource requests to the root site (as the real resource needed will be at e.g. localhost:9000/plugins/…, not localhost:9000/analytics/plugins/…). That rewrite takes the form of:


rewrite /analytics/(.*) /$1 break;

or so.

In case some future miserable sod comes across this, I should note I abandoned this venture. There’s just too many hard-coded locations that expect Piwik to be at the root of the domain. I had a maze of symlinks to hold together calls to plugins and libs in support of my bootstrap (which kept such files out of the web root). I also had a large stack of rewrites to handle actions such as logging in.

Perhaps sometime when I can get my other, more broken issues resolved I’ll give it another go.

@tohuw we have not heard this issue before. Piwik works fine for us when it’s installed at the root of the domain or when it’s installed in a sub-directory. maybe it is nginx config issue related to your config?

Thanks for the reply Matt. I can give it another go. I’ll post the config here.

Do I need to do anything special in the config.ini.php (beyond ensuring the site URL setting is example.org/subdir) or bootstrap when hosting from a subdirectory, or is all the magic done in Nginx?

I got it sorted, thanks to the fine folks in #nginx on Freenode. The real key is:


 location ~ ^/subdirectory/.*\.php {
    alias /physical/path/to/piwik/$1;
    ...
  }
  location /subdirectory/ {
    alias /physical/path/to/piwik/;
  }

I was doing very silly things with proxies that were totally unnecessary. I’m still getting the hang of Nginx, so this was valuable learning time.