Search code examples
authenticationurl-rewritingmatomonginx-reverse-proxy

Configuration for Piwik behind nginx reverse proxy with rewrite


I'm using Piwik behind an Nginx reverse proxy and Piwik is running on an Apache Server. I also use a rewrite rule ( /piwik/ to / ).

For Chrome and Safari on Mac the login process for Piwik isn't working (I only got the general error message to configure browser cookies and proxy server).

But my current configuration is working in FirefoxDeveloperEdition for Mac:

nginx.conf:

location /piwik {
     rewrite             ^/piwik/(.*)$ /$1  break;
     proxy_pass          http://piwik;
     proxy_set_header    Host $host;
     proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header    X-Forwarded-Host $http_host/piwik;
}

config.ini.php

[General]
proxy_client_headers[] = HTTP_X_FORWARDED_FOR
proxy_host_headers[] = HTTP_X_FORWARDED_HOST

When I remove the /piwik in nginx.conf to:

    proxy_set_header    X-Forwarded-Host $http_host;

The login is working but I got 2 other problems:

  • after login I got a wrong redirect to the root / (not Piwik anymore), but after reopening Piwik, I'm logged in
  • the logo is missing because of the wrong url http://localhost:2020/plugins/Morpheus/images/logo.svg instead of http://localhost:2020/piwik/plugins/Morpheus/images/logo.svg

I also would keep the rewrite rule, because the Apache Server is a universal docker container.

Probably I have to analyse the failing authentification condition, but I didn't find the correct line yet.


Solution

  • I have created a pull request to enable and consider a new header info for proxy environment.

    nginx.conf (inform about missing path)

    rewrite             ^/piwik/(.*)$ /$1  break;
    ...
    proxy_set_header    X-Forwarded-Host $http_host;
    proxy_set_header    X-Forwarded-Uri /piwik;
    

    Enable header in config.ini.php

    proxy_uri_header = 1
    

    This option inserts the missing path to the current script name and redirects. See pull request for more details https://github.com/piwik/piwik/pull/12011