Mathieu Hays

I recently moved my development environment from the built-in Apache configuration on my mac to a vagrant setup using ubuntu, apache and php7. It helps me preventing any disruption when doing the occasional Mac OS update (usually breaking my configuration).

I use custom domain names at work pointing to my computer. Everything is setup on a firewall so I can access my local websites from physical devices. Adding the following Apache rule allow me to keep these domain names pointing to my computer and then decide on which vagrant box I want the traffic to be redirected to.

Add the following to /etc/apache2/other/proxy-to-8080.conf. The filename is not important just make sure it finishes with .conf.

<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyPass / http://0.0.0.0:8080/
  ProxyPassReverse / http://0.0.0.0:8080/
</VirtualHost>

ProxyPreserverHost allows you to make the request seamless for the user and the webserver.

The first slash for both ProxyPass & ProxyPassReverse is the prefix you want to proxy. So in our case here it’s every requests. If you replace it with ProxyPass /proxy/ http://0.0.0.0:8080 for example it will only proxy requests for any domain which request that url and any sub-url. (ex: project.mathieu.office/proxy/will-be-proxied/, project.mathieu.office/wont-be-proxied/).

ProxyPass automatically appends the request URI accordingly.

In order for this to work make sure to enable the following modules in /etc/apache2/httpd.conf :

  • xml2enc_module
  • proxy_html_module
  • proxy_module
  • proxy_http_module

Then just restart Apache:

sudo apachectl restart

My use-case here is for pointing traffic to a Vagrant Box but if you want you can use that same method to route traffic to a node app running locally on port 3000 for example. I usually prefer when you don’t have weird ports all over the place.