Redis配置文件中bind参数

发布于 2021-01-26  83 次阅读


前言

我们都知道,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

翻译过来的意思就是:

默认情况下,如果没有指定bind配置指令,则 Redis 监听来自服务器上所有可用网络接口的连接。可以使用bind配置指令来监听一个或多个选定的接口,在bind后拼接一个或多个 IP 地址即可。例如:

bind 192.168.1.100 10.0.0.1
bind 127.0.0.1 ::1

警告:如果运行 Redis 的计算机直接暴露在互联网上,绑定到所有的接口是很危险的,并会将实例暴露给互联网上的每个人。因此,默认情况下,我们取消注释以下绑定指令,这将强制 Redis 只监听 IPv4 回环接口地址(这意味着 Redis 只接受来自运行它的计算机上的客户端的连接)。

如果你确定希望你的实例能够监听所有的接口,只需要注释下面的这一行即可。

bind 127.0.0.1

网络接口

注释说明 bind 的是 network interfaces,即网络接口(网卡)。服务器可以有一个或者多个网络接口。可以使用 ifconfig 查看当前 Linux 服务器上的网络接口。

$ ifconfig
docker0 Link encap:Ethernet HWaddr 3A:F3:20:12:AE:6A
 inet addr:192.168.42.1 Bcast:0.0.0.0 Mask:255.255.255.0
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:362879 errors:0 dropped:0 overruns:0 frame:0
 TX packets:894703 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:28218097 (26.9 MiB) TX bytes:1326305089 (1.2 GiB)
eth0 Link encap:Ethernet HWaddr 00:16:3E:08:18:35
 inet addr:10.25.102.37 Bcast:10.25.103.255 Mask:255.255.252.0
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:4958304 errors:0 dropped:0 overruns:0 frame:0
 TX packets:2766733 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000
 RX bytes:534516269 (509.7 MiB) TX bytes:13382719049 (12.4 GiB)
eth1 Link encap:Ethernet HWaddr 00:16:3E:08:13:6B
 inet addr:120.76.207.187 Bcast:120.76.207.255 Mask:255.255.252.0
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:13183600 errors:0 dropped:0 overruns:0 frame:0
 TX packets:14070363 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000
 RX bytes:6460933699 (6.0 GiB) TX bytes:8462002985 (7.8 GiB)
lo Link encap:Local Loopback
 inet addr:127.0.0.1 Mask:255.0.0.0
 UP LOOPBACK RUNNING MTU:65536 Metric:1
 RX packets:156288093 errors:0 dropped:0 overruns:0 frame:0
 TX packets:156288093 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:19039024606 (17.7 GiB) TX bytes:19039024606 (17.7 GiB)
veth802443e Link encap:Ethernet HWaddr 56:E8:12:D0:88:96
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:120 errors:0 dropped:0 overruns:0 frame:0
 TX packets:162 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0
 RX bytes:44625 (43.5 KiB) TX bytes:18533 (18.0 KiB)

我的服务器是阿里云的ECS,当前有5个网络接口。

网络接口说明

  • docker0 安装 docker 时自动创建的网桥
  • eth0 阿里云内网接口
  • eth1 阿里云公网接口
  • lo 本地回环接口
  • veth88f3e3c 运行 docker 容器创建的 veth pair 的一端

所以,如果要让公网可以连接该服务器上的 redis 服务,除了直接注释掉 bind 这一行来绑定到所有的网络接口之外,更正确的做法应该是不注释,再绑定多 eth1 这个公网接口,地址是 120.76.207.187。

bind 127.0.0.1 120.76.207.187

然后重启下 redis 服务即可,这样配置,Redis 就只监听 IPv4 的本地回环接口和公网接口。

原文網址:https://kknews.cc/code/y3gxvba.html


啦啦啦!