Search code examples
apachereverse-proxymattermost

Bad redirection of Mattermost docker behind reverse proxy apache2


I am trying to install the docker version of Mattermost on my Ubuntu 14.04 with apache2 version 2.4.7.

Here is the configuration of mattermost-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerName www.my.website.fr
        ServerAlias website.fr
        DocumentRoot /home/www/www-website
  [...]
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPreserveHost On
    ProxyRequests Off
    RewriteEngine  on

    ProxyPass /mattermost http://localhost:8083
    ProxyPassReverse /mattermost http://localhost:8083

    RewriteCond %{REQUEST_URI} /api/v[0-9]+/(users/)?websocket [NC,OR]
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    RewriteRule .* ws://127.0.0.1:8083%{REQUEST_URI} [P,QSA,L]

    <Location /home/www/www-soft/mattermost>
      Require all granted
      ProxyPass http://127.0.0.1:8083/
      ProxyPassReverse http://127.0.0.1:8083/
    </Location>
[...]
</VirtualHost>
</IfModule>

The website is accessible but nothing appears on the page, because I got such error messages (one example among several others of the same form):
Failed to load resource: the server responded with a status of 404 (Not Found) https://www.my.website.fr/static/main.9e27a2872d73987ea8ec.css

Instead of trying to access the file at:

https://www.my.website.fr/mattermost/static/main.9e27a2872d73987ea8ec.css (which exists, I tested)

(I replaced my website domain by "my.website.fr")

Is there any obvious reason why the apache proxy is not redirecting correctly with the /mattermost prefix ? Am I missing something ?

The dedicated page in Mattermost documentation didn't help me much resolving this (https://docs.mattermost.com/install/config-apache2.html)


Solution

  • From apache documentation: https://httpd.apache.org/docs/2.4/mod/core.html#location

    The directive limits the scope of the enclosed directives by URL.

    Which means you need to change:

    <Location /home/www/www-soft/mattermost>
      Require all granted
      ProxyPass http://127.0.0.1:8083/
      ProxyPassReverse http://127.0.0.1:8083/
    </Location>
    

    to

    <Location /mattermost>
      Require all granted
      ProxyPass http://127.0.0.1:8083/
      ProxyPassReverse http://127.0.0.1:8083/
    </Location>
    

    Because your document root is set to

    DocumentRoot /home/www/www-website
    

    You're also using proxypass to handle requests to the same path

    ProxyPass /mattermost http://localhost:8083
    ProxyPassReverse /mattermost http://localhost:8083
    

    You need to chose which solution you want to implement: both might not work well together.

    At last, you're also using

    ProxyPreserveHost On
    

    which I'm not sure it's needed/working as you think: please refer to https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypreservehost to find out if it's realy required in your environment.