Kubernetes – Symfony redis sessions always change PHPSESSID

I want to use session in a redis DB with Symfony behind NGINX behind IngressNginx.

Everything is working really well BUT the PHPSESSID cookie changes on almost each request.

That’s why i added the following annotations to the ingress.

Now I have 2 PHPSESSID set. And only the one that changes (first one in the pic) seems to be used.
Multiple cookies

How can I solve this ?

KUBERNETES

ingress-nginx.yaml

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: "true"
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/affinity-mode: "persistent"
    nginx.ingress.kubernetes.io/session-cookie-name: "PHPSESSID"
    nginx.ingress.kubernetes.io/session-cookie-path: "/(.*)"
    nginx.ingress.kubernetes.io/session-cookie-samesite: "Lax"
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800000"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800000"
spec:
  tls:
    - hosts:
        - bar.foo.dev
      secretName: tls-secret
  rules:
    - host: bar.foo.dev
      http:
        paths:
          - backend:
              serviceName: bar-nginx
              servicePort: 80
            path: /(.*)

NGINX

default.conf

server {

[...]

    location ~ ^/index.php(/|$) {
        set_by_lua $upstream_host 'return os.getenv("UPSTREAM") or "php:9000"';
        fastcgi_pass $upstream_host;
        resolver local=on;

        # Increase the buffer size to handle large cache invalidation headers
        fastcgi_buffer_size 32k;
        fastcgi_buffers 32 4k;

        fastcgi_split_path_info ^(.+.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        internal;
    }

[...]
}

Symfony APP

services.yaml

[...]
    Redis:
        class: Redis
        calls:
            - connect:
                  - '%env(REDIS_HOST)%'
                  - '%env(int:REDIS_PORT)%'

    SymfonyComponentHttpFoundationSessionStorageHandlerRedisSessionHandler:
        arguments:
            - '@Redis'
            - { prefix: 'bar_phpsess_' }
[...]

packages.framework.yaml

[...]
    session:
        handler_id: SymfonyComponentHttpFoundationSessionStorageHandlerRedisSessionHandler
        cookie_secure: auto
        cookie_samesite: lax
[...]

VERSIONS:

  • PHP 7.4.12
  • Symfony 4.4
  • Kubernetes 1.17.9
  • redis (pecl) 5.3.2

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *