Redis配置文件中bind参数

前言

我们都知道,redis 的配置文件中,默认绑定接口是 127.0.0.1,也就是本地回环接口,所以是无法从外网连接 redis 服务的。如果想要让外网也能连接使用服务器上的 redis 服务,可以简单地注释掉 bind 这一行。但对于 bind 参数的作用,网上有很多文章的解释都是误人子弟的。

关于bind

翻看网上的文章,此处多翻译为:

指定 redis 只接收来自于该 IP 地址的请求,如果不进行设置,那么将处理所有请求,在生产环境中最好设置该项。
这种解释会搞糊涂初学者,甚至是错误的。查看配置文件 redis.conf,可以看到很详细的注释说明。

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected 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 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bind 127.0.0.1

请认真阅读上述英文注释

正确做法

redis部署在本机

执行ifconfig中获取到本机ip (或者hostname -I)

redis.conf配置为bind 127.0.0.1 本机ip

redis部署在docker中

进入redis所在的docker容器

执行ifconfig中获取到本机ip (或者hostname -I)

redis.conf配置为bind 127.0.0.1 本机ip

参考文章

Redis配置文件中bind参数 https://bingozb.github.io/62.html

posted @ 2019-11-26 20:14  Draymonder  阅读(...)  评论(... 编辑 收藏