We're struggling with the following problem and trying to understand what's the best way to deal with this. The general goal is such:

--> example.com/app1         -->  app1.com/
--> example.com/app1/images  -->  app1.com/images

--> example.com/app2         -->  app2.com/
--> example.com/app2/images  -->  app2.com/images

--> example.com/app3         -->  app3.com/
--> example.com/app3/images  -->  app3.com/images

We've considered the following solutions, some of them work and others don't. We're trying to figure out what's the best way to do this because we're by no means experts. The tool we've been using so far is Nginx to do the reverse proxy, but the tool doesn't really matter, we're open to anything.

Solution 1

Nginx rewrites the body text to add "app1" for app1 requests, etc. As in the example above, there's a request for app1 and an index.html comes back. That index.html in turn has some tag that looks like <img src="images/hello.png" /> file. This obviously won't work, so we rewrite the body (i.e. the index.html) to say <img src="app1/images/hello.png" />.

Problem with Solution 1

We have to be really good about how we craft the body rewrite rules otherwise we could rewrite something we didn't intend and then break some JS or HTML. This solution also doesn't work for some of the JS code (we're using Angular) which doesn't implicitly write out the location of the views and is rather put together by the framework itself.

Solution 2

Move every app from the root to it's own folder. This means any img or other request will update to <img src="app1/images/hello.png" />.

Problem with Solution 2

We have to change all the existing 3 websites to request resources from their respective new roots.

Any help would be greatly appreciated, thanks in advance.

  • Ideally avoid rewriting the document in the proxy -- if you can redirect example.com/app1 --> example.com/app1/ before applying the reverse proxy, your relative urls would work. – Capilé Feb 13 '14 at 22:18

Ideally you can redeploy your applications such that each application is installed in http://app<n>.com/app<n> instead of the document root.

If not maybe you can have the links made relative, instead of linking to /images/logo.png link to ../images/logo.png or images/logo.png or ../../images/logo.png depending on the page that's served, which will translate correctly in a Reverse Proxy situation.

Obviously using subdomains for each application will work well enough, reverse proxy everything on app<n>.example.com/ to your internal app<n>.com/

Normally a reverse proxy will not rewrite the contents of the response body it receives from the server the request was forwarded to, but apache will allow you to do so:

<Location /app1/>
   ProxyPass http://app1.com/
   ProxyPassReverse http://app1.com/ 
   # rewrite the reponse body  
   AddOutputFilterByType SUBSTITUTE text/html
   Substitute "s|/images/|/app1/images/|i" 
  • Thanks, we figured if we hadn't missing anything that the best way forward would be to make everything a relative link or have it's own context root. Thanks. – Arthur Maltson Feb 19 '14 at 2:41

You can use nginx as a reverse proxy for the example.com domain.

So on the conf file for the nginx site for example.com you can set a rewrite sentence to accomplish what you are looking for.

You can read this guide: http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html & https://www.digitalocean.com/community/articles/how-to-configure-nginx-as-a-front-end-proxy-for-apache

Hope this works for you.


  • We are currently using it as a reverse proxy, and it works for the initial request. The issue arises with subsequent requests for resources that the files assume are on the context root but are actually one level down. We don't know if a request for images/hello.png is from app1, app2 or app3. – Arthur Maltson Feb 13 '14 at 16:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.