开启redis 允许外网IP 访问
在 Linux 中安装了redis 服务,当在客户端通过远程连接的方式连接时,报could not connect错误。
错误的原因很简单,就是没有连接上redis服务,由于redis采用的安全策略,默认会只准许本地访问。
需要通过简单配置,完成允许外网访问。
修改redis的配置文件,将所有bind信息全部屏蔽
# bind 192.168.1.100 10.0.0.1 # bind 192.168.1.8 # bind 127.0.0.1
修改完成后,需要重新启动redis服务
redis-server redis.conf
如果iptables 没有开启6379端口,用这个方法开启端口
命令:/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT 保存防火墙修改命令:/etc/rc.d/init.d/iptables save
通过iptables 允许指定的外网ip访问
修改 Linux 的防火墙(iptables),开启你的redis服务端口,默认是6379。
//只允许127.0.0.1访问6379 iptables -A INPUT -s 127.0.0.1 -p tcp --dport 6379 -j ACCEPT //其他ip访问全部拒绝 iptables -A INPUT -p TCP --dport 6379 -j REJECT
未配置拒绝前
配置拒绝后
也可以用下面的方式:如果只是自己做个爬虫缓存的话
# When protected mode is on and if: # # 1) The server is not binding explicitly to a set of addresses using the # "bind" directive. # 2) No password is configured. # # The server only accepts connections from clients connecting from the # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain # sockets. # # By default protected mode is enabled. You should disable it only if # you are sure you want clients from other hosts to connect to Redis # even if no authentication is configured, nor a specific set of interfaces # are explicitly listed using the "bind" directive. protected-mode no # 默认是开启保护模式的yes,当设置成no的话,就直接能用了