From 312e91a9405b47ccb18ef2e3baada682d7d678e7 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 3 Jan 2015 09:44:52 +0100 Subject: [PATCH 1/4] fixed typo --- nginx_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx_proxy.py b/nginx_proxy.py index bb6bdab..6cb549f 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -113,7 +113,7 @@ def handle_event(event): os.kill(pid, signal.SIGHUP) if not os.path.exists(target_path): - os.mkdirs(target_path) + os.mkdir(target_path) pid = get_pid() From f7713d84a3127a76eb714431f801f294f22020d8 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 7 Jan 2015 11:38:22 +0100 Subject: [PATCH 2/4] handled exception if the docker deamon does not expose events on the REST-API --- nginx_proxy.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nginx_proxy.py b/nginx_proxy.py index 6cb549f..89ace5a 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -1,6 +1,7 @@ #!/usr/bin/python3.4 import os +import sys import http.client import json import signal @@ -118,7 +119,12 @@ if not os.path.exists(target_path): pid = get_pid() conn = http.client.HTTPConnection("localhost:2375") -conn.request("GET", "/events") +try: + conn.request("GET", "/events") +except ConnectionRefusedError: + print('Docker does not expose events on its REST-API. Perhaps it is not running?') + sys.exit(-1) + response = conn.getresponse() events = "" From fa01646eb43f09e2b45c2733ebe08e38f4a1e747 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 7 Jan 2015 15:06:15 +0100 Subject: [PATCH 3/4] added option to specify the ip to listen on. If the option is missing, nginx will be listening on all ips. --- nginx_proxy.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/nginx_proxy.py b/nginx_proxy.py index 89ace5a..a6d6de6 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -10,7 +10,7 @@ from string import Template target_path="/tmp/nginx" pid_file="/run/nginx.pid" non_location_template = """server { - listen 80; + listen $listen; server_name $name; location / { proxy_set_header X-Real-IP $$remote_addr; @@ -22,7 +22,7 @@ non_location_template = """server { """ location_template="""server { - listen 80; + listen $listen; server_name $name; location / { return 301 $$scheme://$name/$location; @@ -102,14 +102,18 @@ def handle_event(event): if 'location' in proxy_data: location = proxy_data['location'] + listen ='*:80' + if 'ip' in proxy_data: + listen = proxy_data['ip']+':80' + print('writing /tmp/nginx/proxy_'+container_id) with open('/tmp/nginx/proxy_'+container_id, 'w') as file: if location == '': s = Template(non_location_template) - file.write(s.substitute(name=server_name,ip=ip,port=port)) + file.write(s.substitute(name=server_name,ip=ip,port=port,listen=listen)) else: s = Template(location_template) - file.write(s.substitute(name=server_name,ip=ip,port=port,location=location)) + file.write(s.substitute(name=server_name,ip=ip,port=port,listen=listen,location=location)) print('HUPing nginx') os.kill(pid, signal.SIGHUP) From 1eda6e5cb8649b136dee4eff059ef4e4a4de8bbd Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 7 Jan 2015 15:14:29 +0100 Subject: [PATCH 4/4] proxy variables now may contain ':' (important for IPv6) --- nginx_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx_proxy.py b/nginx_proxy.py index a6d6de6..611094f 100755 --- a/nginx_proxy.py +++ b/nginx_proxy.py @@ -70,7 +70,7 @@ def analyse_env_vars(data): def analyse_proxy_data(data): proxy_data = {} for proxy_var in data.split(','): - t = proxy_var.split(":") + t = proxy_var.split(":",1) proxy_data[t[0]] = t[1] return proxy_data