Rails Behind Apache with a Reverse Proxy

April 16, 2019

rails logo Rails apache logo Apache

I suspect many people don't have this problem but I ran into it so I thought I'd document it. In order to minimize my AWS bill I wanted to run a website built in rails (this one!) on the same AWS instance that was already running apache. So if you need to do the same here's what you do:

Run your rails app on a port other than port 80, anything works even if it's not exposed to the public. The traffic will flow like this:


User -> Port (80) -> Apache -> Port (xyz) --> Rails

First you'll need to install the necessary apache modules



a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html

Then, add a virtual host in your /etc/apache2/sites-enabled/something-something.conf file or wherever you specify your virtual hosts.
It should look like this:

<VirtualHost *:80>
ProxyPreserve
Host On
Redirect permanent / https://www.n8ta.com/ # Forward to https
ServerName site.com
ServerAlias www.site.com
</VirtualHost> # and a second for ssl,
<VirtualHost *:443>
ProxyPreserveHost On
ProxyPass / http://0.0.0.0:3000/ # apache -> rails connection is not secured as it's local to the machien
ProxyPassReverse / http://0.0.0.0:3000/
ServerName site.com
ServerAlias www.site.com
SSLEngine on
SSLCertificateFile /path/to/a.crt
SSLCertificateKeyFile /path/to/a.key
</VirtualHost>


Restart apache with


service apache2 restart

or whatever tool you use to manage services.

And you're golden, you now have a rails app and an apache server running simultaneously.