Adding Session Persistence for NGINX

If you're performing load balancing of a web application, ensure that the same backend server that handled inbound requests continues to do so for the same source. This configuration is important in cases where a website or web service must preserve log-in sessions between requests, cancel an existing request, or monitor the progress of large backend transactions.

  1. Configure session persistence by adding the ip-hash directive to the server group configuration.

    To achieve this behavior, activate the IP hash method for NGINX by specifying the ip_hash directive as part of the server group configuration, for example:

    upstream backend {
        ip_hash;
        server server1.example.com;
        server server2.example.com;
        server server3.example.com;
    }
  2. Reload the NGINX systemd service.

    Reload NGINX to apply the new configuration:

    sudo systemctl reload nginx