Configuration Basic Auth Nginx
Mise en place de l'authentification HTTP Basic avec Nginx.
Installation htpasswd
Créer Fichier .htpasswd
Créer nouveau fichier
Attention: Le flag -c écrase le fichier existant.
Ajouter utilisateur à fichier existant
Supprimer un utilisateur
Configuration Nginx
Activer sur tout le serveur
server {
listen 443 ssl http2;
server_name example.srv759970.hstgr.cloud;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://localhost:8080;
}
}
Activer sur un location spécifique
server {
listen 443 ssl http2;
server_name example.srv759970.hstgr.cloud;
location / {
proxy_pass http://localhost:8080;
}
location /admin {
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://localhost:8080/admin;
}
}
Désactiver pour un sous-path
location / {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://localhost:8080;
}
location /public {
auth_basic off; # Désactive l'auth pour ce path
proxy_pass http://localhost:8080/public;
}
Snippet Réutilisable
Créer /etc/nginx/snippets/basic-auth.conf:
Utiliser dans les server blocks:
server {
listen 443 ssl http2;
server_name example.srv759970.hstgr.cloud;
include snippets/basic-auth.conf;
location / {
proxy_pass http://localhost:8080;
}
}
Permissions
# Sécuriser le fichier .htpasswd
chmod 640 /etc/nginx/.htpasswd
chown root:www-data /etc/nginx/.htpasswd
Vérifier le Fichier
Format: username:encrypted_password
Générer Mot de Passe Sans Interaction
Tester l'Authentification
# Sans credentials → 401
curl https://example.srv759970.hstgr.cloud
# Avec credentials → 200
curl -u admin:password https://example.srv759970.hstgr.cloud
Services Protégés sur srv759970
- Grafana: https://monitoring.srv759970.hstgr.cloud
- RQ Dashboard: https://whisperx-dashboard.srv759970.hstgr.cloud
- Dozzle: https://dozzle.srv759970.hstgr.cloud
Troubleshooting
401 Unauthorized même avec bon mot de passe
# Vérifier permissions fichier
ls -la /etc/nginx/.htpasswd
# Vérifier logs Nginx
tail -f /var/log/nginx/error.log
Pas de prompt d'authentification
Voir aussi
- Infrastructure > Security - Vue d'ensemble sécurité
- Infrastructure > Nginx - Configuration Nginx globale