This guide will describe how to set up a reverse proxy in Apache that can redirect traffic to a backend of our choice. For this guide we will be using Vagrant as our development environment. This guide will cover topics such as:
- How to set up an Apache server using Vagrant
- How to configure reverse proxy for Apache
First step is to set up an Apache server using Vagrant. More on how to use Vagrant can be found in the docs.
Download and install Vagrant from their website.
Configure a Vagrant environment. The environment code, configuration files and content will be stored and shared with the guest environment using a vagrantfolder.
$ mkdir reverse_proxy_example
$ cd reverse_proxy_example
$ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
Add a box to the environment.
$ vagrant box add ubuntu/trusty64
This will install a box from a remote server. It will promt us asking which provider we wish to use, select virtualbox. Open up the vagrant configuration file [vagrantfile] and edit the following.
config.vm.box = "ubuntu/trusty64"
Now bring up our guest machine.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: reverse_proxy_example_default_1486176868029_16297
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
default: /vagrant => /reverse_proxy_example
Connect to the box using SSH.
$ vagrant ssh
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-108-generic x86_64)
[...]
vagrant@trusty-64:~$
Set up the Apache server. Create a folder /vagrant/www/html
to use as a root-dir for apache. Create a file bootstrap.sh
in the vagrantfolder with the following content.
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant/www /var/www
fi
and edit the vagrantfile with the following line.
config.vm.provision :shell, path: "bootstrap.sh"
If the guest machine is destroyed run the initial vagrant up again, otherwise run the reload command with the provision-flag.
$ vagrant reload --provision
Now the guest machine is up and running an Apache webserver. Check this by connecting to the machine using ssh.
vagrant@trusty-64:~$ service apache2 status
Apache2 is running (pid 1630).
To test the server run
vagrant@trusty-64:~$ wget -qO- 127.0.0.1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of /</title>
[...]
<address>Apache/2.4.7 (Ubuntu) Server at 127.0.0.1 Port 80</address>
</body></html>
We have successfully installed Apache v. 2.4.7 and it is up and running. To check the current version of Apache installed.
vagrant@trusty-64:~$ apache2ctl -V | grep version
Server version: Apache/2.4.7 (Ubuntu)
To access the server on the guest machine from the host machine we add port-forwarding to our vagrantfile.
config.vm.network :forwarded_port, guest: 80, host: 4567
Run vagrant reload again to restart the environment with the new changes. Afterwards , open a browser and navigate to 127.0.0.1:4567 which should reveal that our guest environment setup is complete with a running Apache server. Now we can serve static files from the folder /vagrant/www/html.
Configure Reverse Proxy for the Apache server. We want to avoid any editing of the original httpd.conf-file and will resort to making extensions run by bash-scripts and .conf-files. We start off by creating a .conf-file in our vagrantfolder naming it 001-mysite.conf.
listen 5000
<VirtualHost *:5000>
ProxyPass "/proxy" "http://127.0.0.1:8080/"
ProxyPassReverse "/proxy" "http://127.0.0.1:8080/"
</VirtualHost>
This creates an Apache server on port 5000 which redirects all traffic on *:5000/proxy to 127.0.0.1:8080 where we have our backend server running. In our bootstrap.sh-file we add a few lines enabling our configuration-file and the modules related to proxying in Apache.
[...]
cp /vagrant/001-mysite.conf /etc/apache2/sites-available/
a2ensite 001-mysite.conf
a2enmod proxy
a2enmod proxy_http
service apache2 restart
We are also going to run a simple HTTP server as our backend so we add this to the bootstrap.sh-file.
python -m SimpleHTTPServer 8080 &> /dev/null &
We also need to add a port-forwarding rule to our vagrantfile in order to access port 5000 from our host machine.
config.vm.network :forwarded_port, guest: 5000, host: 4568
Now we recreate our vagrant environment or run vagrant reload --provision and access 127.0.0.1:4568/proxy to see the result of our work. The python SimpleHTTPServer should reveal the contents of the home directory on the guest machine.
Note: Accessing any other path on the *:5000 VirtualHost will continue to work as a normal Apache server!
We have managed to provision a local virtual machine running an Apache server with proxy-capabilities to a backend of our choice. We have demonstrated the use of Vagrant, its configuration-file and its provisioning methods.