如何通过 nginx Ubuntu 上的子页面访问 matomo

问题描述

我使用的是配置了 Nginx 的 Ubuntu 18.04.5 LTS 网络服务器。服务器正在运行,我可以访问我的主页 example.com 上的任何文件。现在我想在服务器上安装 matomo。但是,我只能设法通过我的根 url www.example.com 访问安装。每当我尝试将 matomo 访问移动到子页面时,例如example.com/matomo/ 我的服务器改为发送 404。我想我在创建用于调用页面的配置设置时犯了一个错误,但我无法弄清楚出了什么问题。我是 Nginx 的新手,过去 2 天一直在测试解决方案。任何帮助将不胜感激。请找到我的 server.conf,以及下面的 matomo.conf。

我的服务器配置文件如下:

server {
    listen                  443 ssl http2;
    listen                  [::]:443 ssl http2;
    server_name             www.example.com;
    set                     $base /var/www/example.com;
    root                    $base/public;

    # SSL
    ssl_certificate         /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    # security
    include                 Nginxconfig.io/security.conf;

    # logging
    access_log              /var/log/Nginx/example.com.access.log;
    error_log               /var/log/Nginx/example.com.error.log warn;

    # index.PHP
    index                   index.PHP;

    # index.PHP fallback
    location / {
        try_files $uri $uri/ /index.PHP?$query_string;
    }

    # handle .PHP
    location ~ \.PHP$ {
        fastcgi_pass unix:/var/run/PHP/PHP7.2-fpm.sock;
        include      Nginxconfig.io/PHP_fastcgi.conf;
    }
} 

我的 matomo.conf:

server {
    listen 443 ssl http2;
    server_name www.example.com/subpage example.com/subpage; 
    access_log /var/log/Nginx/matomo.access.log;
    error_log /var/log/Nginx/matomo.error.log;


    ## SSL
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

    add_header Referrer-Policy origin always; 
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    root /var/www/example.com/subpage/matomo/; # path to matomo instance

    index index.PHP;

    ## only allow accessing the following PHP files
    location ~ ^/(index|matomo|piwik|js/index|plugins/HeatmapSessionRecording/configs)\.PHP {
        include snippets/fastcgi-PHP.conf;
        fastcgi_param HTTP_PROXY ""; 
        fastcgi_pass unix:/var/run/PHP/PHP7.2-fpm.sock; 
    }

    ## deny access to all other .PHP files
    location ~* ^.+\.PHP$ {
        deny all;
        return 403;
    }

    ## serve all other files normally
    location / {
        #try_files $uri $uri/ =404;
    }

    ## disable all access to the following directories
    location ~ ^/(config|tmp|core|lang) {
        deny all;
        return 403; # replace with 404 to not show these directories exist
    }

    location ~ /\.ht {
        deny  all;
        return 403;
    }

    location ~ js/container_.*_preview\.js$ {
        expires off;
        add_header Cache-Control 'private,no-cache,no-store';
    }

    location ~ \.(gif|ico|jpg|png|svg|js|css|htm|html|mp3|mp4|wav|ogg|avi|ttf|eot|woff|woff2|json)$ {
        allow all;
        ## Cache images,CSS,JS and webfonts for an hour
        ## Increasing the duration may improve the load-time,but may cause old files to show after an matomo upgrade
        expires 1h;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

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

    ## properly display textfiles in root directory
    location ~/(.*\.md|LEgalNOTICE|LICENSE) {
        default_type text/plain;
    }
}
# vim: filetype=Nginx

解决方法

感谢您的快速回复。

在进一步寻找解决方案后,我现在已经完成了这两项工作:我创建了一个子域,并通过将以下代码添加到 subdomain.config 来在子页面下启动 matomo。

location /matomo {    
root /var/www/subdomain.example.com/matomo;
}

我发现 this 是创建子域的绝佳教程。