Giter VIP home page Giter VIP logo

matomo-nginx's Introduction

Nginx Configuration for Matomo

This is a small nginx configuration that should help you get your own Matomo instance running and start collecting your own analytics.

I already know nginx

In this case it should be enough to just take the sites-available/matomo.conf, check if everything is configured as you like it and enable the config.

I want to get started

  • clone this repostitory or download it as a zip then move its content to /etc/nginx/ (or wherever you store your nginx-config)
  • read through the sites-available/matomo.conf and modify the settings to fit your use case:
    • set server_name to the domain(s) of your Matomo instance
    • set the path to your SSL certificate (I really recommend you to make sure your Matomo instance is only reachable via HTTPS. If you don't have an SSL certificate for your domain yet, check out Let's Encrypt.)
    • do you want to support old browsers? Then you'll need to modify ssl.conf according to your need. (the Mozilla SSL Config Generator will help you)
    • replace /var/www/matomo/ with the path to your Matomo instance
  • configure PHP (this depends on your OS and PHP setup)
    • if you are using fastcgi (which is probably the case) set fastcgi_pass to the path of your PHP socket file
    • you can also specify a TCP port
  • go to the sites-enabled folder of your nginx config directory
  • enable the Matomo config by creating a symlink: sudo ln -s ../sites-available/matomo.conf
  • test if there is a syntax error in your config: sudo nginx -t
  • restart nginx: sudo systemctl restart nginx

If you need to check the legacy nginx Matomo configuration, you can find it here: https://github.com/matomo-org/matomo-nginx/tree/1.0.99

Tips

  • never use Matomo without HTTPS
  • make sure you have configured Nginx to only accept modern and secure cryptography
  • add server_tokens off; to your config to disable the server: nginx header on all requests and the nginx version on error pages
  • if you have enabled gzip compression (which improves performance greatly), be aware of the BREACH vulnerability
  • think about enabling the Strict-Transport-Security header, but keep in mind the implications
  • keep HTTP/2 enabled as it brings performance benifits with many small files (e.g. icons)

You know how to improve this config? Open a pull request or GitHub issue!

matomo-nginx's People

Contributors

axelsimon avatar bbodenmiller avatar blueyed avatar findus23 avatar frost-nzcr4 avatar mattab avatar perusio avatar rixafy avatar sb-relaxt-at avatar simpaa avatar tsteur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

matomo-nginx's Issues

blacklist.conf not found

Thanks for this conf file - it has been very useful to me. You may want to include a blacklist.conf file in the sites-available directory, so that nginx does not give an error.

wrong socket location for php-cgi

the README file says that the default configuration uses php-cgi, and

Note that the default socket type is UNIX and the config assumes it to be listening on unix:/tmp/php-cgi/php-cgi.socket, if using the php-cgi

However, this file sets it to unix:/var/run/php-fpm.sock, which means that the default configuration doesn't work.

unable to login on fresh installation

I have a new installation of piwik on a nginx server, but im having a problem logging in with this error Error: Form security failed. Please reload the form and check that your cookies are enabled. If you use a proxy server, you must configure Piwik to accept the proxy header that forwards the Host header. Also, check that your Referer header is sent correctly. I tried on different browsers, cleared the cache,enabled cookies to no avail

I added these to my config.ini file:
[General]
assume_secure_protocol = 1
force_ssl=1
proxy_client_headers[] = HTTP_X_FORWARDED_FOR
proxy_client_headers[] = HTTP_CLIENT_IP

Please help

Configure script

Installing Piwik on nginx is simply a pain. A configure script where the user types in a domain name would really help. Especially one that knew what dependencies to install (Amazon EC2 needs PHP 5.5, nginx, mysql, etc...)

Missing snippets/fastcgi-php.conf

The comment state # if your Nginx setup doesn't come with a default fastcgi-php config replace this with the one from this repository. But there is not fastcgi-php.conf file in this repository.

Would be nice to provide one (even if it is super obvious what you should put into it, it is not for me).

Use of caching piwik.php

