redis使用日志(4):如何让外部服务器访问

开启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的话,就直接能用了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值