Skip to content

Commit 6a88512

Browse files
authored
Merge pull request #6 from rohittp0/main
Multiple Domain Support
2 parents 5a65326 + a341d2a commit 6a88512

File tree

3 files changed

+101
-41
lines changed

3 files changed

+101
-41
lines changed

.github/workflows/docker-image.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ jobs:
1717
steps:
1818
- name: Checkout code
1919
uses: actions/checkout@v4
20-
20+
2121
- name: Login to Docker Registry
2222
uses: docker/login-action@v3
2323
with:
2424
username: ${{ secrets.DOCKERHUB_USERNAME }}
2525
password: ${{ secrets.DOCKERHUB_TOKEN }}
2626

2727
- name: Build and push Docker image
28-
uses: docker/build-push-action@v5
28+
uses: docker/build-push-action@v6
2929
with:
3030
context: .
3131
file: Dockerfile
3232
push: true
3333
tags: |
34-
nocodb/nginx-secure:${{ github.sha }}
35-
nocodb/nginx-secure:latest
34+
${{ secrets.DOCKERHUB_USERNAME }}/nginx-secure:${{ github.sha }}
35+
${{ secrets.DOCKERHUB_USERNAME }}/nginx-secure:latest

default.conf.template

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
upstream target {
1+
upstream $PROXY_HOST-target {
22
least_conn;
33
server $PROXY_HOST:$PROXY_PORT;
44
}
@@ -7,11 +7,13 @@ server {
77
listen 80;
88
server_name $PROXY_DOMAIN;
99

10+
client_max_body_size 50M;
11+
1012
location / {
11-
proxy_pass http://target;
13+
proxy_pass http://$PROXY_HOST-target;
1214
proxy_set_header Host $host;
1315
proxy_set_header X-Real-IP $remote_addr;
1416
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1517
proxy_set_header X-Forwarded-Proto $scheme;
1618
}
17-
}
19+
}

entrypoint.sh

Lines changed: 92 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,108 @@
11
#!/bin/bash
22

3-
# Hydrate default.conf.template PROXY_HOST and PROXY_PORT PROXY_DOMAIN with environment variables
4-
envsubst '$PROXY_HOST,$PROXY_PORT,$PROXY_DOMAIN' < /app/default.conf.template > /etc/nginx/conf.d/default.conf
3+
# Stop on error
4+
set -e
55

6+
# -------------------
7+
# DEBUG information
8+
# -------------------
69
if [ "$DEBUG" = "true" ]; then
710
echo "DEBUG MODE ENABLED"
8-
9-
echo "Nginx configuration:"
10-
cat /etc/nginx/conf.d/default.conf
11-
echo -e "\n==========================="
12-
13-
echo "Existing certificates:"
14-
certbot certificates
15-
echo -e "\n==========================="
16-
17-
echo "Environment variables:"
18-
echo " PROXY_HOST: $PROXY_HOST"
19-
echo " PROXY_PORT: $PROXY_PORT"
20-
echo " PROXY_DOMAIN: $PROXY_DOMAIN"
21-
echo " SSL_ENABLED: $SSL_ENABLED"
11+
echo "MAPPINGS: $MAPPINGS"
12+
echo "SSL_ENABLED: $SSL_ENABLED"
13+
echo "Let's encrypt email: ${LETSENCRYPT_EMAIL:-contact@domain.com}"
2214
echo "==========================="
2315
fi
2416

