update: scaffolding for nginx

This commit is contained in:
maix0 2025-06-12 18:35:04 +02:00
parent d89dd4f315
commit fb48a0fa04
7 changed files with 163 additions and 24 deletions

30
nginx/17-add-template-prefix.sh Executable file
View file

@ -0,0 +1,30 @@
#!/bin/sh
set -e
ME=$(basename "$0")
entrypoint_log() {
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
echo "$@"
fi
}
auto_template() {
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
[ -d "$template_dir" ] || return 0
if [ ! -w "$template_dir" ]; then
entrypoint_log "$ME: ERROR: $template_dir exists, but is not writable"
return 0
fi
find "$template_dir" -follow -type f -print | while read -r template; do
entrypoint_log "$ME: adding $suffix to $template"
mv "$template" "$template$suffix"
done
}
auto_template
exit 0

25
nginx/Dockerfile Normal file
View file

@ -0,0 +1,25 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Dockerfile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/06/12 16:42:38 by maiboyer #+# #+# #
# Updated: 2025/06/12 18:15:38 by maiboyer ### ########.fr #
# #
# **************************************************************************** #
FROM nginx:stable-alpine
RUN mkdir -p /etc/nginx/ && \
mkdir -p /etc/ssl/certs && \
mkdir -p /etc/ssl/private && \
apk add openssl && \
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/nginx-selfsigned.key \
-out /etc/ssl/certs/nginx-selfsigned.crt \
-subj "/C=FR/OU=student/CN=${NGINX_DOMAIN}"
COPY ./17-add-template-prefix.sh /docker-entrypoint.d/
COPY ./conf /etc/nginx/templates

26
nginx/conf/default.conf Normal file
View file

@ -0,0 +1,26 @@
# this allows the redirection of `http://domain/URL` to `https://domain/URL`
server {
charset UTF-8;
listen 80;
listen [::]:80;
server_name $NGINX_DOMAIN;
if ($host = example.com) {
return 301 https://$host$request_uri;
}
return 404;
}
server {
charset UTF-8;
listen [::]:443 ssl;
listen 443 ssl;
server_name $NGINX_DOMAIN;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
ssl_protocols TLSv1.3;
include conf.d/locations/*.conf;
}

View file

@ -0,0 +1,4 @@
location /api/db {
add_header Content-Type text/plain;
return 200 'db api yay';
}