matomo-org/docker

Configure Nginx to serve multiple apps including Matomo

choonchernlim opened this issue · 3 comments

Hello,

I need a little guidance on how to set up Nginx to interact with FastCGI.

I have read the example configs and many other posted issue, but a minor difference is my server that serves multiple apps instead of just Matomo.

So, if my server link is https://myserver.com, then:

So far, while I have other apps working fine, I can't figure out how to get Matomo to work with my Nginx configuration, which I'm sure it is incorrect.

Here's my docker-compose.yml:

version: '2'

services:
  jenkins:
    image: "jenkins/jenkins:lts"
    // ... some config
	
  portainer:
    image: "portainer/portainer"
    // ... some config

  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: always
    volumes:
     - mariadb:/var/lib/mysql
    environment:
     - "MYSQL_ROOT_PASSWORD=somepassword"

  matomo:
    image: matomo:fpm
    container_name: matomo
    restart: always
    links:
     - mariadb:db
    volumes:
     - matomo_config:/var/www/html/config:rw
     - matomo_logs:/var/www/html/logs

  nginx:
    build: ./nginx
    container_name: nginx
    restart: always
    ports:
     - "80:80"
     - "443:443"
    volumes:
     - /etc/localtime:/etc/localtime
    links:
     - jenkins
     - portainer
     - matomo
    depends_on:
     - jenkins
     - portainer
     - matomo
    networks:
      default:
	aliases:
         - "myserver.com"

networks:
  default:

volumes:
  // ... some config

Here's my nginx.conf:

http {
    server {
	listen 80;
        return 301 https://$host$request_uri;
    }

    server {
	listen 443 ssl;

        ssl_certificate         /etc/nginx/cert.crt;
        ssl_certificate_key     /etc/nginx/cert.key;

        location ~ ^/matomo/.+\.php$  {
		fastcgi_param   QUERY_STRING            $query_string;
		fastcgi_param   REQUEST_METHOD          $request_method;
		fastcgi_param   CONTENT_TYPE            $content_type;
		fastcgi_param   CONTENT_LENGTH          $content_length;

		fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
		fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
		fastcgi_param   PATH_INFO               $fastcgi_path_info;
		fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_path_info;
		fastcgi_param   REQUEST_URI             $request_uri;
		fastcgi_param   DOCUMENT_URI            $document_uri;
		fastcgi_param   DOCUMENT_ROOT           $document_root;
		fastcgi_param   SERVER_PROTOCOL         $server_protocol;

		fastcgi_param   GATEWAY_INTERFACE		CGI/1.1;
		fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

		fastcgi_param   REMOTE_ADDR             $remote_addr;
		fastcgi_param   REMOTE_PORT             $remote_port;
		fastcgi_param   SERVER_ADDR             $server_addr;
		fastcgi_param   SERVER_PORT             $server_port;
		fastcgi_param   SERVER_NAME             $server_name;

		fastcgi_param   HTTPS                   $https;

		fastcgi_pass                      	matomo:9000;
        }

	location /jenkins/ {
		proxy_pass          http://jenkins:8080/jenkins/;
	}

        location /portainer/ {
		proxy_pass          http://portainer:9000/;
		proxy_set_header    Connection          "";
       }
}

When running the containers, I see the following in the console log:-

matomo       | [26-Feb-2019 21:21:12] NOTICE: fpm is running, pid 1
matomo       | [26-Feb-2019 21:21:12] NOTICE: ready to handle connections

When I tried to access https://myserver.com/matomo to set up Matomo, I'm getting 404.

How do tweak my Nginx config to allow https://myserver.com/matomo to work?

Thank you very much.

@choonchernlim did you find a solution to your problem? Is it by any chance documented anywhere?

You could take a look at the nginx config:
https://github.com/matomo-org/matomo-nginx

@caillou Here's the simplified configuration to get it working...

docker-compose.yml

The most important part here is setting up volumes_from: in Nginx. Without this config, Nginx.conf won't know where matomo's /var/www/html/ is.

services:
  matomo:
    image: matomo:fpm
    container_name: matomo
    links:
      - mariadb:db
    volumes:
      - matomo:/var/www/html

  nginx:
    build: ./nginx
    container_name: nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/localtime:/etc/localtime
    # `volumes_from` is required to expose  `/var/www/html` volume from Matomo to get static files to work
    volumes_from:
      - matomo
    links:
      - matomo
    depends_on:
      - matomo

nginx.conf

Simplified config of getting /matomo working...

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name myserver.com;

    ssl_certificate         /etc/nginx/cert.crt;
    ssl_certificate_key     /etc/nginx/cert.key;

    index index.php index.htm index.html;

    location = /matomo/index.php {
        include             fastcgi_params;
        fastcgi_param       SCRIPT_FILENAME     /var/www/html/index.php;
        fastcgi_pass        matomo:9000;
    }

    location = /matomo/piwik.php {
        include             fastcgi_params;
        fastcgi_param       SCRIPT_FILENAME     /var/www/html/piwik.php;
        fastcgi_pass        matomo:9000;
    }

    location = /matomo/matomo.php {
        include             fastcgi_params;
        fastcgi_param       SCRIPT_FILENAME     /var/www/html/matomo.php;
        fastcgi_pass        matomo:9000;
    }

    # Matomo: serving static files (don't put trailing slash on it)
    location /matomo {
        alias               /var/www/html/;
        try_files           $uri $uri/ =404;
    }
}

fastcgi_params

This file is included by nginx.conf.

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

Hope this helps.