Nginx – Session Persistence with Nginx as Reverse Proxy

nginx

I am using NGINX as a reverse proxy with two Apache Servers as upstream servers.

Both of the Apache Server has custom PHP application installed and share same MYSQL Database.

I believe Apache stores sessions in /var/lib/sessions.

If I am using NGINX, how to overcome the Session related issue ? I understand the ip_hash; but is that the only feasible way ?

Was wondering on how people solve this issue on their server side ?

Best Answer

Strictly speaking, this has nothing to do with nginx, this is a task to provide users a consistent authentication/authorizing system across multiple backends. Instead of nginx you can use any load-balancing engine.

Standard approach:

  • use memcached to store sessions (although this isn't recommended by memcache authors, this method is largely used, and even PHP has standard support for it - you need to uncomment one line in php.ini)
  • use redis for same purpose
  • recent versions of MySQL provide memcached-identical interface to store sessions in MySQL (this time they will be disk-backed)- use whatever you like for this, the only thing - it should be a centralized storage