« Nginx ReverseProxy LXC Alpine Linux » : différence entre les versions
| (37 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 1 : | Ligne 1 : | ||
Installation d'un reverse proxy | 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 = | = Installation = | ||
| Ligne 5 : | Ligne 5 : | ||
# apk update && apk upgrade | # apk update && apk upgrade | ||
# apk add nginx certbot certbot-nginx | # apk add nginx certbot certbot-nginx | ||
On modifie la configuration de | On modifie la configuration de Nginx pour la rapprocher du standard des distributions type Debian : | ||
{{Méta bandeau | |||
| niveau = information | |||
| icône = important | |||
| texte = Optionnel, sinon utiliser silmplement le répertoire "'''/etc/nginx/conf.d/'''" (Alpine 3.12) ou "'''/etc/nginx/http.d/'''" (Alpine 3.13) | |||
}} | |||
# mkdir /etc/nginx/sites-available && mkdir /etc/nginx/sites-enabled | # mkdir /etc/nginx/sites-available && mkdir /etc/nginx/sites-enabled | ||
On modifie le fichier de configuration de | On modifie le fichier de configuration de Nginx en ajoutant la ligne suivante : | ||
# vi /etc/nginx/nginx.conf | # vi /etc/nginx/nginx.conf | ||
---- | |||
# Includes | * Alpine Linux 3.12 : | ||
include /etc/nginx/ | <font color = grey>...</font> | ||
# Includes virtual hosts configs. | |||
include /etc/nginx/conf.d/*.conf; | |||
<font color = blue>include /etc/nginx/sites-enabled/*;</font> | |||
<font color = grey>...</font> | |||
* Alpine Linux 3.13 : | |||
<font color = grey>...</font> | |||
# Includes virtual hosts configs. | |||
include /etc/nginx/http.d/*.conf; | |||
<font color = blue>include /etc/nginx/sites-enabled/*;</font> | |||
<font color = grey>...</font> | |||
{{Méta bandeau | |||
| niveau = modéré | |||
| icône = important | |||
| texte = Voir [[UpgradeAlpine#NGINX|'''ce lien''']] | |||
}} | |||
---- | |||
On supprime le site par défaut : | On supprime le site par défaut : | ||
* Alpine Linux 3.12 : | |||
# rm /etc/nginx/conf.d/default.conf | # rm /etc/nginx/conf.d/default.conf | ||
* Alpine Linux 3.13 : | |||
# rm /etc/nginx/http.d/default.conf | |||
On active le serveur : | On active le serveur : | ||
# rc-update add nginx default | # rc-update add nginx default | ||
# service nginx start | # service nginx start | ||
=Création d'un "vhost" sécurisé= | =Création d'un "vhost" sécurisé= | ||
{{Méta bandeau | {{Méta bandeau | ||
| Ligne 24 : | Ligne 48 : | ||
| texte = Exemple avec "'''site.exemple.net'''" | | texte = Exemple avec "'''site.exemple.net'''" | ||
}} | }} | ||
==Création du certificat== | |||
# certbot certonly --nginx --agree-tos --email <font color = blue>moncourriel@exemple.net</font> -d <font color = blue>site.exemple.net</font> | |||
==Création du vhost== | |||
On crée le fichier de configuration dans "/etc/nginx/site-availables" | |||
# vi /etc/nginx/sites-available/<font color = blue>exemple</font>.conf | |||
server { | |||
listen 80; | |||
listen [::]:80; | |||
server_name <font color = blue>site.exemple.net</font>; | |||
# return 404; | |||
return 301 <nowiki>https://</nowiki>$host$request_uri; | |||
} | |||
server { | |||
server_name <font color = blue>site.exemple.net</font>; | |||
error_page 403 https://<font color = blue>site.exemple.net</font>; | |||
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://<font color = blue>IP_SERVEUR_WEB:PORT</font>; | |||
# proxy_pass http<font color = red>s</font>://<font color = blue>IP_SERVEUR_WEB:PORT</font>; #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/<font color = blue>site.exemple.net</font>/fullchain.pem; | |||
ssl_certificate_key /etc/letsencrypt/live/<font color = blue>site.exemple.net</font>/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/<font color = blue>site.exemple.net</font>/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/<font color = blue>exemple</font>.conf /etc/nginx/sites-enabled/ | |||
{{Méta bandeau | |||
| niveau = information | |||
| icône = loupe | |||
| texte = Ainsi pour désactiver et conserver le fichier de configuration d'un site on ne supprime que le lien symbolique et on recharge Nginx. | |||
}} | |||
{{Méta bandeau | |||
| niveau = modéré | |||
| icône = important | |||
| texte = Toujours utiliser les chemins complets pour la création des liens symboliques. | |||
}} | |||
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 == | |||
{{Méta bandeau | |||
| niveau = information | |||
| icône = loupe | |||
| texte = Dans cet exemple la commande est lancée tous les premiers du mois à 4h00 avec la date et la sortie texte dans le fichier "/var/log/letsencrypt/crontupdate.log". | |||
}} | |||
# crontab -e | |||
<font color = grey>...</font> | |||
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 : | |||
{{Méta bandeau | |||
| niveau = information | |||
| icône = loupe | |||
| texte = Voir [[Postfix#Alpine_Linux|installation d'un relais local Postfix]]. | |||
}} | |||
<font color = grey>...</font> | |||
<font color =blue>MAILTO=root</font> | |||
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 <font color = blue>site.exemple.net</font> | |||
<font color = grey>... | |||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |||
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:</font> '''Y''' | |||
<font color = grey>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |||
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:</font> '''Y''' | |||
Version actuelle datée du 8 octobre 2022 à 12:24
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