Rails Behind Apache with a Reverse Proxy
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:
Redirect permanent / https://www.n8ta.com/ # Forward to https
</VirtualHost> # and a second for ssl,
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/
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.