phpsymfonynginxdocker-swarmnginx-reverse-proxy

Symfony app in docker swarm mode with multiple replicas, session problem?


I have a problem with Symfony 7 app on production with docker swarm and nginx acting as reverse proxy.

With 1 replica everything works just fine, but with 2 or more replicas my app starts but I can't log into it. The app uses a standard Symfony approach with login form (https://symfony.com/doc/current/security.html#form-login). The application image is build from php:8.2-apache and Apache is of course the running web server inside app containers.

Here is my config (I've omitted irrelevant stuff):

swarm_stack.yml

services:
    nginx:
        image: ghcr.io/mygh/nginx:docker
        deploy:
            replicas: 1
        ports:
            - "80:80"
            - "443:443"

    backend_app:
        image: ghcr.io/mygh/backend:docker
        deploy:
            replicas: 2
        ports:
            - "8000:80"

nginx.conf

server {
    listen              80;
    listen              443 ssl;
    server_name  backendapp.com;

    location / {
        proxy_pass http://backend_app:80/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
    }
}

I'm thinking, since the login in fails, there might be a problem with sharing session between replicas / worker nodes? Or maybe the problem is in the approach itself? This is my first attempt to configure the stack in that way.


Solution

  • Ok, it was a session sharing problem. Since Nginx Plus (and its sticky session) is a 'little' too expensive for my application I went for configuring Symfony to store sessions in Redis. Works like a charm.

    Again thanks to @NicoHaase for pointing that out.