sudo apt install nginx python3-certbot-nginx python3-certbot-dns-ovh systemctl enable nginx.servicecertbot renew avec : systemctl enable certbot.timersystemctl status certbot.timerInstaller le point d'entrée permettant d'accéder au status de Nginx :
nginx -V 2>&1 | grep -o with-http_stub_status_modulevi /etc/nginx/conf.d/status.conf avec le contenu : server { listen 9090; location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; # Autoriser le réseau Docker : allow 172.18.5.0/24; deny all; } }
nginx -t && nginx -s reloadcurl 127.0.0.1:9090/nginx_statusConfigure les logs avec maintient sur 1 an (obligation légale) :
vi /etc/logrotate.d/nginxrotate 14 par rotate 400 dateext
dateyesterday
dateformat .%Y-%m-%d
vi /etc/crontab0 0 (par défaut, c'est 25 6)Modifier les logs d'accès (ajout d'infos) pour Telegraf et GoAccess :
vi /etc/nginx/nginx.conf et remplacer dans la section http {…} contenant :access_log /var/log/nginx/access.log;
# Enabling request time log_format enhanced-fmt '$remote_addr $host $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'rt="$request_time" uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time" ' 'gzr="$gzip_ratio" '; access_log /var/log/nginx/access.log enhanced-fmt;
ATTENTION : vérifier la présence du nom de domaine de l'hôte virtuel ($host) dans les logs d’accès.
Activer la compression Gzip du contenu renvoyé par Nginx pour tous les types Mime (JS, CSS…) :
vi /etc/nginx/nginx.conf et remplacer la section Gzip (qui ne contient que gzip on;) par : #+-------------------------------------------------------------------+# # Gzip Settings gzip on; gzip_comp_level 5; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/vnd.ms-fontobject application/wasm application/x-web-app-manifest+json application/xhtml+xml application/xml font/eot font/otf font/ttf image/bmp image/svg+xml text/cache-manifest text/calendar text/css text/javascript text/markdown text/plain text/xml text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
vi /etc/nginx/nginx.conf) soit dans la section server du fichier de configuration d'un domaine : client_max_body_size 12M;
nginx -V 2>&1 | grep -o with-http_geoip_moduleGeoIP2 existe dans le paquet pour la version Debian 11 Bullseye.apt install libnginx-mod-http-geoip2apt install apache2-utilshtpasswd -c /etc/nginx/.htpasswd <user-name-1>-c) : htpasswd /etc/nginx/.htpasswd <user-name-2>server ou location : satisfy any; allow <ip-v4-instance>; deny all; auth_basic "Zone restreinte"; auth_basic_user_file /etc/nginx/.htpasswd;
ssh admin@web-paca-sinp mkdir ~/dwl cd ~/dwl git clone https://github.com/perusio/nginx_ensite.git cd nginx_ensitesudo make installnginx_dissite et nginx_ensitesudo service nginx reload vi /etc/fail2ban/jail.d/defaults-debian.conf systemctl restart fail2ban.servicebind() to 172.18.5.1:9090 failed (99: Cannot assign requested address) et cela empêche Nginx de démarrer. Il faut donc le lancer manuellement : systemctl start nginx. L'erreur était due au fichier /etc/nginx/conf.d/status.conf qui contenait une ligne listen 172.18.5.1:9090; . Cette ligne n'est finalement pas utile car il suffit d'écouter sur le port 9090 avec la commande listen 9090; les paramètres allow <…> ; suffisent à limiter l'accès. Le port est bien accessible sur 127.0.0.1 comme sur 172.18.5.1 (pour un accès dans un container Docker).
systemctl edit nginx vi /etc/systemd/system/nginx.service.d/override.conf [Unit] Description=The nginx HTTP and reverse proxy server (overrided) After=network.target remote-fs.target nss-lookup.target network-online.target docker.service Wants=network-online.target
network-online.target permet à Nginx d'attendre que le réseau soit démarré.docker.service dans After=… indique à Nginx que le service Docker doit être démarré. systemctl daemon-reload systemctl restart nginx systemctl status nginx systemctl status nginx D'une manière générale la démarche à suivre pour créer un certificat SSL chez Letsencrypt à l'aide de Certbot :
certbot --nginx -d <domaine-principal> -d <alias-du-domaine-principal> certbot --nginx -d expert.silene.eu -d geonature.silene.eu server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name <domaine-principal>; root /chemine/vers/dossier/racine/html; # Exemple pour une API Python utilisant Gunicorn location ^~ "/api/" { proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # Set timeout like Gunicorn in API proxy_read_timeout 300s; proxy_connect_timeout 75s; proxy_pass http://127.0.0.1:8000/;# ATTENTION : bien mettre un slash final ! Sinon => 404 } ssl_certificate /etc/letsencrypt/live/<domaine-principal>/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/<domaine-principal>/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name <alias-domaine-principal>; ssl_certificate /etc/letsencrypt/live/<domaine-principal>/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/<domaine-principal>/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot return 302 https://<alias-domaine-principal>$request_uri; } server { listen 80; listen [::]:80; server_name <domaine-principal> <alias-domaine-principal>; return 302 https://<domaine-principal>$request_uri; }
Si vous souhaitez supprimer une certificat SSL créé par l'intermédiaire de Certbot, utiliser la commande : sudo certbot delete --cert-name <domaine>.<ext>