search

Home  >  Q&A  >  body text

Symfony Redis cannot connect to the host defined in the env file, which defaults to localhost

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粉022501495P粉022501495241 days ago531

reply all(1)I'll reply

  • P粉399090746

    P粉3990907462024-04-07 09:53:53

    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) .

    reply
    0
  • Cancelreply