Skip to content

Commit

Permalink
add STATIC_VIRTUAL_HOST for static mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed Apr 1, 2023
1 parent 64057b6 commit 7599dd7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/venv
/.venv
/.git
/.gitignore
/.dockerignore
Expand All @@ -7,4 +8,4 @@ __pycache__/
/README.md
/Dockerfile-staging
/README.md

.run_data/
7 changes: 4 additions & 3 deletions nginx_proxy/Host.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def __repr__(self):
"port": self.port})

def __str__(self):
hostname= "%s:%s" % (
self.hostname if self.hostname else '?',
str(self.port) if self.port is not None else '?')
return self.__repr__()
# hostname= "%s:%s" % (
# self.hostname if self.hostname else '?',
# str(self.port) if self.port is not None else '?')


8 changes: 4 additions & 4 deletions nginx_proxy/Location.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Any
from typing import Dict, Any, Set

from . import Container

Expand All @@ -12,7 +12,7 @@ def __init__(self, name, is_websocket_backend=False, is_http_backend=True):
self.http = is_http_backend
self.websocket = is_websocket_backend
self.name = name
self.containers = set()
self.containers :Set[Container.Container] = set()
self.extras: Dict[str, Any] = {}

def update_extras(self, extras: Dict[str, Any]):
Expand All @@ -28,13 +28,13 @@ def update_extras(self, extras: Dict[str, Any]):
else:
self.extras[x] = extras[x]

def add(self, container: Container):
def add(self, container: Container.Container):
self.containers.add(container)

def isempty(self):
return len(self.containers) == 0

def remove(self, container: Container):
def remove(self, container: Container.Container):
if container in self.containers:
self.containers.remove(container)
return True
Expand Down
30 changes: 21 additions & 9 deletions nginx_proxy/ProxyConfigData.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, Set, Generator, Tuple, Union

from nginx_proxy.Host import Host
from nginx_proxy.Location import Location


class ProxyConfigData:
Expand Down Expand Up @@ -73,22 +74,33 @@ def __len__(self):
def print(self):

for host in self.host_list():
if host.port != 80:
url = "- " + ("https" if host.secured else "http") + "://" + host.hostname + ":" + str(host.port)
else:
url = "- " + ("https" if host.secured else "http") + "://" + host.hostname
postfix="://" + host.hostname
def host_url(isWebsocket=False):
if host.secured:
return "- " + ("wss" if isWebsocket else "https") + postfix + (":" + str(host.port) if host.port!=443 else '')
else:
return "- " + ("ws" if isWebsocket else "http") + postfix + (":" + str(host.port) if host.port!=80 else '')

if host.isredirect():
print(url)
print(host_url())
print(" redirect : ", host.full_redirect)
else:
if len(host.extras):
print(url)
print(host_url())
self.printextra(" ", host.extras)
for location in host.locations.values():
print(url + location.name)
print(" Type: ", "Websocket" if location.websocket else "Http")
print(host_url(location.websocket)+location.name)
for container in location.containers:
print(" -> ", (container.scheme) + "://"+container.address + (":"+ str(container.port) if container.port else '' )+container.path)

if len(location.extras):
self.printextra(" ", location.extras)
self.printextra(" ", location.extras)

# self.address: str = address
# self.port: int = port
# self.path: Union[str, None] = path
# self.scheme: str = scheme
# self.networks =

@staticmethod
def printextra(gap, extra):
Expand Down
4 changes: 2 additions & 2 deletions nginx_proxy/WebServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import time
from typing import List
import json

import requests
from docker import DockerClient
Expand Down Expand Up @@ -87,7 +88,7 @@ def learn_yourself(self):
file=sys.stderr)
print("Falling back to default network", file=sys.stderr)
network = self.client.networks.get("frontend")
self.networks[network.id] = "frontend"
self.networks[network.id] = network.id

def _register_container(self, container: DockerContainer):
"""
Expand Down Expand Up @@ -151,7 +152,6 @@ def reload(self, forced=False) -> bool:
else:
response = self.nginx.update_config(output)
return response

def disconnect(self, network, container, scope):

if self.id is not None and container == self.id:
Expand Down
15 changes: 13 additions & 2 deletions nginx_proxy/pre_processors/virtual_host_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _parse_host_entry(entry_string: str):
external, internal = (split_url(external), split_url(internal))
c = Container(None,
scheme=list(internal['scheme'])[0] if len(internal['scheme']) else 'http',
address=None,
address=internal["host"] if internal["host"] else None,
port=internal["port"] if internal["port"] else None,
path=internal["location"] if internal["location"] else "/")
h = Host(
Expand All @@ -83,7 +83,8 @@ def host_generator(container: DockerContainer, service_id: str = None, known_net

# List all the environment variables with VIRTUAL_HOST and list them.
virtual_hosts = [x[1] for x in env_map.items() if x[0].startswith("VIRTUAL_HOST")]
if len(virtual_hosts) is 0:
static_hosts = [x[1] for x in env_map.items() if x[0].startswith("STATIC_VIRTUAL_HOST")]
if len(virtual_hosts) == 0 and len(static_hosts) == 0:
raise NoHostConiguration()

# Instead of directly processing container details, check whether or not it's accessible through known networks.
Expand All @@ -102,6 +103,16 @@ def host_generator(container: DockerContainer, service_id: str = None, known_net
else:
raise UnreachableNetwork()

for host_config in static_hosts:
host, location, container_data, extras = _parse_host_entry(host_config)
container_data.id = container.id
host.secured = 'https' in host.scheme or 'wss' in host.scheme or host.port == 443
if host.port is None :
host.port = 443 if host.secured else 80
if container_data.port is None :
container_data.port = 443 if ('https' in container_data.scheme or 'wss' in container_data.scheme) else 80
yield (host, location, container_data, extras)

override_ssl = False
override_port = None
if len(virtual_hosts) == 1:
Expand Down
16 changes: 14 additions & 2 deletions vhosts_template/default.conf.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ server{
proxy_pass {{ location.container.scheme }}://{{ location.upstream }}{{location.container.path}};{% else %}
proxy_pass {{location.container.scheme }}://{{ location.container.address }}:{{ location.container.port }}{{ location.container.path }};{% endif %}{% if location.name != '/' %}
proxy_redirect $scheme://$http_host{{ location.container.path if location.container.path else '/' }} $scheme://$http_host{{location.name}};{% endif %} {% if location.websocket and location.http %}
proxy_set_header Host $http_host;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;{% elif location.websocket %}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;{% elif location.websocket %}
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
Expand All @@ -58,8 +64,14 @@ server{
proxy_pass {{ location.container.scheme }}://{{ location.upstream }}{{location.container.path}};{% else %}
proxy_pass {{location.container.scheme }}://{{ location.container.address }}:{{ location.container.port }}{{ location.container.path }};{% endif %}{% if location.name != '/' %}
proxy_redirect $scheme://$http_host{{ location.container.path if location.container.path else '/' }} $scheme://$http_host{{location.name}};{% endif %} {% if location.websocket and location.http %}
proxy_set_header Host $http_host;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;{% elif location.websocket %}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;{% elif location.websocket %}
proxy_http_version 1.1;
proxy_read_timeout 1h;
proxy_send_timeout 1h;
Expand Down

0 comments on commit 7599dd7

Please sign in to comment.