25-
if [ "$SSL_ENABLED" = "true" ]; then
26-
# check if certbot certificates already exist for $PROXY_DOMAIN
27-
if certbot certificates | grep -q $PROXY_DOMAIN; then
28-
echo "Certificate already exists for $PROXY_DOMAIN"
29-
certbot --cert-name $PROXY_DOMAIN install
17+
# -------------------
18+
# Split the MAPPINGS
19+
# -------------------
20+
IFS=',' read -ra MAPPING_LIST <<< "$MAPPINGS"
21+
22+
# Clear out any old default config(s) (optional)
23+
rm -f /etc/nginx/conf.d/*.conf
24+
25+
# For each mapping: domain=host:port
26+
for MAPPING in "${MAPPING_LIST[@]}"; do
27+
28+
# Extract the domain, host, port
29+
DOMAIN="$(echo "$MAPPING" | cut -d= -f1)"
30+
HOSTPORT="$(echo "$MAPPING" | cut -d= -f2)"
31+
32+
PROXY_HOST="$(echo "$HOSTPORT" | cut -d: -f1)"
33+
PROXY_PORT="$(echo "$HOSTPORT" | cut -d: -f2)"
34+
35+
# Export these so envsubst can substitute them
36+
export PROXY_DOMAIN="$DOMAIN"
37+
export PROXY_HOST="$PROXY_HOST"
38+
export PROXY_PORT="$PROXY_PORT"
39+
40+
# -------------------------
41+
# Render Nginx config
42+
# -------------------------
43+
if [ "$DEBUG" = "true" ]; then
44+
echo "Generating config for:"
45+
echo " Domain: $PROXY_DOMAIN"
46+
echo " Host: $PROXY_HOST"
47+
echo " Port: $PROXY_PORT"
48+
fi
49+
50+
# Determine which template to use
51+
CUSTOM_TEMPLATE="/app/${PROXY_HOST}.${PROXY_PORT}.conf"
52+
DEFAULT_TEMPLATE="/app/default.conf.template"
53+
54+
if [ -f "$CUSTOM_TEMPLATE" ]; then
55+
TEMPLATE="$CUSTOM_TEMPLATE"
56+
echo "Using custom template: $TEMPLATE"
3057
else
31-
echo "Certificate does not exist for $PROXY_DOMAIN, creating..."
32-
certbot --nginx --email "[email protected]" --agree-tos --no-eff-email -d $PROXY_DOMAIN
58+
TEMPLATE="$DEFAULT_TEMPLATE"
59+
echo "Using default template: $TEMPLATE"
3360
fi
34-
fi
3561

62+
# Use envsubst to produce a .conf per domain
63+
envsubst '$PROXY_DOMAIN,$PROXY_HOST,$PROXY_PORT' \
64+
< "$TEMPLATE" \
65+
> "/etc/nginx/conf.d/${PROXY_DOMAIN}.conf"
66+
67+
# -------------------------
68+
# Issue or Install SSL Cert
69+
# -------------------------
70+
if [ "$SSL_ENABLED" = "true" ]; then
71+
72+
# Check whether a cert exists for this domain
73+
if certbot certificates | grep -q "$PROXY_DOMAIN"; then
74+
echo "Certificate already exists for $PROXY_DOMAIN"
75+
certbot --cert-name "$PROXY_DOMAIN" install
76+
else
77+
echo "Creating certificate for $PROXY_DOMAIN..."
78+
certbot --nginx \
79+
--email "${LETSENCRYPT_EMAIL:-contact@domain.com}" \
80+
--agree-tos \
81+
--no-eff-email \
82+
-d "$PROXY_DOMAIN"
83+
fi
84+
fi
85+
86+
if [ "$DEBUG" = "true" ]; then
87+
echo "-------------------------------------------"
88+
fi
89+
done
90+
91+
# -------------------------
92+
# Debug / Verification
93+
# -------------------------
3694
if [ "$DEBUG" = "true" ]; then
37-
echo "Updated Nginx configuration:"
38-
cat /etc/nginx/conf.d/default.conf
39-
echo -e "\n==========================="
95+
echo "Final Nginx Config(s):"
96+
cat /etc/nginx/conf.d/*.conf
97+
echo "-------------------------------------------"
4098

41-
echo "Certbot log:"
42-
cat /var/log/letsencrypt/letsencrypt.log
43-
echo -e "\n==========================="
99+
echo "Existing certificates:"
100+
certbot certificates || true
101+
echo "-------------------------------------------"
44102
fi
45103

46-
# Stop nginx if it's already running
47-
nginx -s stop
104+
# Stop nginx if it's already running (ignore error if not running)
105+
nginx -s stop || true
48106

49-
# Start nginx
50-
nginx -g "daemon off;"
107+
# Start nginx in foreground
108+
exec nginx -g "daemon off;"

0 commit comments

Comments
 (0)