From 7196d03ec8eeb04490705a58b21d95350f977d8c Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 3 Apr 2016 12:05:51 +0200 Subject: [PATCH] enable list of domains for a virtual host Enables the autoproxy to handle multiple domains for a virtual host: e.g. a container can handle requests to www.somedomain.de and somedomain.de --- README.md | 25 ++++++++++++------------- nginx_proxy.py | 5 ++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7683d64..c4d9786 100644 --- a/README.md +++ b/README.md @@ -2,26 +2,26 @@ This image attaches to the docker event queue and creates/removes proxy settings in the contained nginx. ## How it works -Containers that should be proxied neet meta information in the environment variable *PROXY_DATA* available. +Containers that should be proxied need meta information in the environment variable *PROXY_DATA* available. This variable must be of the following format: -`PROXY_DATA=server_name:test.com,port:80` +`PROXY_DATA=server_names:test.com;www.test.com,port:80` The following options are possible: -* **server_name**(required) the name of the virtual host +* **server_names**(required) the names of the virtual hosts separated by ";" * **port**(optional, defaults to 80) the port on the target container -* **ip**(optional, defaults to listen on all IPs) the IP on which the proxy should listen. +* **ip**(optional, defaults to listen on all IPs) the IP on which the proxy should listen. * **location**(optional) if the proxied web application is not running on the /-path ## Starting the container -Since the container uses Docker's internal event reporting, it needs access to the daemon. At the -moment, only access via UNIX socket ist possible. Because of that, the socket has to be handed -into the container (*-v /var/run/docker.sock:/var/run/docker.sock*). +Since the container uses Docker's internal event reporting, it needs access to the daemon. At the +moment, only access via UNIX socket is possible. Because of that, the socket has to be handed +into the container (*-v /var/run/docker.sock:/var/run/docker.sock*). ### Single IP / All IPs -This option is used if your Docker Host has only one IP or if there is no need to differentiate between different IPs regarding wether a Web App +This option is used if your Docker Host has only one IP or if there is no need to differentiate between different IPs regarding wether a Web App is available on it. Run the container like this: @@ -29,7 +29,7 @@ Run the container like this: That means that the container exposes all Web Apps on all IPs. Do **not** use the *ip* option from above on the target containers. The *PROXY_DATA* environment variables would be something like -`PROXY_DATA=server_name:cooldomain.test.com,port:8080,location=/webApp` +`PROXY_DATA=server_names:cooldomain.test.com,port:8080,location=/webApp` ### Multiple IPs This option is used if your Docker Host has multiple IPs (perhaps a public IP in the internet and a private IP on a VPN). It is possible to expose some Web Apps only to the private network. @@ -38,9 +38,8 @@ One container must be started for each IP that should host Web Apps. For example `docker run --name auto_proxy_public -d -v /var/run/docker.sock:/var/run/docker.sock -p 1.2.3.4:80:80 docker_nginx_auto_proxy` `docker run --name auto_proxy_private -d -v /var/run/docker.sock:/var/run/docker.sock -p 10.1.2.3:80:80 docker_nginx_auto_proxy` -If a target container does **not** have the *ip* option set, it listens on **all** IP adresses and will be handled by both containers. +If a target container does **not** have the *ip* option set, it listens on **all** IP adresses and will be handled by both containers. If a container uses, e.g., -`PROXY_DATA=server_name:cooldomain.test.com,port:8080,location=/webApp,ip=10.1.2.3` - -then it will be only available on the private 10.1.2.3 IP (perhaps using a VPN). +`PROXY_DATA=server_names:cooldomain.test.com,port:8080,location=/webApp,ip=10.1.2.3` +then it will be only available on the private 10.1.2.3 IP (perhaps using a VPN). diff --git a/nginx_proxy.py b/nginx_proxy.py index 4c0c19c..1fda14b 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -26,7 +26,7 @@ server { location_template="""# proxy for container '$containername' server { listen $listen; - server_name $name; + server_name $names; location / { return 301 $$scheme://$name/$location; } @@ -94,7 +94,7 @@ def handle_container(id): 'containername': extract_name(inspect_data), 'ip': extract_ip(inspect_data), 'location': get_if_available(proxy_data, 'location', ''), - 'name': get_if_available(proxy_data, 'server_name', ''), + 'names': get_if_available(proxy_data, 'server_names', '').replace(';', ' '), 'port': get_if_available(proxy_data, 'port', 80), 'listen': '*:80' } @@ -190,4 +190,3 @@ if __name__ == '__main__': handle_container(container_id) reload_nginx_configuration() -