redis的安全配置-bind

################################ GENERAL  #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis.pid

# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511

# 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
bind 192.168.1.100

这是来自redis配置文件中的一段信息。
bind上面的英文注释翻译大概是:

默认情况下,Redis侦听服务器上所有可用网络接口的连接。
也可以只听一个或多个。
使用“bind”配置指令的接口,后面跟一个更多的IP地址。

在redis.conf配置文件中,通过注释我们可以知道,通过bind来指定一个或者多个ip,多个ip由空格分隔。

错误认识:
一直误以为是要绑定连接发起方的网卡ip,

试验并查阅资料后深刻认识:
这个bind是绑定的意思,是监听的作用,绑定的是redis服务器本机的网卡ip。一个服务主机可以有多个网卡ip,即使是该服务器只有一个网卡,也会默认有一个127.0.0.1的网卡ip和一个路由分配的192.168.1.*内网ip。而127.0.0.1是本机才能访问,192.168.1.*是同一个局域网内的所有主机都可以访问。
1、仅本机访问
例如我们想要redis只可以自己访问,不被其他机器访问,可以这样设置

bind 127.0.0.1

2、局域网可访问
如果我们希望被局域网的都可以访问,(假设192.168.1.100就是这个redis服务器的网卡ip)。此处比较容易犯错,我之前是以为绑定的是对方的ip,就配置了一连串的其他局域网的ip,其实是错误的。真正的配置如下:

bind 192.168.1.100

或者

bind 127.0.0.1 192.168.1.100

bind 192.168.1.100的配置方式的话,使用./redis-cli是连接不上的,./redis-cli默认是./redis-cli -h 127.0.0.1 -p 6379
在bind的配置中没有监听此ip,所有报错Could not connect to Redis at 127.0.0.1:6379: Connection refused
3、以太网可访问
最后,如果redis服务器是在外网环境下的,绑定了一个公网ip(例如:120.36.58.94),希望同时能被局域网的访问,外网的也能访问(有些风险),则需要这样配置

bind 192.168.1.100 120.36.58.94

或者

bind 0.0.0.0

总而言之,绑定的是redis服务器的ip,与redis客户端没有任何关系。
redis服务端绑定了什么ip,客户端就需要遵守redis服务器的规则,通过它指定的ip进行连接访问即可。如果redis客户端发起的访问没有用到指定的ip,那么redis服务器不能成功监听到,即使这个ip也属于这个服务主机的网卡ip(针对多网卡服务主机)。

猜你喜欢

转载自blog.csdn.net/zhaoxichen_10/article/details/85000309