3

I'd like to proxy http://localhost:1006/ as http://example.com/public/ to the outside world. Here is my configuration:

RewriteEngine On
# Append a slash if necessary
RewriteRule ^/public$ public/ [R,L]

# Request headers: Replace http://localhost:1006/ with http://example.com/public/
ProxyPass /public/ http://localhost:1006/

# Response headers: Replace http://example.com/ with http://example.com/public/
ProxyPassReverse /public/ http://example.com/

ProxyPassReverseCookiePath / /public/
<Location /public>
    Require all granted
</Location>

This works well for rewriting headers, but the server still thinks its context path is / instead of /public/ so when it constructs URLs for embedding into JSON they are incorrect. Httpd only rewrites headers, not the JSON, so it does nothing about this.

I don't want httpd to rewrite the JSON (I read the process is unreliable). I noticed that the Host header sent by httpd to the server contains the external hostname, which enables the server to correct that part of the URL. Is there some other header I could set which would instruct the server to use a different context-path?

Meaning, is there anything I can do on httpd which would alter the server's context-path without altering the server's configuration/code directly?

Gili
  • 295
  • 1
  • 4
  • 20

2 Answers2

2

Short answer: no, not really. Typically, your backend application needs to have an option for configuring its own root URL so that it can generate appropriate links. If your backend application is hosted in something like Tomcat, then just install it such that it's available at the same relative path (so that, for example, you're proxying from example.com/public/ to localhost:1006/public/).

larsks
  • 43,623
  • 14
  • 121
  • 180
  • I suspected as much, but I'll ask just in case: Are you absolutely sure there is no way? Or are you saying it's not worth the effort? :) – Gili Jul 31 '13 at 15:29
  • It really depends on your application. If it's something common and you can tell us what it is, maybe there are application-specific fixes. Otherwise, short of some sort content filter, I think you're out of luck. For HTML pages, [mod_proxy_html](https://httpd.apache.org/docs/2.4/en/mod/mod_proxy_html.html) can help with this sort of thing, but it wouldn't really be appropriate for `JSON`. – larsks Jul 31 '13 at 15:30
  • I can modify the application (running Jetty HTTP server) to take a context-path. I was just hoping there was a way to do this without altering the application code but that's alright. Thanks! – Gili Jul 31 '13 at 15:43
0

You could take a stab at reverse rewriting the output but it is much, much easier to let the back-end app think it lives in /public as you will be playing wack a mole with content rewrites for your forseeable future if you go the rewrite path.

Wyatt Barnett
  • 725
  • 5
  • 14