Looking at the proposed config, I'm wondering what the use of the piwik.php cache is? The piwik.php file is used for tracking, thus:

  • You do not want to cache any requests, since that would interfere with tracking
  • Every request carries a unique query string, so nothing is cached with the current config

So, why is the caching config there?

net::ERR_EMPTY_RESPONSE:The server closed the connection without sending any data."

I have install the piwik with nginx complete, and when I view renew the webpage , then the chrome console come with the error like net::ERR_EMPTY_RESPONSE:The server closed the connection without sending any data." after check the config for a while , I found the solution.
the question is nothing to do with the piwik, but the nginx config. From the offical nginx conf, I found a line like this

location ~* ^.+\.(?:jpg|png|css|gif|jpeg|js|swf)$ {
  # Defining the valid referers.
                  valid_referers none blocked *.piwik.com   othersite.com;   //change this line to deal question
                  if ($invalid_referer)  {
                          return 444;
                  }
                  expires max;
                  break;
         }

when we add tracker to the other site for tracking . the application will get the piwik.js from piwik application. and if this conf doesn't contain the refering url . the nginx will reject this request and return http code 444. my solution is add the valid url here . and the error disappear.

Default configuration makes dashboard unusable

EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "default-src 'self' http: https: data: blob: 'unsafe-inline'".

That's with the conf from this repo and no other changes, on a fresh install of Matomo :)

A CSP is required in some of the blocks:

add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval' https: data:; base-uri 'self'; frame-src 'self'; object-src 'self'";

Another issue I see is that manifest.json is also 403'd by default with the configuration in this repo.

Access to new php file is needed

We, the Piwik team, released a new feature yesterday called Heatmap & Session Recording. In order for it to automatically work, it needs to make a request to a file "plugins/HeatmapSessionRecording/configs.php" when tracking. This file will for example decide if it is needed to record a visitors session or not.

It would be great to adjust piwik-nginx to make that file accessible.

New maintainer?

Hi there,

It seems the project is not maintained anymore, although many people rely on it for Nginx Piwik configuration.

If someone is interested to maintain this repository under the official umbrella of Piwik we would love to collaborate. The goals are to merge & review pull requests, and ensure the project is maintained when changes are required to ensure compatibility with the latest Piwik, for example: #37

@perusio what do you think? Or maybe you'd still be keen to maintain the project?

Thanks all!

Matthieu
Piwik founder

not working with piwik 2.2

just upgraded to piwik 2.2 and this config seems not to work correctly anymore: the "All Websites" menu does not display any websites anymore...

is your nginx config compatible with piwik 2.2?

Getting Loadbalancer IP on Piwik dashboard.

Hi,

I m trying to move piwik from apache to Nginx using below mentioned code. but after shifting traffic to Nginx getting load-balancer internal ip on Piwik dashboard.

https://github.com/perusio/piwik-nginx

  • In piwik config.ini.php
    proxy_client_headers[] = "HTTP_X_FORWARDED_FOR"

I m not getting any clue to resolve this issue.

Please let me know if other information is required.

Shouldn't .md files be denied?

Shouldn't access to .md files be denied, considering the CHANGELOG.md file contains (possibly) sensitive version information?

Problems caching Piwik's index.php

Hi,

I am encountering a really weird problem. I am using nginx 1.0.10 with PHP 5.3.8 running as PHP FPM and using your config files for piwik (v1.6) with having changed the bare minimum (server_name and things like that but nothing much more).

Now the problem I get is that somehow the caching of index.php gets totally messed up: for example I logout of Piwik and login with a another user then I would see the stuff of the previous user. For me this is a real security issue. So somehow the new user which I login get the data cached from the previous user. Totally weird...

Now I noticed that if I uncomment the following two lines, as you can see below, in the "location = /index.php" in server{}:

    ## FastCGI cache.
    ## cache ui for 5m (set the same interval of your crontab)
   include sites-available/fcgi_cache.conf;
   fastcgi_cache_valid 5m;

I don't get this problem anymore. So it is pretty much related with caching... Now I still would like to be able to use caching but without this big problem.

Do you have any clues where this problem could come from? is it your config files? is it my piwik setup? or anything else?

Thanks in advance for your feedback.

nginx: [warn] and [emerg] | config failed

