In ambienti ad alta disponibilità e con più nodi applicativi, il reverse proxy agisce da bilanciatore tra backend server. In questi casi, è fondamentale gestire la sticky session (o session affinity) per mantenere l’utente sempre sullo stesso nodo durante la durata della sessione.
Ogni backend deve identificarsi con un route ID univoco (es. xs22001) e includerlo nel cookie di sessione:
Set-Cookie: XSTORESESSION=abcdef123456.xs22001; Path=/; HttpOnly; SameSite=Lax
In Delphi, la logica potrebbe essere:
function GetRouteSuffix: string;
begin
Result := 'xs' + IntToStr(Settings.porta_http);
end;
procedure SetSessionCookie(Response: TWebResponse; SessionID, RouteSuffix: string);
begin
Response.SetCookieField(
'',
'XSTORESESSION',
SessionID + '.' + RouteSuffix,
'/',
Now + 1,
True, // Secure
True, // HttpOnly
'Lax' // SameSite
);
end;
Nel contesto della sticky session, l’attributo SameSite del cookie è cruciale:
<Proxy "balancer://xstorecluster">
BalancerMember http://10.30.0.23:22001 route=xs22001
BalancerMember http://10.30.0.23:22002 route=xs22002
ProxySet stickysession=XSTORESESSION
</Proxy>
ProxyPass "/" "balancer://xstorecluster/"
ProxyPassReverse "/" "balancer://xstorecluster/"
upstream xstorecluster {
ip_hash;
server 10.30.0.23:22001;
server 10.30.0.23:22002;
}
server {
listen 443 ssl;
server_name tecnomodelcar.com;
location / {
proxy_pass http://xstorecluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Quando si implementa sticky session, è importante assicurarsi che:
Redis è un sistema di archiviazione in-memory open source, estremamente veloce, ideale per la gestione di dati temporanei come le sessioni utente.
In alternativa alla sticky session, è possibile centralizzare le sessioni in un’istanza Redis condivisa da tutti i backend:
Vantaggi di Redis rispetto alla sticky session:
Redis richiede però più infrastruttura e attenzione alla sicurezza, ma rappresenta la scelta ottimale in ambienti enterprise o microservizi complessi.
Autore: Ivan Revelli – Synaptica