# Infrastruttura Docker per Web Application Java con Nginx e Let's Encrypt Questa configurazione fornisce un'infrastruttura completa per esporre una web application Java con: - Nginx come reverse proxy - Rinnovo automatico dei certificati TLS con Let's Encrypt/Certbot - Configurazione SSL/TLS sicura ## Struttura dei File ``` . ├── docker-compose.yml ├── init-letsencrypt.sh ├── nginx/ │ ├── nginx.conf │ └── conf.d/ │ └── app.conf └── README.md ``` ## Prerequisiti 1. Docker e Docker Compose installati 2. Un dominio puntato al server (record DNS A configurato) 3. Porte 80 e 443 aperte sul firewall 4. La tua applicazione Java containerizzata ## Setup Iniziale ### 1. Configura la tua applicazione Nel `docker-compose.yml`, modifica la sezione `app`: ```yaml app: image: your-java-app:latest # <-- Sostituisci con la tua immagine # ... altre configurazioni ``` ### 2. Prima configurazione dei certificati #### Opzione A: Script Automatico (Consigliato) Usa lo script di inizializzazione: ```bash # Test con certificati staging (consigliato per il primo test) ./init-letsencrypt.sh tuodominio.com tua-email@example.com 1 # Produzione (dopo aver verificato che funziona) ./init-letsencrypt.sh tuodominio.com tua-email@example.com 0 ``` #### Opzione B: Manuale 1. Modifica `nginx/conf.d/app.conf` sostituendo `example.com` con il tuo dominio 2. Avvia solo nginx: ```bash docker-compose up -d nginx ``` 3. Richiedi il certificato: ```bash docker-compose run --rm certbot certonly \ --webroot \ --webroot-path=/var/www/certbot \ --email tua-email@example.com \ --agree-tos \ --no-eff-email \ -d tuodominio.com \ -d www.tuodominio.com ``` 4. Ricarica nginx: ```bash docker-compose exec nginx nginx -s reload ``` ### 3. Avvio Completo ```bash docker-compose up -d ``` ## Rinnovo Automatico dei Certificati Il container `certbot` è configurato per: - Controllare il rinnovo dei certificati ogni 12 ore - Rinnovare automaticamente i certificati quando mancano meno di 30 giorni alla scadenza - Let's Encrypt invia email di promemoria a 20, 10 e 1 giorno dalla scadenza ### Verifica Manuale del Rinnovo ```bash # Test di rinnovo (dry-run) docker-compose run --rm certbot renew --dry-run # Forzare il rinnovo (solo se necessario) docker-compose run --rm certbot renew --force-renewal ``` ## Gestione ### Visualizza i Log ```bash # Log nginx docker-compose logs -f nginx # Log applicazione docker-compose logs -f app # Log certbot docker-compose logs certbot ``` ### Riavvio dei Servizi ```bash # Riavvia tutto docker-compose restart # Riavvia solo nginx docker-compose restart nginx # Ricarica configurazione nginx senza downtime docker-compose exec nginx nginx -s reload ``` ### Verifica Stato Certificati ```bash docker-compose run --rm certbot certificates ``` ## Configurazione Nginx ### Headers di Sicurezza La configurazione include: - HSTS (HTTP Strict Transport Security) - X-Frame-Options - X-Content-Type-Options - X-XSS-Protection - OCSP Stapling ### WebSocket Support Il proxy è già configurato per supportare WebSocket. Se la tua applicazione non li usa, puoi rimuovere: ```nginx proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; ``` ### Timeout e Buffer Configurati per applicazioni tipiche. Modifica se necessario: ```nginx proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; ``` ## Personalizzazioni per JHipster Se usi JHipster, considera: ### 1. Variabili d'Ambiente ```yaml app: environment: - SPRING_PROFILES_ACTIVE=prod - JHIPSTER_SECURITY_AUTHENTICATION_JWT_BASE64_SECRET=your-secret - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/yourdb ``` ### 2. Database PostgreSQL Aggiungi al `docker-compose.yml`: ```yaml services: db: image: postgres:15-alpine environment: - POSTGRES_USER=yourapp - POSTGRES_PASSWORD=yourpassword - POSTGRES_DB=yourdb volumes: - postgres-data:/var/lib/postgresql/data networks: - app-network volumes: postgres-data: ``` ### 3. Health Check Endpoint JHipster espone `/management/health`: ```yaml healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/management/health"] ``` ## Troubleshooting ### Certificato Non Ottenuto 1. Verifica che il DNS punti correttamente al server 2. Controlla che le porte 80 e 443 siano aperte 3. Usa la modalità staging per test (`--staging`) 4. Controlla i log: `docker-compose logs certbot` ### Nginx Non Si Avvia ```bash # Testa la configurazione docker-compose exec nginx nginx -t # Verifica i log docker-compose logs nginx ``` ### Applicazione Non Raggiungibile 1. Verifica che l'app sia in esecuzione: `docker-compose ps` 2. Controlla i log dell'app: `docker-compose logs app` 3. Verifica la connettività: `docker-compose exec nginx curl http://app:8080` ### Rate Limit Let's Encrypt Let's Encrypt ha limiti: - 50 certificati per dominio registrato alla settimana - 5 duplicati alla settimana Usa `--staging` per i test! ## Sicurezza ### Best Practices 1. **Firewall**: Chiudi tutte le porte tranne 22 (SSH), 80 (HTTP), 443 (HTTPS) 2. **Secrets**: Non committare mai secret o password nel repository 3. **Aggiornamenti**: Mantieni aggiornate le immagini Docker 4. **Monitoring**: Implementa monitoring e alerting 5. **Backup**: Configura backup regolari dei volumi ### Aggiornamento Immagini ```bash docker-compose pull docker-compose up -d ``` ## Monitoring e Manutenzione ### Check Scadenza Certificati ```bash # Mostra informazioni sui certificati docker-compose run --rm certbot certificates # Verifica SSL del sito curl -vI https://tuodominio.com 2>&1 | grep -A 10 "SSL certificate" ``` ### Log Rotation Considera l'uso di log rotation per i log di nginx: ```yaml nginx: logging: driver: "json-file" options: max-size: "10m" max-file: "3" ``` ## Supporto Per problemi o domande: - Documentazione Let's Encrypt: https://letsencrypt.org/docs/ - Documentazione Nginx: https://nginx.org/en/docs/ - Documentazione Certbot: https://certbot.eff.org/docs/