Symfony Redis cannot connect to the host defined in the env file, which defaults to localhost
P粉022501495
P粉022501495 2024-04-06 10:53:02
0
1
503

We have a new Symfony setup with Redis as the caching mechanism. We want to connect to a specific host, not the default localhost. In production, ./bin/console debug:dotenv gives the correct REDIS_HOST. This is configured in our .env and .env.local.php.

The error we get is:

Connection refused: tcp:127.0.0.1/6379

This is our configuration:

services.yml

services:
    Redis:
        # you can also use \RedisArray, \RedisCluster or \Predis\Client classes
        class: \Predis\Client
        calls:
            - connect:
                  - '%env(REDIS_HOST)%'
                  - '%env(int:REDIS_PORT)%'
    Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
        arguments:
            - '@Redis'
            - prefix: sp_ss_
            - ttl: 1800

cache.yml

framework:
    cache:
        app: cache.adapter.redis
        default_redis_provider: 'Redis'
        pools:
            site.cache:
                adapter: cache.app

And our .env file:

APP_ENV=prod
APP_SECRET=****
MESSENGER_TRANSPORT_DSN=redis://redis.local:6379/messages
REDIS_HOST=redis.local
REDIS_PORT=6379
REDIS_URL=redis://redis.local:6379

P粉022501495
P粉022501495

reply all(1)
P粉399090746

Symfony's documentation recommends using "calls -> connect", but only use it if you define your class as "Redis". When you use '\Predis\Client' you need to use the following settings:

"config/services.yaml"

    Redis:
        # you can also use \RedisArray, \RedisCluster or \Predis\Client classes
        class: \Predis\Client
        # See all parameters here: https://github.com/predis/predis/wiki/Connection-Parameters#list-of-connection-parameters
        arguments:
            - host: '%env(REDIS_HOST)%'
            - port: '%env(int:REDIS_PORT)%'
            # uncomment the following if your Redis server requires a password
            # - password: '%env(REDIS_PASSWORD)%'

I also used "\Predis\Client", after changing to "arguments" the connection worked here.

For more parameter reference, please check this link (Connection parameter list) .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template