nginx-proxy sets up a container running nginx and docker-gen. docker-gen generate reverse proxy configs for nginx and reloads nginx when containers are started and stopped.
See Automated Nginx Reverse Proxy for Docker for why you might want to use this.
This fork is from jwilder.
About this fork
This fork adds the possibility to specify some load_balancing nginx behaviour:
- can set least connected checking the env 'LOAD_BALANCER_LEAST_CONN' from the container
- can set session persistence checking the env 'LOAD_BALANCER_IP_HASH' from the container
- can set backup checking the env 'MARK_BACKUP_AFTER' from the container
- can set access from localhost checking the env 'LOCALHOST' from the container
Start any container you want proxied with an env var
$ docker run -e VIRTUAL_HOST=foo.bar.com -e LOAD_BALANCER_LEAST_CONN=true -e LOAD_BALANCER_IP_HASH=true -e LOCALHOST=true -e MARK_BACKUP_AFTER=2...
The env MARK_BACKUP_AFTER must be initialized with a numeric value. This numeric value specify after how many container they are marked as backup.
Es. If 5 containers with the same VIRTUAL_HOST env and MARK_BACKUP_AFTER=2 are started, the last 2 are used as normal server and the first 3 are marked as backup server
The original features
To run it:
$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy
Then start any containers you want proxied with an env var
$ docker run -e VIRTUAL_HOST=foo.bar.com ...
Provided your DNS is setup to forward foo.bar.com to the a host running nginx-proxy, the request will be routed to a container with the VIRTUAL_HOST env var set.
If your container exposes multiple ports, nginx-proxy will default to the service running on port 80. If you need to specify a different port, you can set a VIRTUAL_PORT env var to select a different one. If your container only exposes one port and it has a VIRTUAL_HOST env var set, that port will be selected.
If you need to support multiple virtual hosts for a container, you can separate each entry with commas. For example,
foo.bar.com,baz.bar.com,bar.com and each host will be setup the same.
You can also use wildcards at the beginning and the end of host name, like
foo.bar.*. Or even a regular expression, which can be very useful in conjunction with a wildcard DNS service like xip.io, using
~^foo\.bar\..*\.xip\.io will match
foo.bar.10.0.2.2.xip.io and all other given IPs. More information about this topic can be found in the nginx documentation about
You may want to do this to prevent having the docker socket bound to a publicly exposed container service.
To run nginx proxy as a separate container you'll need to have nginx.tmpl on your host system.
First start nginx with a volume:
$ docker run -d -p 80:80 --name nginx -v /tmp/nginx:/etc/nginx/conf.d -t nginx
Then start the docker-gen container with the shared volume and template:
$ docker run --volumes-from nginx \ -v /var/run/docker.sock:/tmp/docker.sock \ -v $(pwd):/etc/docker-gen/templates \ -t docker-gen -notify-sighup nginx -watch -only-published /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
Finally, start your containers with
VIRTUAL_HOST environment variables.
$ docker run -e VIRTUAL_HOST=foo.bar.com ...
SSL is supported using single host, wildcard and SNI certificates using naming conventions for
certificates or optionally specifying a cert name (for SNI) as an environment variable.
To enable SSL:
$ docker run -d -p 80:80 -p 443:443 -v /path/to/certs:/etc/nginx/certs -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy
The contents of
/path/to/certs should contain the certificates and private keys for any virtual
hosts in use. The certificate and keys should be named after the virtual host with a
.key extension. For example, a container with
VIRTUAL_HOST=foo.bar.com should have a
foo.bar.com.key file in the certs directory.
Wildcard certificates and keys should be name after the domain name with a
VIRTUAL_HOST=foo.bar.com would use cert name
If your certificate(s) supports multiple domain names, you can start a container with
to identify the certificate to be used. For example, a certificate for
could be named
shared.key. A container running with
CERT_NAME=shared will then use this shared cert.
How SSL Support Works
The SSL cipher configuration is based on mozilla nginx intermediate profile which
should provide compatibility with clients back to Firefox 1, Chrome 1, IE 7, Opera 5, Safari 1,
Windows XP IE8, Android 2.3, Java 7. The configuration also enables HSTS, and SSL
The behavior for the proxy when port 80 and 443 are exposed is as follows:
- If a container has a usable cert, port 80 will redirect to 443 for that container so that HTTPS
is always preferred when available.
- If the container does not have a usable cert, a 503 will be returned.
Note that in the latter case, a browser may get an connection error as no certificate is available
to establish a connection. A self-signed or generic cert named
will allow a client browser to make a SSL connection (likely w/ a warning) and subsequently receive
Basic Authentication Support
In order to be able to securize your virtual host, you have to create a file named as its equivalent VIRTUAL_HOST variable on directory
$ docker run -d -p 80:80 -p 443:443 -v /path/to/htpasswd:/etc/nginx/htpasswd -v /path/to/certs:/etc/nginx/certs -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy
You'll need apache2-utils on the machine you plan to create de htpasswd file. Follow these instructions