Jeff Mair - Blog

Apache Reverse Proxy with optional trailing forward slash

Jeff Mair

tl;dr: the Redirect directive is a simple solution to appending a trailing slash.

I spent a fair bit of time messing around with Apache (HTTPD) config
trying to accomplish what I thought was a simple task (and it turned out to be).
That is: be able to host a node application behind an apache reverse proxy, and be
able to access the app at https://mysite.com/app/ OR https://mysite.com/app

(no trailing slash). I ended up chasing a red herring for long enough to give me
a giant headache. Essentially it turns out you should read at least some of the
documentation before you start editing configuration.

What I did was, I started digging into the apache config file (on my Ubuntu Digital

Ocean droplet, at /etc/apache2/apache2.conf) and I thought I needed to do something with
the “rewrite” module in apache. This sounded familiar from my WordPress days, so
that was where I focused. That and variations in the reverse proxy configuration.
So, my attempts went as follows:

# Fragment of apache config:

ProxyPass /weather/ http://127.0.0.1:4000/
ProxyPassReverse /weather/ http://127.0.0.1:4000/ 

The result of this configuration is that hitting mysite.com/weather/ correctly returns the
response from my node app running on the server at http://127.0.0.1:4000/. However, hitting
mysite.com/weather returns a 404 page. So I began trying things like:

RewriteRule ^/weather /weather/ 

Or:

RewriteRule ^/weather mysite.com/weather/ 

Etc… And I fought and fought with various versions of this from what I found with
google searches. But nothing worked. Setting up another ProxyPass /weather
with no trailing slash partially worked, but when you hit that url with no trailing slash,
the static resources were not resolved properly.

Finally, finally… I was reading on my phone in bed at the end of the day and I
found the “alias” module with the Redirect directive. Bingo, this was exactly
what I needed. Some of the existing directories in my site (not in a reverse-proxy,
just directories on disk) already worked properly and automatically added the trailing
slash. I didn’t realize it was doing this with a redirect and I couldn’t find any evidence
of configuration of that in the apache config file. Turns out there is a default directive
in the “mod_dir” module named DirectorySlash which automatically redirects
to a path with a trailing slash if you visit a url that resolves to a directory on disk AND
you fail to provide the trailing slash. That behavior was leading me to think that there
was some “RewriteRule” in play and I needed to employ that in the case of my reverse proxy.
Not the case though. Here’s the full solution:

 # Fragment of apache config:

ProxyPass /weather/ http://127.0.0.1:4000/
ProxyPassReverse /weather/ http://127.0.0.1:4000/
Redirect /weather /weather/ 

This of course requires both the Rewrite and Alias modules to be enabled.

Edit:The rewrite may work also; I was placing the rewrite rules in the wrong part of the configuration.
It seems that they need to go inside a Directory context in my config file. The docs aren’t entirely intuitive.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top