I have 2 servers running same laravel app (laravel 5.3) and both are behind reverse proxy(nginx). for the session I set it to use redis. Also, I use cartalyst sentinel for user authentication.
let say the reverse Proxy is A, while servers for laravel are B and C.
The problem is:
When I log in A which is forwarding the request to B, and then the next request in A is forwarded to C.
When the request is forwarded to C, i can't pass the request because C see me as a guest not as an authenticate user.
how can I share session behind reverse proxy in different server?
config/cache.php
'default' => env('CACHE_DRIVER', 'redis'),
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
config/session.php
'driver' => env('SESSION_DRIVER', 'redis'),
'connection' => 'session',
config/database.php
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', '192.168.1.102'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'prefix' => env('REDIS_PREFIX', 'prod'),
],
'session' => [
'host' => env('REDIS_HOST', '192.168.1.102'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
'prefix' => env('REDIS_PREFIX', 'prod_session'),
],
update: 11 Oct 2017
I try using laravel default Auth and it's work. from my view point the problem is in cartalyst sentinel that not ready for use in horizontal scaling server.
Related
I'm trying to connect Laravel application to Redis (hosted by DigitalOcean) but without success. I'm getting the following error:
Predis\Connection\ConnectionException
Connection timed out [tls://private-db-redis-ams3-xxxxyyyyzzzz.db.ondigitalocean.com:25061]
Here is how my .env file's looking:
REDIS_HOST=db-redis-ams3-xxxxyyyyzzzz.db.ondigitalocean.com
REDIS_PASSWORD=alfjaslkdfjwlkfjlweh
REDIS_PORT=25061
And here's config/database.php
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'scheme' => 'tls',
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
I added the IP of the web server inside the Trusted Sources section in DigitalOcean's Redis Server Control Panel but it still doesn't want to connect. I tried almost every solution from Google searches. I'm stuck, every help is greatly appreciated!
I'm trying to setup Redis client to connect to the Redis server in Laravel 6.0 version, but when I try to change the REDIS_HOST in database.php file in config it gives me an error:
Connection refused 127.0.0.1:6379
This is how my code looks like in routes api.php file
Route::get('test',function(Request $request){
$redis=Redis::connection();
$redis->set("key1","keyValue");
$a=$redis->get("key1");
return response()->json( ['test'=>$a,]);
});
and this is how my database.php file looks like
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
So when i change the REDIS_HOST its gives me error,any idea is my code problem the configurations?
Thanks !
I encountered this on my MacBook and fixed it by installing Redis using homebrew as directed here
PS: I'm working with Laravel 8.40
I've setup redis-cluster on Amazon ElastiCache for cache, using Laravel 5.5 and Predis package, I get the following error.
Predis\ClientException: No connections available in the pool in vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php:337
Trace:
Predis\ClientException: No connections available in the pool in vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php:337
Stack trace:
0 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(411):
Predis\Connection\Aggregate\RedisCluster->guessNode(153)
1 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(388):
Predis\Connection\Aggregate\RedisCluster->getConnectionBySlot(153)
2 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(550):
Predis\Connection\Aggregate\RedisCluster->getConnection(Object(Predis\Command\StringSetExpire))
3 vendor/predis/predis/src/Connection/Aggregate/RedisCluster.php(593):
Predis\Connection\Aggregate\RedisCluster->retryCommandOnFailure(Object(Predis\Command\StringSetExpire),
'executeCommand')
4 vendor/predis/predis/src/Client.php(331): Predis\Connection\Aggregate\RedisCluster->executeCommand(Object(Predis\Command\StringSetExpire))
5 vendor/predis/predis/src/Client.php(314): Predis\Client->executeCommand(Object(Predis\Command\StringSetExpire))
6 vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php(96):
Predis\Client->__call('setex', Array)
7 vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php(108):
Illuminate\Redis\Connections\Connection->command('setex', Array)
8 vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php(93): Illuminate\Redis\Connections\Connection->__call('setex', Array)
9 vendor/laravel/framework/src/Illuminate/Cache/Repository.php(195): Illuminate\Cache\RedisStore->put('5rr44TBjIPEgJSx...',
'a:1:{s:6:"_flas...', 480)
10 vendor/laravel/framework/src/Illuminate/Session/CacheBasedSessionHandler.php(66):
Illuminate\Cache\Repository->put('5rr44TBjIPEgJSx...',
'a:1:{s:6:"_flas...', 480)
11 vendor/laravel/framework/src/Illuminate/Session/Store.php(128): Illuminate\Session\CacheBasedSessionHandler->write('5rr44TBjIPEgJSx...',
'a:1:{s:6:"_flas...')
12 vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(87):
Illuminate\Session\Store->save()
13 vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(218):
Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request),
Object(Illuminate\Http\Response))
14 vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(189):
Illuminate\Foundation\Http\Kernel->terminateMiddleware(Object(Illuminate\Http\Request),
Object(Illuminate\Http\Response))
15 public/index.php(58): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request),
Object(Illuminate\Http\Response))
16 {main}
Technical stack I used:
PHP7.0
Laravel5.5
OS (Debian GNU/Linux 9.6 (stretch)), Release 9.6
I tried the following configs in config/database.php:
redis=> [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_CLUSTER_HOST', 'localhost'),
'password' => env('REDIS_CLUSTER_PASSWORD', null),
'port' => env('REDIS_CLUSTER_PORT', 6379),
'database' => 0,
],
],
'cache' => [
[
'host' => env('REDIS_CLUSTER_HOST', 'localhost'),
'password' => env('REDIS_CLUSTER_PASSWORD', null),
'port' => env('REDIS_CLUSTER_PORT', 6379),
'database' => 0,
],
],
]
]
I also tried with timeout => 0|5|60 but every time I get the same error.
Any help would be highly appreciated!
This is working in in our env:
'redis' => [
'cluster' => true,
'client' => 'predis',
'options' => [
'cluster' => 'redis',
'parameters' => [
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
],
],
'clusters' => [
'default' => [
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'timeout' => 15,
],
],
],
Based on info found here: https://github.com/nrk/predis/issues/480 - but needed to also replicate all 'default' config values in the options -> parameters group
REDIS_HOST, is pointing to the configuration endpoint in AWS elasticache's redis cluster.
I think the problem is that I'm trying to switch amz cloud service by service... ANd redis can only be accessed through the vpc which I'm not in this case.
I need to test is from an ec2 inside the vpc for testing.
I am trying to use ElasticCache Redis Cluster(Cluster mode enabled not sentinel) on Laravel-5.4.32 but getting below error:
1/1) ServerException
MOVED 13491 10.0.1.199:6379
My database.php looks like below:
'redis' => [
'client' => 'predis',
'cluster' => true,
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
REDIS_HOST value is been provided using .env file.
My application works fine with single redis instance.
Below configuration worked for me:
'redis' => [
'client' => 'predis',
'options' => [
'cluster' => 'redis',
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
],
],
Actually, this is clearly mentioned in laravel documentation: https://laravel.com/docs/5.4/redis#configuration
I want to put session and cache data into redis. This is my configuration in database.php:
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '192.168.56.101',
'port' => 6379,
'database' => 0,
),
'session' => array(
'host' => '192.168.56.101',
'port' => 6379,
'database' => 1,
),
),
session.php:
return array(
'driver' => 'redis',
'connection' => 'session',
);
cache.php:
'driver' => 'redis',
However, where I write code like this:
Cache::remember('aa',1,function(){
return 'bb';
});
cache driver uses the same redis database as session driver does, which results in:
127.0.0.1:6379[1]> keys *
1) "aa"
2) "e0606244bec40b0352fb2b7b65d98049e49f6189"
Anyone knows how to force cache to use a specific redis connection? Or I have to mix them up together?
Introduction
Here is my note, for some other guy who running in to this problem, I think this is should be in the docs.
By default, redis gives you 16 separate databases, but laravel out of the box will try to use database 0 for both sessions and cache.
Our solution is to let Redis caching using database 0, and database 1 for Session, there for solving the session clear by running php artisan cache:clear problem.
Note: Tested at Laravel 5.1
1. Setting up Session Redis connection
Modify config/database.php, add session key to the redis option:
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
2. Make use of the session connection
Modify config/session.php, change the following:
'connection' => null,
to:
'connection' => 'session',
3. Using Redis as session driver
Modify .env, change SESSION_DRIVER:
SESSION_DRIVER=redis
4. Testing out
Execute the following artisan command, then check your login state:
php artisan cache:clear
If the login state persists, voilĂ !
Laravel 5.5:
database.php should look like this:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
And in the session.php you have to update also the key "connection" to the right key. In this case 'session'
'connection' => 'session',
Laravel 5 now supports this.
https://github.com/laravel/framework/commit/d10a840514d122fa638eb5baa24c8eae4818da3e
You can select redis connection by modifying config/cache.php
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'your-connection-name',
],
],
Laravel 4 CacheManager does not support selecting redis connection.
What you need to do is to modify/extend CacheManager and override createRedisDriver() method.
Modify this line
return $this->repository(new RedisStore($redis, $this->getPrefix()));
To
return $this->repository(
new RedisStore($redis, $this->getPrefix(),
$this->app['config']['cache.redis'])
);
Now you can define your configuration in cache.php
'redis' => 'your-connection-name'