You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨服务器Redis缓存配置:如何向Django应用传递Redis服务器IP

Hey Lisa, great question! Setting up Redis as a cross-server cache for your Django app is totally manageable—let’s break down the steps you need to follow to get it working reliably.

1. First: Prepare Your Remote Redis Server

By default, Redis only accepts local connections, so you’ll need to tweak its config to allow your Django app server to connect:

  • Edit the Redis configuration file (usually located at /etc/redis/redis.conf or /etc/redis/6379.conf):
    • Find the line bind 127.0.0.1 and change it to either bind 0.0.0.0 (allows connections from any IP, not recommended for production) or specifically your app server’s IP (e.g., bind 192.168.1.50).
    • Set protected-mode no (you can re-enable this later if you set a strong password).
    • Optional but highly recommended: Add a password to secure Redis with requirepass your_strong_unique_password.
  • Restart Redis to apply changes:
    sudo systemctl restart redis
    
  • Update firewall rules on your Redis server to allow incoming traffic on port 6379 (Redis’s default port):
    • For Ubuntu/Debian (using ufw):
      sudo ufw allow from <YOUR_APP_SERVER_IP> to any port 6379
      sudo ufw reload
      
    • For CentOS/RHEL (using firewalld):
      sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="<YOUR_APP_SERVER_IP>" port port="6379" protocol="tcp" accept' --permanent
      sudo firewall-cmd --reload
      
2. Configure Your Django App to Use Remote Redis

Next, set up your Django project to connect to the remote Redis server:

  • Install the required package: Django doesn’t have built-in Redis support, so use the popular django-redis backend:
    pip install django-redis
    
  • Modify your settings.py file to update the CACHES configuration:
    • Basic configuration (without password):
      CACHES = {
          'default': {
              'BACKEND': 'django_redis.cache.RedisCache',
              'LOCATION': 'redis://<YOUR_REDIS_SERVER_IP>:6379/1',  # The /1 refers to Redis database number (0-15)
              'OPTIONS': {
                  'CLIENT_CLASS': 'django_redis.client.DefaultClient',
              }
          }
      }
      
    • Configuration with password (use this if you set a Redis password):
      CACHES = {
          'default': {
              'BACKEND': 'django_redis.cache.RedisCache',
              'LOCATION': 'redis://<YOUR_REDIS_SERVER_IP>:6379/1',
              'OPTIONS': {
                  'CLIENT_CLASS': 'django_redis.client.DefaultClient',
                  'PASSWORD': 'your_strong_unique_password',
                  # Optional: Add connection timeouts and pool settings for better reliability
                  'CONNECTION_POOL_KWARGS': {
                      'max_connections': 100,
                      'socket_timeout': 5,
                      'socket_connect_timeout': 5,
                  }
              }
          }
      }
      
    Replace <YOUR_REDIS_SERVER_IP> with the actual public/private IP of your Redis cache server (e.g., 10.0.2.10 for a private network).
3. Test the Connection

To make sure everything is working, verify the connection between Django and Redis:

  • Open the Django shell:

    python manage.py shell
    
  • Run these commands to set and retrieve a test cache key:

    from django.core.cache import cache
    # Set a key that expires in 60 seconds
    cache.set('test_remote_cache', 'Hello from Django!', timeout=60)
    # Retrieve the key
    print(cache.get('test_remote_cache'))
    

    If you see Hello from Django! printed, the connection is successful.

  • You can also verify directly on the Redis server by running:

    redis-cli -h <YOUR_REDIS_SERVER_IP> get test_remote_cache
    

    This should return the same value if the cache is working across nodes.

4. Production Best Practices
  • Restrict firewall access: Only allow your app server’s IP to connect to Redis port 6379 (avoid opening it to the entire internet).
  • Use Redis with SSL: For production, configure Redis to use SSL encryption (change the LOCATION to rediss://<REDIS_SERVER_IP>:6380/1 and add SSL-related options in CACHES).
  • Monitor Redis: Keep an eye on Redis performance and connection counts using tools like redis-cli info stats.

内容的提问来源于stack exchange,提问作者Lisa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:10:16