skip to Main Content

We have a new Symfony setup with Redis as cache mechanism. We want to connect to a specific host, not the default localhost. On production, the ./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 config:

services.yml

services:
    Redis:
        # you can also use RedisArray, RedisCluster or PredisClient classes
        class: PredisClient
        calls:
            - connect:
                  - '%env(REDIS_HOST)%'
                  - '%env(int:REDIS_PORT)%'
    SymfonyComponentHttpFoundationSessionStorageHandlerRedisSessionHandler:
        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

2

Answers


  1. There might be hidden other .env files. May you check out the files list with the following code at the root of your projects? Are there other env files such as .env.prod, .env.local?

    ls -lah
    
    Login or Signup to reply.
  2. The Symfony’s documentation suggest to use "calls -> connect", but it’s used only when you was defined the class as ‘Redis’. When you use ‘PredisClient’, you need to use the settings bellow:

    "config/services.yaml"

        Redis:
            # you can also use RedisArray, RedisCluster or PredisClient classes
            class: PredisClient
            # 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’m also using the ‘PredisClient’ and after to change to ‘arguments’ the connection was worked here.

    For more parameters references, please check this link (List of connection parameters).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search