Note that there are some explanatory texts on larger screens.

plurals
  1. POStrategies for dealing with URIs when building an application that sits behind a reverse proxy
    text
    copied!<p>I'm building an application with a self-contained HTTP server which can be either accessed directly, or put behind a reverse proxy (like Apache <code>mod_proxy</code>). </p> <p>So, let's say my application is running on port 8080 and you set up your Apache like this:</p> <pre><code>ProxyPass /myapp http://localhost:8080 ProxyPassReverse /myapp http://localhost:8080 </code></pre> <p>This will cause HTTP requests coming into the main Apache server that go to <code>/myapp/*</code> to be proxied to my application. If a request comes in like <code>GET /myapp/bar</code>, my application will see <code>GET /bar</code>. This is as it should be.</p> <p>The problem that arises is in generating URIs that have to be translated from my application's URI-space in order to work correctly via the proxy (i.e. prepending <code>/myapp/</code>). </p> <p>The <code>ProxyPassReverse</code> directive takes care of handling this for URIs in HTTP headers (redirects and so forth.) But that doesn't handle URIs in the HTML generated by my application, or in static files and templates. </p> <p>I'm aware of filters like <code>mod_proxy_html</code>, but this is a non-standard Apache module, and in any case, such filters may not be available for other front-end web servers which are capable of acting as a reverse proxy.</p> <p>So I've come up with a few possible strategies:</p> <ol> <li><p>Require an environment variable be set somewhere that contains the proxy path, and prepend this to all generated URIs. This seems inelegant; it breaks the encapsulation provided by the reverse proxy.</p></li> <li><p>Put the proxy path in a configuration file for my application. Same objection as above.</p></li> <li><p>Use only relative URIs in my application. This can get somewhat tricky; I would have to calculate the path difference between the current resource and where the link is going and add the appropriate number of <code>../</code>'es. Seems messy. Another problem is that some things must generate absolute URIs, like RSS feeds and generated emails.</p></li> <li><p>Use some hacky Javascript on the front-end to mungle URIs in the document text. This seems like a really horrible idea from an interoperability standpoint.</p></li> <li><p>Use a singe URI-generating function throughout my code, and require "static" files like Javascript, CSS, etc. to be run through my templating system. This is the idea I'm leaning towards now.</p></li> </ol> <p>This must be a fairly common problem. How have you approached it in the past? What has worked and what has made things more difficult? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload