« Nginx ReverseProxy LXC Alpine Linux » : différence entre les versions

De Le Wiki de Lug
Aller à la navigation Aller à la recherche
 
(16 versions intermédiaires par le même utilisateur non affichées)
Ligne 9 : Ligne 9 :
   | niveau = information
   | niveau = information
   | icône = important
   | 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)
   | 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
Ligne 16 : Ligne 16 :
----
----
* Alpine Linux 3.12 :
* Alpine Linux 3.12 :
  ...
  <font color = grey>...</font>
         # Includes virtual hosts configs.                                       
         # Includes virtual hosts configs.                                       
         include /etc/nginx/conf.d/*.conf;                                         
         include /etc/nginx/conf.d/*.conf;                                         
         <font color = blue>include /etc/nginx/sites-enabled/*;</font>
         <font color = blue>include /etc/nginx/sites-enabled/*;</font>
  ...
  <font color = grey>...</font>
* Alpine Linux 3.13 :
* Alpine Linux 3.13 :
  ...
  <font color = grey>...</font>
         # Includes virtual hosts configs.
         # Includes virtual hosts configs.
         include /etc/nginx/http.d/*.conf;
         include /etc/nginx/http.d/*.conf;
         <font color = blue>include /etc/nginx/sites-enabled/*;</font>
         <font color = blue>include /etc/nginx/sites-enabled/*;</font>
  ...
  <font color = grey>...</font>
{{Méta bandeau
{{Méta bandeau
   | niveau = modéré
   | niveau = modéré
Ligne 49 : Ligne 49 :
  }}
  }}
==Création du certificat==
==Création du certificat==
  # certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email <font color = blue>moncourriel@exemple.net</font> -d <font color = blue>site.exemple.net</font>
  # 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==
==Création du vhost==
On crée le fichier de configuration dans "/etc/nginx/site-availables"
On crée le fichier de configuration dans "/etc/nginx/site-availables"
  # vi /etc/nginx/sites-available/exemple.conf
  # vi /etc/nginx/sites-available/<font color = blue>exemple</font>.conf


  server {
  server {
Ligne 103 : Ligne 103 :
== Test et activation ==
== Test et activation ==
On crée un lien symbolique du fichier de configuration dans le répertoire des site actifs :
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/
  # ln -s /etc/nginx/sites-available/<font color = blue>exemple</font>.conf /etc/nginx/sites-enabled/
{{Méta bandeau
{{Méta bandeau
   | niveau = information
   | niveau = information
Ligne 118 : Ligne 118 :
On recharge la configuration pour activation :
On recharge la configuration pour activation :
  # service nginx reload
  # 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