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 [17-10-2025 10:18] (Version actuelle) – 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 | ||
| + | |||
| + | < | ||
| + | # 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 | ||
| + | |||
| + | < | ||
| + | #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 | ||
| + | |||
| + | < | ||
| + | mmdblookup --file / | ||
| + | |||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | 6255148 < | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | 2658434 < | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | 6252001 < | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Config pour restreindre par pays | ||
| + | |||
| + | < | ||
| + | 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 | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||