<commit_before># Using Nginx as a reserve proxy/load balancer with Weave and Docker #

## What you will build ##

Weave allows you to focus on developing your application, rather than your infrastructure.

In this example we will demonstrate how Weave allows you to quickly and easily deploy Nginx as 
a load balancer for a simple php application running in containers on multiple nodes, with no 
modifications to the application and minimal docker knowledge. 

You will also be introduced to [WeaveDNS](https://github.com/zettio/weave/tree/master/weavedns#readme),
which provides a simple way for containers to find each other using hostnames and requires no code
changes.
 
## What you will use ##

* [Weave](http://weave.works)
* [Docker](http://docker.com)
* [Nginx](http://Nginx.org)
* [Ubuntu](http://ubuntu.com)

## What you will need to complete this guide ##

This getting started guide is self contained. You will use Weave, Docker, Nginx and Ubuntu, and we make use of VirtualBox and Vagrant to allow you to run this entire getting started guide on your personal system.

* 20 minutes
* [Git](http://git-scm.com/downloads)
* [VirtualBox > 4.3.20](https://www.virtualbox.org/wiki/Downloads)
* [Vagrant > 1.6](https://docs.vagrantup.com/v2/installation/index.html)

## Setting up our hosts ##

All of the code for this example is available on github, and you first clone the getting started repository.

```
git clone http://github.com/fintanr/weave-gs
```

You will use Vagrant to setup and configure three Ubuntu hosts and install Docker. We make use of Vagrant's functionality to download the base docker images we will be using, and we then install Weave. If you would like to work through the installation steps please review our [getting started guide](https://github.com/fintanr/weave-gs/blob/master/ubuntu-simple/README.md) for a more manual example.
   
```
cd weave-gs/nginx-ubuntu-simple
vagrant up
```

Vagrant will pull down and configure an Ubuntu image, this may take a few minutes depending on the speed of your network connection. For more details on Vagrant please refer to the [Vagrant documentation](http://vagrantup.com).

You may be prompted for a password when `/etc/hosts` is being updated during the Vagrant setup, please just hit return at this point.

Once the setup of the hosts is complete you can check their status with

```
vagrant status
```

The IP addresses we use for this demo are

```
172.17.8.101 	weave-gs-01
172.17.8.102 	weave-gs-02
172.17.8.103 	weave-gs-03
```

Our Vagrantfile also configures weave-gs-01 to pass traffic from port 80 to localhost port 8080.

## Introducing WeaveDNS ##

[WeaveDNS](https://github.com/zettio/weave/tree/master/weavedns#readme) answers name queries in a Weave network. WeaveDNS provides a simple way for containers to find each other: just give them hostnames and tell other containers to connect to those names. Unlike Docker 'links', this requires no code changes and works across hosts.

In this example you will give each container a hostname and use WeaveDNS to allow Nginx to find the correct container for a request.

## Nginx and a simple PHP application running in Apache ##

Nginx is a popular free, open-source, high-performance HTTP server and reverse proxy. It is frequently used as a load balancer. In this 
example we will use Nginx to load balance requests to a set of containers running Apache. 

While our example application is very simple, as a php application running on apache2 it resembles many real world applications 
in use. We have created an Nginx configuration which will round-robin across the websevers our php application is running on.
The details of the Nginx configuration are out of scope for this article, but you can review it at [on github](https://github.com/fintanr/weave-gs/blob/master/nginx-ubuntu-simple/example/nginx.conf)  

We have created two docker containers specifically for this guide, if you would like further details on how these were created refer to 
the Dockerfile section at the end of this guide. 

## Starting our example ##

To start the example run the script `launch-nginx-demo.sh`. This will 

* launch Weave and WeaveDNS on each host 
* launch six containers across our three hosts running an apache2 instance with our simple php site
* launch Nginx as a load balancer in front of the six containers  

```
./launch-nginx-demo.sh
```

If you would like to execute these steps manually the commands to launch Weave and WeaveDNS are

```
vagrant ssh weave-gs-01
sudo weave launch
sudo weave launch-dns 10.2.1.1/24

vagrant ssh weave-gs-02
sudo weave launch 172.17.8.101
sudo weave launch-dns 10.2.1.2/24 

vagrant ssh weave-gs-03
sudo weave launch 172.17.8.101 
sudo weave launch-dns 10.2.1.3/24
```

The commands to launch our application containers are 

```
vagrant ssh weave-gs-01
sudo weave run --with-dns 10.3.1.1/24 -h ws1.weave.local fintanr/weave-gs-nginx-apache
sudo weave run --with-dns 10.3.1.2/24 -h ws2.weave.local fintanr/weave-gs-nginx-apache

vagrant ssh weave-gs-02
sudo weave run --with-dns 10.3.1.3/24 -h ws3.weave.local fintanr/weave-gs-nginx-apache
sudo weave run --with-dns 10.3.1.4/24 -h ws4.weave.local fintanr/weave-gs-nginx-apache

vagrant ssh weave-gs-03
sudo weave run --with-dns 10.3.1.5/24 -h ws5.weave.local fintanr/weave-gs-nginx-apache
sudo weave run --with-dns 10.3.1.6/24 -h ws6.weave.local fintanr/weave-gs-nginx-apache
```

Note the --with-dns option and the -h option, these tell Weave to register this container with WeaveDNS using the hostname provided.
When you use --with-dns and -h you must include .weave.local in the -h option. 

Finally we launch our Nginx container
```
vagrant ssh weave-gs-01
sudo weave run --with-dns 10.3.1.7/24 -ti -h nginx.weave.local -d -p 80:80 fintanr/weave-gs-nginx-simple 
```
 
### What has happened? ###

At this point you have launched Weave and WeaveDNS on all of your hosts, and connected six containers running our simple php
application and Nginx together using Weave. 
<commit_msg>Clarify --with-dns and -h options to weave<commit_after># Using Nginx as a reserve proxy/load balancer with Weave and Docker #

## What you will build ##

Weave allows you to focus on developing your application, rather than your infrastructure.

In this example we will demonstrate how Weave allows you to quickly and easily deploy Nginx as 
a load balancer for a simple php application running in containers on multiple nodes, with no 
modifications to the application and minimal docker knowledge. 

You will also be introduced to [WeaveDNS](https://github.com/zettio/weave/tree/master/weavedns#readme),
which provides a simple way for containers to find each other using hostnames and requires no code
changes.
 
## What you will use ##

* [Weave](http://weave.works)
* [Docker](http://docker.com)
* [Nginx](http://Nginx.org)
* [Ubuntu](http://ubuntu.com)

## What you will need to complete this guide ##

This getting started guide is self contained. You will use Weave, Docker, Nginx and Ubuntu, and we make use of VirtualBox and Vagrant to allow you to run this entire getting started guide on your personal system.

* 20 minutes
* [Git](http://git-scm.com/downloads)
* [VirtualBox > 4.3.20](https://www.virtualbox.org/wiki/Downloads)
* [Vagrant > 1.6](https://docs.vagrantup.com/v2/installation/index.html)

## Setting up our hosts ##

All of the code for this example is available on github, and you first clone the getting started repository.

```
git clone http://github.com/fintanr/weave-gs
```

You will use Vagrant to setup and configure three Ubuntu hosts and install Docker. We make use of Vagrant's functionality to download the base docker images we will be using, and we then install Weave. If you would like to work through the installation steps please review our [getting started guide](https://github.com/fintanr/weave-gs/blob/master/ubuntu-simple/README.md) for a more manual example.
   
```
cd weave-gs/nginx-ubuntu-simple
vagrant up
```

Vagrant will pull down and configure an Ubuntu image, this may take a few minutes depending on the speed of your network connection. For more details on Vagrant please refer to the [Vagrant documentation](http://vagrantup.com).

You may be prompted for a password when `/etc/hosts` is being updated during the Vagrant setup, please just hit return at this point.

Once the setup of the hosts is complete you can check their status with

```
vagrant status
```

The IP addresses we use for this demo are

```
172.17.8.101 	weave-gs-01
172.17.8.102 	weave-gs-02
172.17.8.103 	weave-gs-03
```

Our Vagrantfile also configures weave-gs-01 to pass traffic from port 80 to localhost port 8080.

## Introducing WeaveDNS ##

[WeaveDNS](https://github.com/zettio/weave/tree/master/weavedns#readme) answers name queries in a Weave network. WeaveDNS provides a simple way for containers to find each other: just give them hostnames and tell other containers to connect to those names. Unlike Docker 'links', this requires no code changes and works across hosts.

In this example you will give each container a hostname and use WeaveDNS to allow Nginx to find the correct container for a request.

## Nginx and a simple PHP application running in Apache ##

Nginx is a popular free, open-source, high-performance HTTP server and reverse proxy. It is frequently used as a load balancer. In this 
example we will use Nginx to load balance requests to a set of containers running Apache. 

While our example application is very simple, as a php application running on apache2 it resembles many real world applications 
in use. We have created an Nginx configuration which will round-robin across the websevers our php application is running on.
The details of the Nginx configuration are out of scope for this article, but you can review it at [on github](https://github.com/fintanr/weave-gs/blob/master/nginx-ubuntu-simple/example/nginx.conf)  

We have created two docker containers specifically for this guide, if you would like further details on how these were created refer to 
the Dockerfile section at the end of this guide. 

## Starting our example ##

To start the example run the script `launch-nginx-demo.sh`. This will 

* launch Weave and WeaveDNS on each host 
* launch six containers across our three hosts running an apache2 instance with our simple php site
* launch Nginx as a load balancer in front of the six containers  

```
./launch-nginx-demo.sh
```

If you would like to execute these steps manually the commands to launch Weave and WeaveDNS are

```
vagrant ssh weave-gs-01
sudo weave launch
sudo weave launch-dns 10.2.1.1/24

vagrant ssh weave-gs-02
sudo weave launch 172.17.8.101
sudo weave launch-dns 10.2.1.2/24 

vagrant ssh weave-gs-03
sudo weave launch 172.17.8.101 
sudo weave launch-dns 10.2.1.3/24
```

The commands to launch our application containers are 

```
vagrant ssh weave-gs-01
sudo weave run --with-dns 10.3.1.1/24 -h ws1.weave.local fintanr/weave-gs-nginx-apache
sudo weave run --with-dns 10.3.1.2/24 -h ws2.weave.local fintanr/weave-gs-nginx-apache

vagrant ssh weave-gs-02
sudo weave run --with-dns 10.3.1.3/24 -h ws3.weave.local fintanr/weave-gs-nginx-apache
sudo weave run --with-dns 10.3.1.4/24 -h ws4.weave.local fintanr/weave-gs-nginx-apache

vagrant ssh weave-gs-03
sudo weave run --with-dns 10.3.1.5/24 -h ws5.weave.local fintanr/weave-gs-nginx-apache
sudo weave run --with-dns 10.3.1.6/24 -h ws6.weave.local fintanr/weave-gs-nginx-apache
```

Note the --with-dns option and the -h option, --with-dns tells the container to use WeaveDNS to resolve names and
-h x.weave.local allows the host to be resolvable with WeaveDNS. 

Finally we launch our Nginx container
```
vagrant ssh weave-gs-01
sudo weave run --with-dns 10.3.1.7/24 -ti -h nginx.weave.local -d -p 80:80 fintanr/weave-gs-nginx-simple 
```
 
### What has happened? ###

At this point you have launched Weave and WeaveDNS on all of your hosts, and connected six containers running our simple php
application and Nginx together using Weave. 

{'fraction_non_alphanumeric': 0.08085177175178838, 'fraction_numerical': 0.0272833139244718, 'mean_word_length': 3.9375770020533882, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 12, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 30, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6248643', 'n_tokens_mistral': 4055, 'n_tokens_neox': 3658, 'n_words': 1608}