Saturday, November 17, 2018

Apache Reverse Proxy for Microservice Subdomain

You've got Apache 2 running. What more does a plain-old simple HTML site need, after all?

Now you want to add a microservice, but you don't want to add more hosting. Can you add it to this existing host? Yes you can!

Here's how to add a subdomain - tictactoe.example.com - that will send requests to your microservice that's listening on port 8888.

Put the following in /etc/apache2/sites-available/tictactoe.com.conf

<VirtualHost *:80>
    ServerName tictactoe.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ProxyPass "/"  "http://example.com:8888/"
    ProxyPassReverse "/"  "http://example.com:8888/"

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


Then add your site:

sudo a2ensite tictactoe.com

The best part is your Go code (or Rust, Ruby, Python, whatever) can just handle HTTP. You can use Let's Encrypt/certbot to add the HTTPS virtual host that will proxy secure traffic to your microservice as well.

UPDATE: Nothing like getting an invalid command error trying to do this on an Apache without proxy enabled. So...

sudo a2enmod proxy_http

No comments: