Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ podman run -d \
```bash
podman build -t nginx-certbot:latest .
```
## Hostname resolution
In order to update the IP of a backend container once it has been restarted this container must specify a DNS server. For Podman this defaults to `10.88.0.1`, if you alter your container networking this will have to be amended in `src/skel.conf` and the container will have to be built yourself.
5 changes: 4 additions & 1 deletion conf.d/site.conf.sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
server {
listen 80;
server_name contoso.com;
resolver @@resolver@@ valid=30s ipv6=off;

location / {
proxy_pass http://webserver_container_name/;
set $backend http://$webserver_container_name;
proxy_pass $backend;
}
}
20 changes: 19 additions & 1 deletion src/nginx_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import re
import logging
import subprocess

logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)

Expand All @@ -20,6 +21,19 @@
email = os.getenv('EMAIL')
prod = os.getenv('PRODUCTION')

# get resolver
resolver = None
try:
out = subprocess.check_output(['ip', 'route'], stderr=subprocess.DEVNULL).decode()
m = re.search(r'^default via (\d+\.\d+\.\d+\.\d+)', out, re.MULTILINE)
if m:
resolver = m.group(1)
except Exception:
resolver = os.getenv('10.88.0.1')
logging.info("ip route unavailable, obtaining resolver failed. Container IPs most likely won't update after restarting")

logging.info("Using resolver: " + resolver)

# copy in any manual conf files the user made
os.system("cp -rf /etc/nginx/conf.avail/*.conf /etc/nginx/http.d/ &> /dev/null")
logging.info("Configurations present: " + str(os.listdir("/etc/nginx/http.d")))
Expand All @@ -44,7 +58,11 @@
with open('/root/skel.conf') as conf_file:
conf_contents = conf_file.read()

conf_complete = re.sub(r"@@(\w+?)@@", lambda match: host[match.group(1)], conf_contents)
conf_complete = re.sub(
r"@@(\w+?)@@",
lambda match, host=host: str(host.get('resolver', resolver)) if match.group(1) == 'resolver' else str(host.get(match.group(1), '')),
conf_contents
)

logging.info("Writing /etc/nginx/http.d/" + host['hostname'] + ".conf")

Expand Down
5 changes: 4 additions & 1 deletion src/skel.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
server {
listen 80;
server_name @@hostname@@;
resolver @@resolver@@ valid=30s ipv6=off;

location / {
proxy_pass @@proxy_pass@@;
set $backend @@proxy_pass@@;
proxy_pass $backend;
}
}