> 技术文档 > Redis 外网访问与后台运行配置_redis允许外部访问

Redis 外网访问与后台运行配置_redis允许外部访问


1. 修改 redis.conf 配置文件

✅ 允许外部访问:
bind 0.0.0.0

或者注释掉 bind 127.0.0.1(防止只监听本地)

✅ 关闭 protected-mode(不建议生产用):
protected-mode no

⚠️ 注意:这样配置后,Redis 将对所有 IP 开放访问,务必设置密码!


2. 设置访问密码(强烈推荐)

redis.conf 中加入:

requirepass yourStrongPassword123

3. 启动 Redis 后台运行

方法一:修改配置文件
daemonize yes

然后用配置文件启动:

redis-server redis.conf
方法二:命令行临时后台启动
redis-server redis.conf --daemonize yes

4. 开放服务器的端口(默认是 6379)

如果你使用的是云服务器:

  • 去控制台 安全组 添加一条规则,允许外网访问 6379 端口

如果你使用的是防火墙(例如 ufw 或 firewalld):

# 示例(Ubuntu)sudo ufw allow 6379

5. 远程连接测试

用另一台机器测试:

redis-cli -h your.server.ip -p 6379 -a yourStrongPassword123

✅ 附:查看 Redis 是否已开启监听

netstat -tlnp | grep 6379