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

請認真閱讀上述英文注釋, 下面是google翻譯

默認情況下,如果未指定“ bind”配置指令,則Redis將偵聽服務器上所有可用網絡接口的連接。 可以使用“ bind”配置指令僅偵聽一個或多個所選接口,然后偵聽一個或多個IP地址。
如果運行Redis的計算機直接暴露於Internet,則綁定到所有接口都是很危險的,並且會將實例暴露給Internet上的所有人。 因此,默認情況下,我們取消注釋以下bind指令,這將強制Redis僅偵聽IPv4回顧接口地址(這意味着Redis將只能接受來自運行在同一台計算機上的客戶端的連接)。
如果您確定要立即偵聽所有接口,請僅注意以下內容。
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM