Files
openresty-gateway/scripts/ensure-domain-conf.sh
2026-05-18 22:48:12 +08:00

59 lines
1.2 KiB
Bash

#!/usr/bin/env sh
set -eu
# Generate a minimal nginx virtual host for each missing domain config.
ROOT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
CONF_DIR="${CONF_DIR:-./conf/conf.d}"
cd "$ROOT_DIR"
if [ "$#" -eq 0 ]; then
echo "Usage: sh scripts/ensure-domain-conf.sh <domain> [domain...]" >&2
exit 1
fi
ensure_domain_conf() {
domain="$1"
conf_file="$CONF_DIR/$domain.conf"
if [ -f "$conf_file" ]; then
echo "Skipping existing nginx config: $conf_file"
return
fi
echo "Creating nginx config template: $conf_file"
mkdir -p "$CONF_DIR"
cat > "$conf_file" <<EOF
server {
listen 80;
listen 443 ssl;
server_name $domain;
ssl_certificate /etc/letsencrypt/live/$domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$domain/privkey.pem;
root /var/www/$domain;
index index.html;
location ^~ /.well-known/acme-challenge/ {
root /var/www;
default_type text/plain;
try_files \$uri =404;
}
location / {
if (\$scheme = http) {
return 301 https://\$host\$request_uri;
}
try_files \$uri \$uri/ /index.html;
}
}
EOF
}
for domain in "$@"; do
ensure_domain_conf "$domain"
done