1

I've got the following config for a VirtualHost:

<VirtualHost *:80>
    DocumentRoot /srv/emptywebroot
    <Directory /srv/emptywebroot>
        Require all denied
    </Directory>

    # Dashboard proxy
    RewriteEngine on
    LogLevel alert rewrite:trace6
    RewriteCond %{HTTP:Upgrade} =websocket
    RewriteRule /dashboard/(.*) ws://dashboard:3838/dashboard/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket
    RewriteRule /dashboard/(.*) http://dashboard:3838/dashboard/$1 [P,L]
    ProxyPreserveHost on
    ProxyPassReverse /dashboard http://dashboard:3838/dashboard
</VirtualHost>

When I visit http://myurl/dashboard I get a 404 from the dashboard backend.

When I inspect rewrite logs I get:

[Wed Mar 21 14:04:08.051070 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] init rewrite engine with requested uri /dashboard/
[Wed Mar 21 14:04:08.051114 2018] [rewrite:trace1] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] pass through /dashboard/
[Wed Mar 21 14:04:08.051176 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] add path info postfix: /srv/emptywebroot/dashboard -> /srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051186 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] applying pattern '(.*)' to uri '/srv/emptywebroot/dashboard/'
[Wed Mar 21 14:04:08.051199 2018] [rewrite:trace4] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] RewriteCond: input='' pattern='=websocket' => not-matched
[Wed Mar 21 14:04:08.051208 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] add path info postfix: /srv/emptywebroot/dashboard -> /srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051216 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] applying pattern '(.*)' to uri '/srv/emptywebroot/dashboard/'
[Wed Mar 21 14:04:08.051225 2018] [rewrite:trace4] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] RewriteCond: input='' pattern='!=websocket' => matched
[Wed Mar 21 14:04:08.051234 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] rewrite '/srv/emptywebroot/dashboard/' -> 'http://dashboard:3838//srv/emptywebroot/dashboard/'
[Wed Mar 21 14:04:08.051243 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] escaped URI in per-dir context for proxy, http://dashboard:3838//srv/emptywebroot/dashboard/ -> http://dashboard:3838//srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051264 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] forcing proxy-throughput with http://dashboard:3838//srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051272 2018] [rewrite:trace1] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] go-ahead with proxy request proxy:http://dashboard:3838//srv/emptywebroot/dashboard/ [OK]

So /srv/emptywebroot gets prefixed before passing the URI to the proxy. I would like this not to be prefixed. I've tried RewriteBase / and some other things, but I don't get the point.

How can I avoid DocumentRoot to be prefixed to URI in this context?

EDIT 1: Dashboard backend at dashboard:3838 is not served by apache, it's a shiny server (https://shiny.rstudio.com/). I've run a tcpdump at dashboard's host, and what happens is what's seen above: DocumentRoot gets prefixed by apache's proxy virtualhost, so the backend is asked for http://dashboard:3838//srv/emptywebroot/dashboard, instead of http://dashboard:3838/dashboard as i need. I hope this clarifies my situation, so reformulating the question: How do i instruct mod_rewrite not to resolve local paths (prefixing DocumentRoot)? In my case this is a mod_rewrite proxy (check the [P] at the rewrite rule, so prefixing local paths is not my desidered behaviour.

2
  • Do you have an .htaccess file in the /dashboard/ directory? Or <Directory> container that controls this same directory? Those log entries don't look as if they match the directives you've posted above in your VirtualHost? "I've tried RewriteBase /" - If you'd tried RewriteBase in your VirtualHost container (above) it would have broken your server! RewriteBase only applies in a directory (or .htaccess) context.
    – MrWhite
    Mar 22, 2018 at 1:34
  • Edited original question Mar 22, 2018 at 7:49

2 Answers 2

1

According to the Rewrite Log these lines of your Ruleset have been hit

RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /dashboard/(.*) http://dashboard:3838/dashboard/$1 [P,L]

Since the RewriteRule has a [L] Flag the next lines will not be checked anymore

Now this Request will enter the next Rewrite Round on <VirtualHost dashboard:3838> You might look there to find why your URI /dashboard/ results in a HTTP Error 404.

  • Does this directory exist?
  • Is there any RewriteRule that can resolve it?
1
  • I've edited my original question, as i think it was not understood... My problem is with DocumentRoot being prefixed (local path resolution) while mod_rewrite proxy ([P]) is in use. Mar 22, 2018 at 7:48
1

I've found that forcing the rewriterule to match against REQUEST_URI, by adding a rewrite condition on REQUEST_URI before, solves the issue, so the following works for me:

<VirtualHost *:80>
    DocumentRoot /srv/emptywebroot
    <Directory /srv/emptywebroot>
        Require all denied
    </Directory>

    # Dashboard proxy
    RewriteEngine on
    LogLevel alert rewrite:trace6
    RewriteCond %{HTTP:Upgrade} =websocket
    RewriteCond %{REQUEST_URI} ^/dashboard
    RewriteRule /dashboard/(.*) ws://dashboard:3838/dashboard/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket
    RewriteCond %{REQUEST_URI} ^/dashboard
    RewriteRule /dashboard/(.*) http://dashboard:3838/dashboard/$1 [P,L]
    ProxyPreserveHost on
    ProxyPassReverse /dashboard http://dashboard:3838/dashboard
</VirtualHost>

I've found the documentation at http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule somewhat confusing, as it states "In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.", but in my case it seems to match against the local resolved path instead. So adding a RewriteCond to modify what the RewriteRule rewrites (REQUEST_URI in my case) seems to work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .