Skip to Content
TecnicaInfrastruttura🔧 FIX NGINX per Cookie Authentication

🔧 FIX NGINX per Cookie Authentication

🚨 Problema Trovato

Il log mostra:

URL: https://localhost:4000/api/auth/login Host: foodcostapp.meating3srl.it

Il problema: Next.js vede localhost:4000 internamente, ma il browser vede foodcostapp.meating3srl.it.

Quando il cookie viene impostato, il browser lo associa a localhost invece di foodcostapp.meating3srl.it, quindi non lo invia nelle richieste successive.


✅ Soluzione Immediata (già implementata nel codice)

Ho cambiato il codice per:

  1. ✅ Rimuovere il doppio Set-Cookie (stava duplicando)
  2. ✅ Usare solo response.headers.set() per controllo completo
  3. ✅ Non specificare il dominio (il browser userà l’host della richiesta)

Fai il deploy e riprova.


🔧 Fix Nginx (Configurazione Server)

Il tuo Nginx deve passare correttamente gli headers al backend Next.js.

Trova il file di configurazione

# Possibili posizioni: ls /etc/nginx/sites-available/foodcost* ls /etc/nginx/conf.d/foodcost*

Configurazione Corretta Nginx

server { listen 443 ssl http2; server_name foodcostapp.meating3srl.it; # SSL certificates ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { # Proxy al backend Next.js proxy_pass http://localhost:4000; # HEADERS CRITICI per i cookie proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port; # Passa i cookie proxy_pass_header Set-Cookie; proxy_pass_header Cookie; # NO cookie rewrite se usiamo lo stesso dominio # proxy_cookie_domain localhost $host; # Timeout proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; } } # Redirect HTTP to HTTPS server { listen 80; server_name foodcostapp.meating3srl.it; return 301 https://$server_name$request_uri; }

Verifica Headers Nginx

Dopo aver modificato:

# Test della configurazione sudo nginx -t # Se OK, ricarica sudo systemctl reload nginx # Verifica log Nginx tail -f /var/log/nginx/access.log tail -f /var/log/nginx/error.log

🧪 Test dopo il Fix

1. Verifica gli headers che Nginx passa

Aggiungi temporaneamente nel tuo Nginx:

location /api/auth/login { proxy_pass http://localhost:4000; # ... altri proxy_set_header ... # DEBUG: log degli headers access_log /var/log/nginx/auth-debug.log; }

2. Test con cURL

# Test dal server curl -X POST https://foodcostapp.meating3srl.it/api/auth/login \ -H "Content-Type: application/json" \ -d '{"email":"enniocecco@gmail.com","password":"PASSWORD"}' \ -v 2>&1 | grep -i "set-cookie"

Dovresti vedere:

< Set-Cookie: auth-token=...; Path=/; HttpOnly; Secure; SameSite=Lax

Nota: NON dovrebbe esserci Domain=localhost !

3. Test nel browser

  1. Fai login
  2. DevTools > Network > POST /api/auth/login
  3. Response Headers → cerca Set-Cookie
  4. Verifica che NON contenga Domain=localhost

📋 Checklist Completa

  • Nginx ha proxy_set_header Host $host
  • Nginx ha proxy_set_header X-Forwarded-Proto $scheme
  • Nginx ha proxy_pass_header Set-Cookie
  • Deploy del nuovo codice (senza doppio Set-Cookie)
  • Restart PM2: pm2 restart foodcost
  • Reload Nginx: sudo systemctl reload nginx
  • Test login e verifica log
  • Cookie visibile in DevTools
  • Cookie inviato in richiesta /api/auth/me

🎯 Verifica Risultato

Dopo il fix, nei log dovresti vedere:

[LOGIN] Request info: { "url":"https://foodcostapp.meating3srl.it/api/auth/login", ← NON localhost! "host":"foodcostapp.meating3srl.it", "protocol":"https" } [LOGIN] Cookie domain info: { "host":"foodcostapp.meating3srl.it", "domain":"foodcostapp.meating3srl.it" } [AUTH/ME] Request info: { "cookieHeader":"auth-token=..." ← NON "NONE"! } [AUTH/ME] Cookie check { "authTokenPresent":true ← SUCCESS! }

🆘 Se ancora non funziona

Prova la soluzione drastica - forza il dominio nel cookie:

// In src/app/api/auth/login/route.ts const cookieValue = `auth-token=${token}; Domain=foodcostapp.meating3srl.it; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=${ 60 * 60 * 24 * 7 }`;

Attenzione: Questo rende il cookie funzionante SOLO suquel dominio specifico.

Last updated on