nginx: [warn] server name "$scheme://stats.example.com$request_uri" has suspicious symbols in /etc/nginx/sites-enabled/stats.example.com:15
nginx: [emerg] "expires" directive is duplicate in /etc/nginx/apps/piwik/proxy_piwik_cache.conf:30
nginx: configuration file /etc/nginx/nginx.conf test failed

*Note: I put stats.example back in for privacy reasons.

geoip

Hello Perusio,

does your config authorise the geoip location for Piwik or do I have to modify it a little bit ?

Don't have configuration errors by default

Some lines gives configuration errors, such as:

ssl_certificate /etc/letsencrypt/live/matomo.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/matomo.example.com/privkey.pem;

The reason is obvious .. the lines point to a non-existing file. The purpose of these lines is that they should be edited manually. I would like to ask that all lines that yield invalid configuration that need to be edited manually to be commented out or remove and place a description instead.

Right now i can not install the matomo nginx script and then run let's encrypt certbot because it will choke on these configuration errors. The two lines in the link in particularly don't need to be there because certbot will offer to add them (with nginx flag) if the conf file can be found.

I'm making an unattended install script and like as little manual intervention as possible.

Why override a lot of existing nginx files???

git clone https://github.com/perusio/piwik-nginx.git /etc/nginx

seems a bit crazy to advice to replace the current nginx.conf with the one from the repo just like that ...

yes ok you make a backup with nginx.old which should then be deleted later on??

Is there a configuration available that concentrate on just making piwik work and not mess around with all the fastcgi configuration and other not directly related piwik configs ??

Referer blocking piwik.js

One other item that you may want to consider: referer blocking js files by default. People who blindly install your Piwik nginx config (and do not set up the referer blocking domains correctly) may be confused when one of their sites does not work.

Seems like you should allow it by default, and then if someone wants to harden his install, to uncomment a line that has the added JS referer blocking.

Not too important, but may make it easier to use for newbies.

Do not cache tag manager preview files

To make sure always the newest version of a preview container in tag manager is served, we should prevent the caching in the browser. For the apache version see matomo-org/matomo#13977
Header set Cache-Control "Cache-Control: private, no-cache, no-store" for ^js/container_.*_preview\.js$

Otherwise the user will need to use ctrl+r or something to reload a page that includes a preview file to make sure the cache is ignored and many users would not know that.

php7.0-fpm

Is php7.2-fpm required for this to work? I'm using 7.0 and Matomo's PHP files don't get executed

site_available/fcgi_cache.conf use $no_cache but not defined

I want to use the microcaching system.

So I have include fcgi_cache.conf for my piwik.php (in my site conf).

And I can't start because of the $no_cache variable missing.

in fastcgi_cache_zone.conf, i have added this :
map $http_cookie $no_cache {
default 0;
~_mcnc 1;
}

It auto set no_cache depending on the presence of _mcnc in http_cookie. But this part is still not set.

If I take a look at this : http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n

I see the _mcnc is set by a way, I can try to copy that but I'm not sure is ok for piwik.

Could you take a look and may be you have already a solution on this. I would love have the full conf for piwik.

Thanks

comment refers to a missing file

A comment in the main config says:

# if your Nginx setup doesn't come with a default fastcgi-php config replace this with the one from this repository

There is no fastcgi-php config file in the repo though (anymore).

deny of .git/config

Required Private Directories | https://xxxxxxx/.git/config

We found that the above URLs are accessible via the browser, but they should NOT be. Allowing them to be accessed can pose a potential security risk since the contents can provide information about your server and potentially your users. Please restrict access to them.

The libs|vendor|plugins|misc|node_modules rule breaks Matomo

This rule:

location ~ ^/(libs|vendor|plugins|misc|node_modules) {
    deny all;
    return 403;
}

breaks my site completely. I have not narrowed it down yet because I don't want the site to be down while trying configs, but it seems like that rules denies JS files that are needed for Matomo to work.

topic/new_conf - give me your point

hi !
I have create a new topic, tell me your point on this.
I use a generic piwik conf, people should be able to not change it.

they is no default nor upstream anymore,

