I often want to mount a node.js single-page web application into a subdirectory of an Apache-hosted website, and the reverse-proxy feature of the Apache module mod_proxy is perfect for this. Getting the forward-slashes at the end of the URL correct is important for successfully loading assets, so this post provides a short summary of how to do so.
tl;dr: snippet at the end of this post
If my application references its CSS and JS using relative paths, e.g.
<script src="assets/main.css">, the following ProxyPass rule in the Apache config appears to work correctly:
ProxyPass /someapp http://localhost:23456
This works fine if you request
example.com/someapp/, because the browser will translate a script source of
assets/main.css into an HTTP request for
http://example.com/someapp (note the lack of trailing-slash) will mean the CSS request goes to
http://example.com/assets/main.css, which is outside the directory of the proxied app. To ensure that the application is only visited with the trailing slash present, requests can be redirected to the application using the following mod_rewrite rule (assuming you've already turned
RewriteRule ^/someapp$ someapp/ [L,R=301]
These rules combine to form the following snippet that I use when mounting a node.js web application under a subdirectory on my website:
RewriteRule ^/someapp$ someapp/ [L,R=301] ProxyPass /someapp http://localhost:23456 ProxyPassReverse /someapp http://localhost:23456