Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| linux:nginx [30-01-2019 14:59] – créée edmc73 | linux:nginx [12-05-2026 20:15] (Version actuelle) – [Convertir pour le smartphone (.p12)] edmc73 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Nginx ====== | ====== Nginx ====== | ||
| + | |||
| + | ===== Config reverse proxy ===== | ||
| <code nginx> | <code nginx> | ||
| server | server | ||
| Ligne 33: | Ligne 35: | ||
| </ | </ | ||
| + | |||
| + | |||
| + | <code nginx> | ||
| + | server { | ||
| + | listen 80 default; | ||
| + | server_name test.local; | ||
| + | |||
| + | location / { | ||
| + | # permet de transmettre l'ip réel au serveur interne | ||
| + | proxy_set_header X-Real-IP $remote_addr; | ||
| + | proxy_set_header X-Forwarded-For $remote_addr; | ||
| + | proxy_set_header Host $http_host; | ||
| + | |||
| + | # sur une certaine condition... | ||
| + | if ($request_body ~* ^(.*)\.test) { | ||
| + | proxy_pass http:// | ||
| + | break; | ||
| + | } | ||
| + | |||
| + | root /srv/http; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== GeoIP2 ===== | ||
| + | |||
| + | Créer un compte gratuit sur https:// | ||
| + | |||
| + | apt install libnginx-mod-http-geoip2 | ||
| + | |||
| + | |||
| + | Installation de geoipupdate pour télécharger et maintenir à jour la base de données des GeoIP | ||
| + | |||
| + | Doc → https:// | ||
| + | |||
| + | wget https:// | ||
| + | dpkg -i geoipupdate_7.1.0_linux_amd64.deb | ||
| + | |||
| + | <code bash> | ||
| + | # configuration | ||
| + | vi / | ||
| + | |||
| + | # GeoIP.conf file for `geoipupdate` program, for versions >= 3.1.1. | ||
| + | # Used to update GeoIP databases from https:// | ||
| + | # For more information about this config file, visit the docs at | ||
| + | # https:// | ||
| + | |||
| + | # `AccountID` is from your MaxMind account. | ||
| + | AccountID YOUR_ACCOUNT_ID_HERE | ||
| + | |||
| + | # `LicenseKey` is from your MaxMind account. | ||
| + | LicenseKey YOUR_LICENSE_KEY_HERE | ||
| + | |||
| + | # `EditionIDs` is from your MaxMind account. | ||
| + | # EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country | ||
| + | EditionIDs GeoLite2-Country | ||
| + | |||
| + | # Run GeoIP update | ||
| + | geoipdate | ||
| + | |||
| + | # Télécharge la bdd ici | ||
| + | / | ||
| + | |||
| + | # Ajout dans le cron | ||
| + | 32 2 * * 1,4 / | ||
| + | </ | ||
| + | |||
| + | Pour Nginx | ||
| + | |||
| + | <code nginx> | ||
| + | #Fichier de config pour spécifier l' | ||
| + | vi / | ||
| + | |||
| + | geoip2 / | ||
| + | auto_reload 60m; | ||
| + | $geoip2_data_country_code country iso_code; | ||
| + | } | ||
| + | |||
| + | geoip2 / | ||
| + | auto_reload 60m; | ||
| + | $geoip2_data_isp autonomous_system_organization; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Tester | ||
| + | |||
| + | apt install mmdb-bin | ||
| + | |||
| + | <code json> | ||
| + | mmdblookup --file / | ||
| + | |||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | 6255148 < | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | 2658434 < | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | 6252001 < | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Config pour restreindre par pays | ||
| + | |||
| + | <code nginx> | ||
| + | server{ | ||
| + | server_name toto.com; # managed by Certbot | ||
| + | listen [::]:443 ssl ipv6only=on; | ||
| + | listen 443 ssl; # managed by Certbot | ||
| + | ssl_certificate / | ||
| + | ssl_certificate_key / | ||
| + | include / | ||
| + | ssl_dhparam / | ||
| + | |||
| + | location / | ||
| + | if ($geoip2_data_country_code !~* " | ||
| + | return 403; | ||
| + | } | ||
| + | proxy_pass http:// | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | # Pour débugguer | ||
| + | #location / | ||
| + | # default_type text/plain; | ||
| + | # return 200 " | ||
| + | #} | ||
| + | |||
| + | access_log / | ||
| + | error_log / | ||
| + | } | ||
| + | server{ | ||
| + | if ($host = toto.com) { | ||
| + | return 301 https:// | ||
| + | } # managed by Certbot | ||
| + | |||
| + | listen 80 ; | ||
| + | listen [::]:80 ; | ||
| + | server_name toto.com; | ||
| + | return 404; # managed by Certbot | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Authentification par Certificat Client (TLS Mutual Auth) ===== | ||
| + | |||
| + | Permet de sécuriser l' | ||
| + | |||
| + | ==== Création du notre Autorité de Certification (CA) ==== | ||
| + | |||
| + | <code bash> | ||
| + | # Générer la clé de l' | ||
| + | openssl genrsa -out ca.key 4096 | ||
| + | # Créer le certificat de l' | ||
| + | openssl req -new -x509 -days 3650 -key ca.key -out ca.crt | ||
| + | </ | ||
| + | |||
| + | Quand la commande vous demande les informations, | ||
| + | * Country Name : FR | ||
| + | * Organization Name : Ma Maison | ||
| + | * Common Name : Mon-Autorite-CA (Utilisez un nom qui indique clairement que c'est le " | ||
| + | ==== Création du certification pour le client ==== | ||
| + | On génère une clé spécifiquer pour le client, signée par le CA. | ||
| + | |||
| + | Dans l' | ||
| + | <code bash> | ||
| + | # Générer la clé du smartphone | ||
| + | openssl genrsa -out smartphone.key 4096 | ||
| + | # Créer la demande de signature | ||
| + | openssl req -new -key smartphone.key -out smartphone.csr | ||
| + | # Signer le certificat avec votre CA | ||
| + | openssl x509 -req -days 365 -in smartphone.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out smartphone.crt | ||
| + | </ | ||
| + | |||
| + | C'est ici qu'il faut être vigilant : | ||
| + | * Country Name : FR | ||
| + | * Organization Name : Ma Maison (Le même que le CA, c'est mieux). | ||
| + | * Common Name : Smartphone-Jean (Il doit être différent du CN de l' | ||
| + | ==== Convertir pour le smartphone (.p12) ==== | ||
| + | < | ||
| + | openssl pkcs12 -export -out smartphone.p12 -inkey smartphone.key -in smartphone.crt -certfile ca.crt | ||
| + | </ | ||
| + | Un mot de passe sera demandé, il permet de sécuriser le fichier car il contient à la fois le certificat et sa clé privée. | ||
| + | |||
| + | |||
| + | |||
| + | Envoyez ce fichier .p12 à votre téléphone et installez-le dans les '' | ||
| + | |||
| + | Le mot de passe sera demandé lors de l' | ||
| + | |||
| + | Sur Chrome, il faut aller ici -> '' | ||
| + | ==== Configuration Nginx ==== | ||
| + | <code nginx> | ||
| + | server { | ||
| + | listen 443 ssl; | ||
| + | server_name votre-ha.domaine.fr; | ||
| + | |||
| + | ssl_certificate / | ||
| + | ssl_certificate_key / | ||
| + | |||
| + | # --- AJOUT MTLS --- | ||
| + | ssl_client_certificate / | ||
| + | ssl_verify_client on; | ||
| + | # ------------------ | ||
| + | |||
| + | location / { | ||
| + | proxy_pass http:// | ||
| + | # ... reste de votre config proxy habituelle | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||