I have add a piwik_vars.conf, and set root / host and sock or proxy in this. So all the conf use it,

I have make piwik a default conf and auto enable it

the readme need to be adapted,

could you take a look and tell me what do you think on it ?

I can't use the vars to set the path of the fcgi root dir, too bad, but well everything else should be good.

I have check the host and www.hostname and made the same kind of error.

I can't use it to invalidate bad hostname, valid_referrer don't support vars.

all conf are relative now, and all conf are in the root dir of nginx, not in piwik subdir.

I create a piwik.conf in site-available and site-enable (as a symlink) because this conf is really made for this, and the name seems good like this.

give me your point, so we can merge it to master.

Fix helper files regex

piwik.conf
As far as I understand your helper files regex should be modified to something like this:

(.*) for any filename; Exclude .md files as well:
location ~* \.(?:bat|git|ini|sh|svn[^.]*|txt|tpl|xml)$
location ~* (.*)\.(?:bat|git|ini|sh|svn[^.]*|txt|tpl|xml|md)$

What do you think?

Matomo not working properly in Azure App Service with PHP 8 and Nginx

We are having problems running Matomo 4.6.2 in Azure App Service (Linux based) with PHP 8 and Nginx server. If we run Matomo from the App Service's /home/site/wwwroot/ directory the problems occurs. If we copy the Matomo files over to different directory (/var/www/html owned by the same user running the Nginx) it works just fine. /home directory should be used for apps deployed in App Service.

Is this some kind of permission issue? It is not possible to modify /home permission in Azure App Service. We used https://github.com/matomo-org/matomo-nginx as base config for our Nginx configuration.

Expected Behavior

Matomo works with the Azure App Service + PHP8 + Nginx combination as it is working with Azure App Service with PHP7 and Apache from the default App Service directory.

Current Behavior

Matomo loads very slow, resources (CSS, images) not getting loaded and nothing works. No errors in logs though.

Possible Solution

Temporary workaround is to run the Matomo from different directory (/var/www/html). In this case Matomo works just fine.

Steps to Reproduce (for Bugs)

  1. Set up an Azure Linux-based App Service with PHP8 and Nginx and deploy Matomo
  2. Use Nginx config from https://github.com/matomo-org/matomo-nginx

Context

Your Environment

  • Matomo Version: 4.6.2
  • PHP Version: 8.0.11
  • Server Operating System: Azure App Service (Linux)
  • Additionally installed plugins:
  • Browser:
  • Operating System:

piwik.js is blocked by valid_referers setting

I have 50+ sites that are connected to piwik instance, and it is not very convenient to add each of them to nginx referer validation. The only file that is needed to be excluded from this validation rule is "piwik.js"

Review new configs

I looked through your changes and have some additional hints:

Combine these two locations:

