Nginx – Load balancing: Nginx, Varnish and HTTP sessions

load balancingnginxPHPsessionvarnish

I have to setup a load balancing system. At the moment I have two machines running Nginx 0.8.52, PHP-FPM (on unix socket) and an application based on PHP Symfony 1.4.11.

On a third machine, I have to choose a which balancer to install.

I've already used Varnish for static file caching, but I don't know if Varnish load balancing can handle Symfony PHP sessions.
Another option would be Nginx load balancing with ip_hash.

Which one is the best?

Thanks in advance

Best Answer

Why not both? Put NginX on the front to handle gzip compression and SSL and Varnish behind it to handle caching and load balancing. The backend application will need some session sharing on DB or Memcache as suggested by Kristaps and Varnish will not cache any dynamic content that is served with Cookies or authentication, but all the rest will be ok.

The recommended setup would be for you to be to create a separated static hostname (like static.example.com and serve all static content from there, without any cookies (Varnish will cache it fine since there are no cookies), and another one for the dynamic content that can be load balanced by NginX or Varnish, using a cookie for session handling, and all the backends sharing the session information as stated above.