Nginx ReverseProxy LXC Alpine Linux
Installation d'un reverse proxy Nginx avec Certbot. Le container servira de point d'entrée pour les requêtes HTTP (port:80) et HTTPS (port:443) pour les rediriger sur les serveurs idoines en HTTPS uniquement. Certbot permettra de créer des certificat SSL reconnu par les navigateurs comme sécurisé.
Installation
On installe les paquets de base :
# apk update && apk upgrade # apk add nginx certbot certbot-nginx
On modifie la configuration de Nginx pour la rapprocher du standard des distributions type Debian :
# mkdir /etc/nginx/sites-available && mkdir /etc/nginx/sites-enabled
On modifie le fichier de configuration de Nginx en ajoutant la ligne suivante :
# vi /etc/nginx/nginx.conf
- Alpine Linux 3.12 :
...
# Includes virtual hosts configs.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
...
- Alpine Linux 3.13 :
...
# Includes virtual hosts configs.
include /etc/nginx/http.d/*.conf;
include /etc/nginx/sites-enabled/*;
...
On supprime le site par défaut :
- Alpine Linux 3.12 :
# rm /etc/nginx/conf.d/default.conf
- Alpine Linux 3.13 :
# rm /etc/nginx/http.d/default.conf
On active le serveur :
# rc-update add nginx default # service nginx start
Création d'un "vhost" sécurisé
Création du certificat
# certbot certonly --nginx --agree-tos --email moncourriel@exemple.net -d site.exemple.net
Création du vhost
On crée le fichier de configuration dans "/etc/nginx/site-availables"
# vi /etc/nginx/sites-available/exemple.conf
server {
listen 80;
listen [::]:80;
server_name site.exemple.net;
# return 404;
return 301 https://$host$request_uri;
}
server {
server_name site.exemple.net;
error_page 403 https://site.exemple.net;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
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 $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_pass http://IP_SERVEUR_WEB:PORT;
# proxy_pass https://IP_SERVEUR_WEB:PORT; #Alternatif pour redirection sur https
#proxy_buffering off; #activer pour désactiver le "buffering"
}
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/site.exemple.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site.exemple.net/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header Strict-Transport-Security "max-age=31536000" always;
ssl_trusted_certificate /etc/letsencrypt/live/site.exemple.net/chain.pem;
ssl_stapling on;
ssl_stapling_verify on;
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
}
Test et activation
On crée un lien symbolique du fichier de configuration dans le répertoire des site actifs :
# ln -s /etc/nginx/sites-available/exemple.conf /etc/nginx/sites-enabled/
On test la configuration :
# nginx -t
On recharge la configuration pour activation :
# service nginx reload
On peut aussi utiliser la commande suivante :
# nginx -s reload
Renouvellement certificat "Let's Encrypt"
Ligne de commande
La commande suivante renouvellera tous les certificats avec une période de validité inférieur à 30 jours :
# certbot renew
Automatisation via la crontab
# crontab -e
... 0 4 1 * * /bin/date > /var/log/letsencrypt/crontupdate.log && /usr/bin/certbot renew >> /var/log/letsencrypt/crontupdate.log
On peut également recevoir le résultat par mail :
... MAILTO=root 0 4 1 * * /bin/date > /var/log/letsencrypt/crontupdate.log && /usr/bin/certbot renew >> /var/log/letsencrypt/crontupdate.log
Révocation certificat "Let's Encrypt"
# certbot revoke --cert-name site.exemple.net
... - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you like to delete the certificate(s) you just revoked, along with all earlier and later versions of the certificate? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es (recommended)/(N)o: Y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following certificate(s) are selected for deletion: * site.exemple.net Are you sure you want to delete the above certificate(s)? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y