Using Apache httpd to redirect context path?

7,373

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/).

Share:
7,373

Related videos on Youtube

Robert Seal
Author by

Robert Seal

Email: cowwoc at bbs dot darktech dot org.

Updated on September 18, 2022

Comments

  • Robert Seal
    Robert Seal almost 2 years

    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?

  • Robert Seal
    Robert Seal almost 11 years
    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? :)
  • user2751502
    user2751502 almost 11 years
    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 can help with this sort of thing, but it wouldn't really be appropriate for JSON.
  • Robert Seal
    Robert Seal almost 11 years
    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!