Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apache Reverse Proxy Support #647

Closed
anonymous-matomo-user opened this issue Apr 9, 2009 · 8 comments
Closed

Apache Reverse Proxy Support #647

anonymous-matomo-user opened this issue Apr 9, 2009 · 8 comments
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. wontfix If you can reproduce this issue, please reopen the issue or create a new one describing it.

Comments

@anonymous-matomo-user
Copy link

If I install Piwik behind a reverse proxy, I get some problems with the images and javascript URLs created by the tool. Instead of http://piwik.my-domain.com/… (public URL) I get http://piwik.local_network/ (internal URL) generated in front end.
The problem occures with Piwik versions 0.2.32 and 0.2.33.

For configuring reverse proxy I use apache mod_proxy module and this command in VHost configuration on public accessable host:

```
ProxyPass / http://piwik.local_network/
ProxyPassReverse / http://piwik.local_network/
```

and using own VHost on internal server.

Well, the cause is: with this type of installation the`
$SERVER[‘HTTP_HOST’]`variable contains ’piwik.localnetwork’ and not the real host name, which should be like ‘piwik.my-domain.com’.

The (proposed and working) solution is:
1. change lines 114-ff. in core/Url.php as following:

instead of

```
if(isset($SERVER[‘HTTP_HOST’]))
{
$url .= $_SERVER[’HTTP
HOST’];
}
else
{
$url .= ‘unknown’;
}
```

use this one:

```
if(isset($SERVER[‘HTTP_X_FORWARDED_HOST’]))
{
$url .= $SERVER[‘HTTP_X_FORWARDED_HOST’];
}
elseif(isset($SERVER[’HTTPHOST’]))
{
$url .= $
SERVER[’HTTP
HOST’];
}
else
{
$url .= ‘unknown’;
}
```
1. Replace line 181-ff. in plugins/Login/controller.php as following:

after

```
$piwikHost = $SERVER[’HTTPHOST’];
```

add

```
if(isset($SERVER[‘HTTP_X_FORWARDED_HOST’]))
{
$piwikHost = $SERVER[’HTTP_XFORWARDED
HOST’];
}
```

That’s all.

PS:
Patched files are attached.

@anonymous-matomo-user
Copy link
Author

Attachment:
[patched_files.zip](http://issues.piwik.org/attachments/647/patched_files.zip)

@robocoder
Copy link
Contributor

Duplicates #466.

Thanks for the patch, however, it is the responsibility of the reverse proxy to make it transparent for the web site/web application because Piwik doesn’t know if it’s behind one or more reverse proxies, or none at all. For Apache, I suggest using mod_proxy_html (which works now without code changes). Java System Web Proxy Server also takes the URL remapping approach (i.e., content URL rewriting).
- X-Forwarded-Host is a non-standard header, albeit possibly a defacto standard. Other proxies may use something else, e.g., lighttpd uses X-Host. Moreover, if there are multiple hosts in X-Forwarded-Host, we must “assume” that subsequent proxy hosts are appended to the list, the last being the closest hop to the server.
- However, this doesn’t handle the issue raised in #466 where the reverse proxy also maps https to http; X-Forwarded-Ssl, X-Forwarded-Scheme, and X-Forwarded-Proto are also non-standard headers, and may not even be present.
- Umm…if both X-Host and X-Forwarded-Host are present, which do we rely on?

@mattab
Copy link
Member

mattab commented Apr 13, 2009

vipsoft I agree with you that this seems like a bug in the reverse proxy configuration.

However if there is a quick fix as the one provided might be worth considering?

My question would be: how other softwares like Wordpress or Drupal deal with this problem? javalexG, can you please quickly research and let us know? thanks

@anonymous-matomo-user
Copy link
Author

Thank you for your answer, vipsoft!
I cant’t find the possibility to reconfigure apache reverse proxy to pass proper HTTP_Host environment variable to the backend server, but I’m still looking for the solution…

Anyway, my patch described above is based on solutions for other PHP-based tools installed behind a reverse proxy (sorry, I can’t find links to them anymore). Another (smart) solution is provided by Typo3 (v4.2) and is described here (with a link to bugtracker in a red box): http://www.henningpingel.de/TYPO3-Backend-Via-SSL-Proxy.124.0.html?&L=0

@mattab
Copy link
Member

mattab commented Apr 15, 2009

can you please find a link for your source (either wordpress, or typo3, drupal…) and a patch? we would like to add this support but want to get it right which means copy what other softwares did :)

@anonymous-matomo-user
Copy link
Author

There's a simple solution: let the admin set the host or better an URL. I use Apache Roller in a reverse proxy situation. In Roller you can set an absolute URL for your application in the configuration dialogue, which replaces every automatic determined hostname/URL like the above mentioned (HTTP_HOST). If there's no absolute URL set, Roller uses HTTP_HOST.

@anonymous-matomo-user
Copy link
Author

Replying to rafaelo:
PS: Every link on a page has to be prefixed with the URL from the configuration. The advantage: you don't have to twiddle with the HTTP-server configuration to rewrite the HTML-code of the pages. You only need your reverse proxy configuration.

@robocoder
Copy link
Contributor

(In [1155]) fixes #691 - use relative URLs (except in Widgetize iframe & flash embed
code); refs #466 and #647 - should also resolve the reverse proxy issues

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. wontfix If you can reproduce this issue, please reopen the issue or create a new one describing it.
Projects
None yet
Development

No branches or pull requests

3 participants