title: Multiple PHP Web Servers to Share a Single Redis Server for User Sessions
body: <p>I have 2 web servers running PHP and Nginx... And I have 1 Redis Server. I want the web servers to use the Redis database for user login data. This will allow me to scale out the web servers without forcing logged-in users to re-authenticate if/when they hit a new server.</p>
  
  <p>Both PHP/Nginx servers are using Redis for session handling, and they both point to the same Redis database. When I visit either site in my browser it generates a session token and stores it in Redis (creating two Redis keys total).... <strong>But what I want is to create a new session when I visit Site #1, store the session in Redis, and have Site #2 recognize me when I visit because it queries Redis (so only one Redis key total).</strong></p>
  
  <p>All 3 of my servers are in AWS, in the same VPC. I'm using internal IP addresses for communication b/w the web servers and Redis (so there is some amount of trust b/w the machines).</p>
  
  <p>I'm obviously missing something here, and it might be the load balancer (which I will implement). But I was attempting to develop a Redis-sharing design with the minimal number of components. Also, I'm not entirely sure the load balancer helps w/ this issue.</p>
  
  <p>Here are the relevant portions of code:</p>
  
  <p><strong><em>PHP/Nginx Servers: (x2)</em></strong></p>
  
  <p><strong>(1) /etc/php/7.2/fpm/php.ini</strong></p>
  
  <pre><code>session.save_handler = redis
  session.save_path = "tcp://10.xxx.xx.xxx:6379" // both web servers point to the same Redis IP address
  </code></pre>
  
  <p><strong>(2) /var/www/html/test.php</strong></p>
  
  <pre><code>&lt;?php 
  
  ini_set('session.save_handler', 'redis'); // redundant; including for completeness
  ini_set('session.save_path', 'tcp://10.xxx.xx.xxx:6379'); // redundant; including for completeness
  
  session_name('FOOBAR');
  session_start();
  echo nl2br(
      '&lt;pre&gt;' . session_save_path() . '&lt;/pre&gt;' . PHP_EOL
  );
  
  echo nl2br(
      'Running PHP version: ' . phpversion() . PHP_EOL
  );
  
  if (!array_key_exists('visit', $_SESSION)) {
      $_SESSION['visit'] = 0;
  }
  $_SESSION['visit']++;
  
  echo nl2br(
      'You have been here ' . $_SESSION['visit'] . ' times.'
  );
  </code></pre>
  
  <p><strong><em>Redis Server: (x1)</em></strong></p>
  
  <p><strong>(3) /etc/redis/redis.conf</strong></p>
  
  <pre><code>bind 0.0.0.0
  </code></pre>
  
  <p>When I hit the <em>test.php</em> page for both sites, they successfully generate and store their own session in the Redis database. And they correctly increment their respective session token... But I want these web servers to share the Redis session data b/w them, so they "remember" all logged-in users (even if it's the user's first time hitting a given web server).</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> I also attempted to manually query Redis for logged-in user data. This yielded similar results to <em>test.php</em> above. Each website ended up creating and managing its own session token.</p>
  
  <p><strong><em>PHP/Nginx Servers: (x2)</em></strong></p>
  
  <p><strong>(4) /var/www/html/redis-check.php</strong></p>
  
  <pre><code>&lt;?php
  
     $cookie = "PHPREDIS_SESSION:" . $_COOKIE['FOOBAR'];
  
     // Connecting to Redis server on localhost 
     $redis = new Redis(); 
     $redis-&gt;connect('10.xxx.xx.xxx', 6379); 
     echo "Connection to server sucessfully";
     echo '&lt;br /&gt;';
  
     // Check if cookie value exists in Redis 
     $seenuser = $redis-&gt;get($cookie);
  
     if( !empty($seenuser) )
     {
        echo 'Hello, old friend!&lt;br /&gt;';
        echo '&lt;br /&gt;';
        echo 'Cookie Value: ' . $cookie;
        echo '&lt;br /&gt;';
        echo 'Session Value: ' . $seenuser;
     }
     else
     {
        // This is a new user, so create a new session
        ini_set('session.save_handler', 'redis'); // redundant; including for completeness
        ini_set('session.save_path', 'tcp://10.xxx.xx.xxx:6379'); // redundant; including for completeness
        session_name('FOOBAR');
        session_start();
  
        if (!array_key_exists('visit', $_SESSION))
        {
            $_SESSION['visit'] = 0;
        }
        $_SESSION['visit']++;
  
        $cookie = "PHPREDIS_SESSION:" . $_COOKIE['FOOBAR'];
        $seenuser = $redis-&gt;get($cookie);
  
        echo 'New Login&lt;br /&gt;';
        echo '&lt;br /&gt;';
        echo 'Cookie Value: ' . $cookie;
        echo '&lt;br /&gt;';
        echo 'Session Value: ' . $seenuser;
     }
  </code></pre>