在redis配置文件裏修改一些配置的時候,總會看到一些對配置的解釋,每次讀到都感受頗有收穫,甚至感受比網上的不少資料都要好,所以下決心本身完整翻譯並整理一份。redis
# Redis configuration file example. # # Note that in order to read the configuration file, Redis must be # started with the file path as first argument: # # ./redis-server /path/to/redis.conf # Note on units: when memory size is needed, it is possible to specify # it in the usual form of 1k 5GB 4M and so forth: # # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 bytes # 1g => 1000000000 bytes # 1gb => 1024*1024*1024 bytes # # units are case insensitive so 1GB 1Gb 1gB are all the same.
Redis配置文件示例.爲了能讀取到配置文件,注意Redis服務必須以配置文件的路徑做爲第一個參數啓動
./redis-server /path/to/redis.conf安全關於單位,當你須要指定內存的大小時,可使用以下的單位來指定服務器
1k => 1000 bytes
1kb => 1024 bytes
1m => 1000000 bytes
1mb => 1024*1024 bytes
1g => 1000000000 bytes
1gb => 102410241024 bytes網絡單位是大小寫不敏感的 因此 1GB 1Gb 1gB 都是同樣的dom
# Include one or more other config files here. This is useful if you # have a standard template that goes to all Redis servers but also need # to customize a few per-server settings. Include files can include # other files, so use this wisely. # # Notice option "include" won't be rewritten by command "CONFIG REWRITE" # from admin or Redis Sentinel. Since Redis always uses the last processed # line as value of a configuration directive, you'd better put includes # at the beginning of this file to avoid overwriting config change at runtime. # # If instead you are interested in using includes to override configuration # options, it is better to use include as the last line. # # include /path/to/local.conf # include /path/to/other.conf
在這裏包含一個或多個配置文件。若是您有一個適用於全部Redis服務器的標準模板,可是還須要自定義每一個服務器的一些設置,那麼這是很是有用的。引入文件能夠包含其餘的文件,因此用這種方式是很明智的。
注意選項「include」不會被來自admin或Redis哨兵的命令「CONFIG REWRITE」重寫。由於Redis老是使用最後一次處理做爲配置指令的值,
因此您最好在此文件的開頭放入include,以免在運行時覆蓋配置更改。但反過來若是您感興趣的是使用include覆蓋配置,最好把include配置放在文件的最後。socket
include /path/to/local.conf
include /path/to/other.conftcp
# Load modules at startup. If the server is not able to load modules # it will abort. It is possible to use multiple loadmodule directives. # # loadmodule /path/to/my_module.so # loadmodule /path/to/other_module.so
在啓動時加載模塊。若是服務沒法加載模塊它將停止。可使用多個loadmodule模塊加載指令。loadmodule /path/to/my_module.so
loadmodule /path/to/other_module.soide
# 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 loopback 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 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # 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 yes # Accept connections on the specified port, default is 6379 (IANA #815344). # 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 # Unix socket. # # Specify the path for the Unix socket that will be used to listen for # incoming connections. There is no default, so Redis will not listen # on a unix socket when not specified. # # unixsocket /tmp/redis.sock # unixsocketperm 700 # Close the connection after a client is idle for N seconds (0 to disable) timeout 0 # TCP keepalive. # # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence # of communication. This is useful for two reasons: # # 1) Detect dead peers. # 2) Take the connection alive from the point of view of network # equipment in the middle. # # On Linux, the specified value (in seconds) is the period used to send ACKs. # Note that to close the connection the double of the time is needed. # On other kernels the period depends on the kernel configuration. # # A reasonable value for this option is 300 seconds, which is the new # Redis default starting with Redis 3.2.1. tcp-keepalive 300
默認狀況下,若是沒有指定「bind」配置指令,則Redis偵聽服務器上全部可用網絡接口的鏈接。可使用「bind」配置指令監聽一個或多個選定的接口,而後監聽一個或多個IP地址。oop
例如:
bind 192.168.1.100 10.0.0.1
bind 127.0.0.1 ::1ui警告:若是運行Redis的計算機直接暴露在internet上,綁定到全部接口是危險的,會將實例暴露給internet上的每一個人。所以,在默認狀況下,咱們取消對如下bind指令的註釋,這將迫使Redis只監聽IPv4環回接口地址(這意味着Redis將只能接受來自運行在同一臺計算機上的客戶端的鏈接)。
若是您肯定但願實例偵聽全部接口只需註釋下面的行。
bind 127.0.0.1protected-mode是一種安全保護層,爲了不在internet上打開的Redis實例被訪問和利用。
當保護模式打開時,若是:
1)服務器沒有使用「綁定」指令顯式綁定到一組地址。
2)未設置密碼。服務器只接受來自IPv4和IPv6環回地址127.0.0.1和::1的客戶端鏈接,以及來自Unix域套接字的鏈接。
默認狀況下啓用了受保護模式。只有在您肯定但願其餘主機的客戶端鏈接到Redis(即便沒有配置任何身份驗證),或者使用「bind」指令顯式列出一組特定的接口時,才應該禁用它。
protected-mode yes接受指定端口上的鏈接,默認是6379 (IANA #815344)。若是端口0被指定,Redis將不會監聽TCP套接字。
port 6379TCP監聽 backlog 。
在每秒請求數很高的環境中,您須要一個高的backlog,以免緩慢的客戶端鏈接問題。請注意,Linux內核將靜默地將其截斷爲/proc/sys/net/core/somaxconn的值,所以請確保同時提升somaxconn和tcp_max_syn_backlog的值,以得到所需的效果。
tcp-backlog 511Unix 套接字。
爲Unix套接字指定用於監聽傳入鏈接的路徑。沒有默認值,因此在未指定時,Redis不會監聽unix套接字。
unixsocket / tmp / redis.sock
unixsocketperm 700在客戶端空閒N秒後關閉鏈接(0表示禁用)
timeout 0TCP keepalive.
若是該值不爲0,將使用 SO_KEEPALIVE 這一默認的作法來向客戶端鏈接發送TCP ACKs
這樣的好處有如下兩個緣由
1)檢測已經死亡的對端
2)保持鏈接在網絡環境中的存活在Linux上,指定的值(以秒爲單位)是用於發送ack的週期。
注意,關閉鏈接須要雙倍的時間。
對於其餘內核,週期取決於內核配置。此選項的合理值是300秒,即從Redis 3.2.1開始的新Redis默認值。
tcp-keepalive 300