跨服务器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.
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.confor/etc/redis/6379.conf):- Find the line
bind 127.0.0.1and change it to eitherbind 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.
- Find the line
- 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
- For Ubuntu/Debian (using ufw):
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-redisbackend:pip install django-redis - Modify your
settings.pyfile to update theCACHESconfiguration:- 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, } } } }
<YOUR_REDIS_SERVER_IP>with the actual public/private IP of your Redis cache server (e.g.,10.0.2.10for a private network). - Basic configuration (without password):
To make sure everything is working, verify the connection between Django and Redis:
Open the Django shell:
python manage.py shellRun 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_cacheThis should return the same value if the cache is working across nodes.
- 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
LOCATIONtorediss://<REDIS_SERVER_IP>:6380/1and add SSL-related options inCACHES). - Monitor Redis: Keep an eye on Redis performance and connection counts using tools like
redis-cli info stats.
内容的提问来源于stack exchange,提问作者Lisa

