reverse-proxy.md 6.1 KB

% Reverse Proxy

Introduction

Voir Infrastructure du réseau sur serveur Kimsufi OVH

Installation letsencrypt

cf Certbot Instructions | Certbot

Dépendances

sudo apt update && sudo apt install python3 python3-venv libaugeas0

Installation de certbot

sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
sudo /opt/certbot/bin/pip install certbot certbot-nginx
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

Installation des sous-domaines

Domaines à installer

On prépare une liste des services et de leur sous-domaine associé. Par exemple, voir cette page: Services

Configuration de nginx

Chargement dans les sous-dossiers

Édition de /etc/nginx/nginx.conf

Remplacer la ligne:

include /etc/nginx/sites-enabled/*;

Par:

include /etc/nginx/sites-enabled/*/*;

Script

Je crée un script d'installation qui génère automatiquement chaque fichier de configuration nginx pour les sous-domaines listés dans un fichier (ici par exemple: subdomains.txt) :

$ cat ~/subdomains.txt 
nu.aezi.fr        
cloud.aezi.fr     
git.aezi.fr       
wallabag.aezi.fr  
appflowy.aezi.fr  
rustdesk.aezi.fr  
sync.aezi.fr      
pihole.aezi.fr    
hedgedoc.aezi.fr  
dessin.aezi.fr

Script install-subdomains

IMPORTANT: script inspiré de ce gist: How to use nginx as a reverse-proxy with letsencrypt · GitHub

ATTENTION: si le script ne fonctionne pas, voir la variante en dessous.

#!/bin/bash

set -euo pipefail

if [ $EUID != 0 ]
then
	echo "You must be root" >&2
	exit 1
fi


available_sites_dir=/etc/nginx/sites-available

subdomains_list="${1:-}"
if [ -z "${subdomains_list:-}" ]
then
	echo "Please give me a subdomain list"
	exit 2
fi
shift


create_subdomain(){
	local base_directory=$2
	local subdomain_name=$1
	local target_ip=$3
	local main_domain=$4
	if [ ! -f "$base_directory/$subdomain_name" ]
	then
		echo "Creating '$base_directory/$subdomain_name'"
		cat > $base_directory/$subdomain_name <<EOF
server {
    server_name $subdomain_name;

    listen 80;
    listen [::]:80;

    location / {
        proxy_pass  https://$target_ip:443;
        proxy_redirect                      off;
        proxy_set_header  Host              \$http_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_read_timeout                  900;
    }
}
EOF
	fi
}

while read subdomain
do
	subdomain=$(echo $subdomain | xargs)

	if [ -n "$subdomain" ]
	then
		echo "subdomain='$subdomain'"
		domain_name=${subdomain#*.}
		domain_dir=${available_sites_dir}/${domain_name}
		if [ ! -d ${domain_dir} ]
		then
			echo "Creating dir '${domain_dir}'"
			mkdir -p ${domain_dir}
		fi
		create_subdomain $subdomain ${domain_dir} 10.1.0.14 nu.aezi.fr
	fi
done < ${subdomains_list}

Variante

Ajouter le code suivant dans la section server sous la section listen 80; listen [::]:80; :

    # HTTP to HTTPS
    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

Activation des fichiers de configuration

Après avoir généré les fichiers avec le le script précédent (et fait les modifications indiquées dans nginx.conf au paragraphe précédent), on active les configurations des sites:

cd /etc/nginx/sites-enabled/aezi.fr
ls ../../sites-available/aezi.fr/ | xargs -i{} sudo ln -s ../../sites-available/aezi.fr/{}

Installation du certificat

L'installation du certificat se fait en lançant :

sudo certbot --nginx

Puis en sélectionnant la liste des certificats en sélectionnant le premier comme principal (ici : nu.aezi.fr).

(en cas de mauvaise manipulation voir Dépannage)

Donc on saisit 5 pour mettre nu.aezi.fr en premier puis les autres numéros:

5 1 2 3 4 6 7 8 9

Ce qui donne:

$ sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log

Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: appflowy.aezi.fr
2: dessin.aezi.fr
3: git.aezi.fr
4: hedgedoc.aezi.fr
5: nu.aezi.fr
6: pihole.aezi.fr
7: rustdesk.aezi.fr
8: sync.aezi.fr
9: wallabag.aezi.fr
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 

Une fois que c'est près, normalement tout devrait fonctionner

Dépannage{#depannage}

Modifier le domaine principal

Il faut le mettre en premier et utiliser --force-renewal:

./certbot --apache -d domaine-principal.fr -d www.domaine-principal.fr -d mysql.domaine-principal.fr --force-renewal

Voir: cette réponse à "Change base domain name for Lets Encrypt SSL certificate"

Notes supplémentaires