Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

by Jesus Vte. Vila -
Number of replies: 6
Hi, I've installed x-sendfile on apache following the official docs and our site it's not working correctly.

The guide is 
5.6 X-Sendfile

We have done:

1) Install apache x-modfile using "apt install libapache2-mod-xsendfile"

2) Enable module and check if enabled

a2enmod xsendfile

apachectl -M | grep xsendfile

 xsendfile_module (shared)

3) Added to Moodle config.php

$CFG->xsendfile = 'X-Sendfile';
$CFG->xsendfilealiases = array(
    '/dataroot/' => $CFG->dataroot,
    '/cachedir/' => '/var/www/moodledata/cache',
    '/localcachedir/' => '/var/www/moodledata/localcache',
    '/tempdir/' => '/var/www/moodledata/temp',
   '/filedir' => '/var/www/moodledata/filedir',
);

Also we have tried remove all the folders except /dataroot/

NOTE: $CFG->dataroot is $CFG->dataroot = '/var/www/moodledata';

3) Added to apache site config 

XSendFile On
XSendFilePath /var/www/html/moodle/ <--- this based on this post and no luck Permalink
XSendFilePath /var/www/moodledata/

Full config is:

<IfModule mod_ssl.c>
  <VirtualHost *:443>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
  ServerName site.notworking.com
  SSLCertificateFile /etc/letsencrypt/live/site.notworking.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/site.notworking.com/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
  XSendFile On
  XSendFilePath /var/www/html/moodle/
  XSendFilePath /var/www/moodledata/
</VirtualHost>
</IfModule>

Moodle is in site.notworking.com/moodle, there is a redirection on site.notworking.com/index.html -> /moodle

4) Clear Moodle cache and restart apache

The result is server can't find files and images and scripts are not working.

Apache error shows lines like

[Sat Nov 07 22:19:17.415787 2020] [:error] [pid 3308:tid 140610607892224] (20023)The given path was above the root path: [client xxxx:57163] xsendfile: unable to find file: /dataroot/localcache/theme/1604782453/adaptable/pix/core/i/navigationitem.svg, referer: https://site.notworking.com/moodle/my/

/dataroot/localcache/theme/1604782453/adaptable/pix/core/i/navigationitem.svg (replace dataroot by /var/www/moodledata) and file exists and with right permissions.

ls -l /var/www/moodledata/localcache/theme/1604782453/adaptable/pix/core/i/navigationitem.svg

-rw-rw-rw- 1 www-data www-data 396 nov  7 21:54 

/var/www/moodledata/localcache/theme/1604782453/adaptable/pix/core/i/navigationitem.svg

What can we do?

we have tried paths involved ended with /, not ended, ... 


Average of ratings: -
In reply to Jesus Vte. Vila

Re: Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

by Mateus Mercer -
Hi!

I solved this error by fixing the definition of the NGINX alias:

location ~ ^/dataroot/(.*)$ {
    # The definition of 'internal' here is CRITICAL as it prevents client access to your dataroot.
    internal;
    alias <full_moodledata_path>/$1; # ensure the path ends with '/$1'
}

The 404 errors where fixed this way.
Average of ratings: Useful (1)
In reply to Mateus Mercer

Re: Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Hi Mateus,

This doesn't make sense to me. Firstly this is replying to an Apache question with an Nginx fix. Secondly the fix contradicts the Nginx docs which say:

location /protected/ {
  internal;
  alias   /some/path/; # note the trailing slash
}

I've reverted your change to the Moodle Docs because I think it is incorrect, the change summary cites this issue (Apache not Nginx), and another issue with no fix. If you disagree with what I've done, please provide more information, such as why the proposed change to the Moodle Docs' Nginx page outweighs the Nginx docs.

In reply to Leon Stringer

Re: Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

by Mateus Mercer -
Hi Leon!

Sorry for the confusion, I was fixing this problem and replied to the wrong thread. This one is apache, the other one is nginx.

The current sendfiles documentation configuration for nginx doesn't works for my moodle instance, I had to configure as I sent above.

The fix is actually based from this: https://serverfault.com/a/376161

When I use the documentation configuration, the nginx returns errors that it couldn't find the file. Once I configure the way I sent it here, it started working.

So I don't think my fix is invalid, since it is currently working in production.

If the configuration from documentation works in other moodle instances, but not on mine, then we can start analyzing the version numbers and environment configuration. Because this is something that needs to be documented if possible.
In reply to Mateus Mercer

Re: Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Perhaps you could edit the Moodle Docs page and add that under the existing configuration, for example: "If the above configuration does not work try …" with the alternate location {} block with the ServerFault link as reference?

I'm using the location {} block from the original/current Moodle Docs page. For comparison I've attached my Nginx configuration and relevant parts of config.php. Moodledata is in /var/moodledata/moodle.example.com and I have Nginx 1.14.1 on CentOS 8.

Jesus: Apologies for posting on your thread.

In reply to Leon Stringer

Re: Ubuntu 16.04 + Apache + X-SendFile not working (no images and scripts)

by Jesus Vte. Vila -
Don't worry. All info is welcome!!