仅允许来自本地主机的 Redis 连接?

Allow Redis connections from only localhost?

我在我的网络服务器 (Debian/Nginx/Gunicorn) 上 运行ning Redis 用于会话存储,并且有理由相信我的 Redis 服务器被黑了。这绝对是可能的,因为如果我 运行 在另一台机器上对 Web 服务器执行命令 "redis-cli -h (HOST IP)",我可以进入控制台和 运行 命令。我有两个问题。首先,如果我如下所示向我的 iptables 文件添加一个新部分,我是否会正确地阻止除网络服务器本身之外的所有机器访问我的 Redis 服务器? Redis 运行正在默认端口 6379 上运行。

*filter

-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT

# Allow pings, SSH, and web access
-A INPUT -p icmp -m state --state NEW --icmp-type 8 -j ACCEPT
-A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
-A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
-A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT

# NEW SECTION...
# IS THIS CORRECT?
-A INPUT -p tcp --dport 6379 -j DROP
-A INPUT -p tcp -s 127.0.0.1 --dport 6379 -m state --state NEW -j ACCEPT
# END NEW SECTION

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

其次,如果以上是正确的,我还能在我的iptables的IPv6版本中使用127.0.0.1还是需要使用“::1”?

谢谢。

您应该可以通过 Redis configuration file:

# By default Redis listens for connections from all the network interfaces  
# available on the server. It is possible to listen to just one or multiple 
# interfaces using the "bind" configuration directive, followed by one or 
# more IP addresses. 
# 
# Examples: 
# 
# bind 192.168.1.100 10.0.0.1 
# bind 127.0.0.1

修改redis.conf文件:

绑定 127.0.0.1 ==> redis instanse 将只接受来自 localhost

的连接

绑定 127.0.0.1 xxx.xx.xx.xxx ==> 如果你想接受来自服务器的连接,请添加服务器的 ip

#bind 127.0.0.1 ==> 注释此行将使 redis 从任何网络接口侦听