<p>Having an app running perfectly on my local with docker.</p>
<p>When I deploy it, I can't get why a new session is started on each request. It seems it can't come from the code since it's exactly the same.</p>
<p>The session is stored on a redis DB (I have the same bug if I use the filesystem session). It's there I can see all the new session created. (see last code block).</p>
<p>From the log I can clearly identify that the <code>$request-&gt;getSession()-&gt;getId()</code> changes on each request but not the PHPSESSID cookie.</p>
<p>For example:</p>
<p><em>First request</em></p>
<pre><code>[2020-11-02 15:03:59] request.INFO: Matched route &quot;app_login&quot;. {&quot;route&quot;:&quot;app_login&quot;,&quot;route_parameters&quot;:{&quot;_route&quot;:&quot;app_login&quot;,&quot;_controller&quot;:&quot;App\\Controller\\SecurityController::login&quot;},&quot;request_uri&quot;:&quot;https://foo.bar.dev/login&quot;,&quot;method&quot;:&quot;POST&quot;} []
[2020-11-02 15:03:59] security.DEBUG: Checking for guard authentication credentials. {&quot;firewall_key&quot;:&quot;main&quot;,&quot;authenticators&quot;:1} []
[2020-11-02 15:03:59] security.DEBUG: Checking support on guard authenticator. {&quot;firewall_key&quot;:&quot;main&quot;,&quot;authenticator&quot;:&quot;App\\Security\\LoginFormAuthenticator&quot;} []
[2020-11-02 15:03:59] app.DEBUG: [LoginFormAuthenticator::supports] $request session id =&gt; 6491ddf4e8f3e2eaa22b44b3a98c094a [] []
[2020-11-02 15:03:59] app.DEBUG: [LoginFormAuthenticator::supports] $_COOKIE =&gt;  {&quot;PHPSESSID&quot;:&quot;87cf6185b652f8d713c45031ebe6d8a4&quot;} []
</code></pre>
<p><em>Second one</em></p>
<pre><code>[2020-11-02 15:04:33] request.INFO: Matched route &quot;app_login&quot;. {&quot;route&quot;:&quot;app_login&quot;,&quot;route_parameters&quot;:{&quot;_route&quot;:&quot;app_login&quot;,&quot;_controller&quot;:&quot;App\\Controller\\SecurityController::login&quot;},&quot;request_uri&quot;:&quot;https://foo.bar.dev/login&quot;,&quot;method&quot;:&quot;POST&quot;} []
[2020-11-02 15:04:33] security.DEBUG: Checking for guard authentication credentials. {&quot;firewall_key&quot;:&quot;main&quot;,&quot;authenticators&quot;:1} []
[2020-11-02 15:04:33] security.DEBUG: Checking support on guard authenticator. {&quot;firewall_key&quot;:&quot;main&quot;,&quot;authenticator&quot;:&quot;App\\Security\\LoginFormAuthenticator&quot;} []
[2020-11-02 15:04:33] app.DEBUG: [LoginFormAuthenticator::supports] $request session id =&gt; 41b08dac8a803337a48dca7d5b33b840 [] []
[2020-11-02 15:04:33] app.DEBUG: [LoginFormAuthenticator::supports] $_COOKIE =&gt;  {&quot;PHPSESSID&quot;:&quot;87cf6185b652f8d713c45031ebe6d8a4&quot;} []
</code></pre>
<p><em><strong>KUBERNETES</strong></em></p>
<p><strong>ingress-nginx.yaml</strong></p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: secured-front
  namespace: foo-apis-dev
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/affinity: &quot;cookie&quot;
    nginx.ingress.kubernetes.io/affinity-mode: &quot;persistent&quot;
    nginx.ingress.kubernetes.io/session-cookie-name: &quot;PHPSESSID&quot;
    nginx.ingress.kubernetes.io/session-cookie-path: &quot;/&quot;
    nginx.ingress.kubernetes.io/session-cookie-samesite: &quot;Lax&quot;
    nginx.ingress.kubernetes.io/session-cookie-expires: &quot;172800000&quot;
    nginx.ingress.kubernetes.io/session-cookie-max-age: &quot;172800000&quot;
spec:
  tls:
    - hosts:
        - bar.foo.dev
      secretName: tls-secret
  rules:
    - host: bar.foo.dev
      http:
        paths:
          - backend:
              serviceName: bar-nginx
              servicePort: 80
            path: /(.*)
</code></pre>
<p><em><strong>Symfony APP</strong></em></p>
<p><strong>security.yaml</strong></p>
<pre><code>[...]
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: lazy
            pattern: ^/.*
            logout:
                path: app_logout
                target: login

            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
[...]
</code></pre>
<p><strong>services.yaml</strong></p>
<pre><code>[...]
    Redis:
        class: Redis
        calls:
            - connect:
                  - '%env(REDIS_HOST)%'
                  - '%env(int:REDIS_PORT)%'

    Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
        arguments:
            - '@Redis'
            - { prefix: 'admin_phpsess_' }
[...]
</code></pre>
<p><strong>packages.framework.yaml</strong></p>
<pre><code>[...]
    session:
        handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
        cookie_secure: auto
        cookie_samesite: lax
[...]
</code></pre>
<p><strong>REDIS</strong></p>
<pre><code>127.0.0.1:6379&gt; KEYS *admin*
1) &quot;admin_phpsess_245e4a79fe35e2320943770061884c24&quot;
2) &quot;admin_phpsess_0ff29464322b3c2cfc5d8f5fd323ef75&quot;
3) &quot;admin_phpsess_26812c17f93a5d28a71853b77ac85386&quot;
4) &quot;admin_phpsess_7fbae6f0b1fdbe9576e41c9eee2cd60f&quot;
</code></pre>
<p>VERSIONS:</p>
<ul>
<li>PHP 7.4.12</li>
<li>Symfony 4.4</li>
<li>Kubernetes 1.17.9</li>
<li>redis (pecl) 5.3.2</li>
</ul>
<p><strong>IMPORTANT NOTE</strong></p>
<p>The issue was about the redis configuration.</p>
<p>I took the step of using <code>PdoSessionHandler</code> and it works. The problem comes from Redis and / or Kubernetes, I have been trying for 2 hours to point out the configuration which generates this bug but impossible for now.</p>

<p>Try using <a href="https://github.com/predis/predis" rel="nofollow noreferrer">Predis</a> session handler instead.</p>
<ol>
<li>Setup your services</li>
</ol>
<pre class="lang-yaml prettyprint-override"><code># services.yaml
    Redis:
        class: \Predis\Client
        arguments:
            - 'redis://%env(REDIS_HOST)%:%env(REDIS_PORT)%'

    predis_session_handler:
        class: 'Predis\Session\Handler'
        arguments:
            - '@Redis'
</code></pre>
<ol start="2">
<li>Configure Symfony</li>
</ol>
<pre class="lang-yaml prettyprint-override"><code># framework.yaml
framework:
  session:
    handler_id: predis_session_handler
</code></pre>