location ~ /(config|tmp|core|lang) {

location ~ /(libs|vendor|plugins|misc/user) {

Do not serve .ht files:

        location ~ /\.ht {
                deny  all;
        }

Serve .well-known:

        location ^~ /.well-known {
                allow all;
                default_type "text/plain";
        }

Make sure httpoxy is prohibited:


fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

                fastcgi_param HTTP_PROXY "";

You did not provide a nginx.conf, but there are some more important configs to consider. Are you sure, you do not want to provide one?

deny of /config/config.ini.php

Hello,

on my nginx 1.13.1 the deny of /config/config.ini.php does not work, i can access (download) it as octet/stream

Valid Referrer Question

I noticed that when I put the referrer as *.example.com (substituting my domain of course) visits are not getting tracked. However, if I put it as example.com visits are tracked but images are not loaded in piwik. I have piwik in a subdomain.

I disabled the referrer check and everything works great.

Suggestions?

Best practice for combing two of your configs with SSL

Obviously there's a lot of overlap between your various configs, so I've tried to take that into account. I've had a solid drupal + piwik config combination working previously, but after updating both Piwik and the Drupal site, enabling SSL with SPDY, the Piwik end 'dropped off' (I'm getting 404s at the moment, but also had bad gateway 502 errors with a few tweaks...).

I'm having trouble piecing the right config back together. I have a piwik.conf in sites-enabled, as per your example, which includes the apps/piwik/piwik.conf file also. The latter seems to have no effect on a TLS/SSL setup. I've done the following with the former (sites-enabled/piwik.conf):

server {
    listen 80;
    server_name piwik;
    return 301 https://www.domainname.com/piwik/$request_uri;
}

server {
    listen 443 ssl spdy;

    limit_conn arbeit 32;
    server_name piwik1;
    keepalive_timeout 75 75;

    ssl_certificate /etc/ssl/certs/acme.pem;
    ssl_certificate_key /etc/ssl/private/acme.key;

    ## Access and error log files.
    access_log /var/log/nginx/stats.domainname.com_access.log;
    error_log /var/log/nginx/stats.domainname.com_error.log;

    root /usr/share/nginx/www/piwik;
    index index.php;

    ## Include the piwik configuration.
    include apps/piwik/piwik.conf;
}

I'll admit having a weak grasp on server_names when used with sub-directories and, well, nginx in general at times.

In the apps/piwik/piwik.conf file I did try to adjust the location blocks to include /piwik before each location (ie. /piwik/favicon.ico etc.) but that didn't seem to do it either, esp. given that the root is given in the sites-enabled config.

Any idea where I'm going wrong, and what others have done to fix this?

Thanks in advance.

try_files duplicate

There is a try_files in the first defined location and one in location /. This prevents nginx from loading as it's complaining about duplicate try_files. How can I fix this?

$no_cache better setting and cache for ui

Hi, thanks for the over patch, but I have suggestion :

for piwik.php the no_cache should be always set to 0.
for ui, we can have another directory of cache, with all module except POST and Real TIME cache.
All setting should no be cache too.

I run the crontab (archive.sh) every 5 min, or anything,
when the archive run, we can delete the cache directory to force a refresh.
another way, is to set the time of cache equal to the frequency you run the archive.sh.

So 2 vars could be add :

time_of_cache
subdir_for_cache

for piwiki.php
no_cache=0
time_of_cache = 2h
subdir_for_cache = stats

for index.php
time_of_cache = 5m (or anything we set for the crontab)
subdir_for_cache = ui (may be autodelete by crontab, but if time_of_cache is set the same skip the delete)
no_cache = 1 by default
except : module for stats

or no_cache = 0 by default
except POST, configuration module

I will setup this, and purpose a patch

Denying /plugins results in missing images/icons in the GUI

The current suggestion is to render a 403 forbidden response to requests for /plugins/..., with the following rule (see https://github.com/matomo-org/matomo-nginx/blob/5b232af8ec1fd9d033f1c4ab9343f4073df64644/sites-available/matomo.conf#L80C1-L83C6):

    location ~ ^/(libs|vendor|plugins|misc|node_modules) {
        deny all;
        return 403;
    }

This results in e.g. the Matomo Logo on top left corner of the Matomo GUI to be unavailable: https://your.webserver.tld/plugins/Morpheus/images/logo.svg?matomo

Suggested solution: add a more specific location matcher so that requests to images are still possible, but not neccessarily everything unter /plugins/ is reachable.

piwik apps doesn't work

I have this setting :

http://stats.example.com/

so, it try this :

http://stats.example.com/?account...

and you conf say to redirect to index.php (without the query string)

here the fix :

instead of this :
## Try all locations and relay to index.php as a fallback.
location / {
try_files $uri /index.php;
}

we need this :

## Try all locations and relay to index.php as a fallback.
location / {
    try_files $uri /index.php?$query_string;
}

Warnings about sensitive files being exposed during Matomo setup still there after using this repository

While running through the current 4.11.0 setup and using the files in the repository, I still get

PHP FPM will ignore .htaccess rules for .php files. To ensure that sensitive files cannot be accessed directly it is recommended to exclude certain directories from being handled by PHP FPM. For more information please see the official nginx server configuration

To ensure that sensitive files cannot be accessed directly it is recommended to configure your web server to restrict access to certain directories. For more information please see the official nginx server configuration

in PHP SAPI and Server info.

Isn't the solution to these errors to utilize the files in this repository?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.