0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 课时

使用 TLS 加密传输

启用 TLS、配置证书并从客户端安全连接,保护 Redis 流量免受窃听

使用 TLS 加密传输 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Encrypt in Transit?

By default Redis speaks plaintext over the network. Anyone able to sniff the wire can read your commands, including AUTH passwords and cached data. TLS encrypts the connection so traffic stays confidential and tamper-evident.

TLS Building Blocks

TLS uses certificates:

  • A server certificate proves the server's identity
  • A private key the server keeps secret
  • A CA certificate clients use to verify the server

Generating Certificates

For testing, the Redis source ships a helper script, or you can use openssl to create a CA and a server cert/key pair.

openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.crt -days 365 -nodes

Enabling TLS on the Server

Configure the TLS port and certificate paths. Setting port 0 disables the plaintext port so only TLS is accepted.

tls-port 6379
port 0
tls-cert-file server.crt
tls-key-file server.key
tls-ca-cert-file ca.crt

Mutual TLS

For stronger security, require clients to present their own certificate (mTLS). The server then authenticates the client in addition to encrypting traffic.

tls-auth-clients yes

Connecting with redis-cli

redis-cli supports TLS with the --tls flag plus the CA and, for mTLS, the client cert and key.

redis-cli --tls --cacert ca.crt -h myhost -p 6379

Connecting from Code

Client libraries accept TLS settings: enable TLS, point to the CA, and (for mTLS) the client certificate and key.

client = redis.Redis(host='myhost', port=6379, ssl=True, ssl_ca_certs='ca.crt')

Replication and Cluster over TLS

Inter-node traffic should be encrypted too. Enable tls-replication yes and tls-cluster yes so replicas and cluster bus connections also use TLS.

tls-replication yes
tls-cluster yes

Protocol and Cipher Hardening

Restrict allowed protocols and ciphers to modern, strong options to avoid downgrade attacks.

tls-protocols "TLSv1.2 TLSv1.3"

TLS Is Not Everything

TLS protects data in transit, not at rest, and does not replace authentication. Keep using requirepass/ACLs and bind to trusted interfaces; TLS is one layer of defense in depth.

Cost and Trade-offs

TLS adds CPU overhead for the handshake and encryption. It is usually negligible with persistent connections and connection pooling, but worth measuring under load.

Quick Check

Test your understanding of Redis TLS.

Recap

You enabled TLS on Redis: generated certificates, configured the TLS port and key/cert files, optionally required client certs for mTLS, secured replication and cluster traffic, and connected from CLI and code. Remember TLS is one layer; pair it with authentication and network isolation.

常见问题解答

「使用 TLS 加密传输」课时是免费的吗?

是的 — 「使用 TLS 加密传输」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「使用 TLS 加密传输」这节课中我会学到什么?

启用 TLS、配置证书并从客户端安全连接,保护 Redis 流量免受窃听 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 TLS 加密传输」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 身份验证与授权
  2. Redis 网络安全
  3. 运维最佳实践
  4. 使用 TLS 加密传输
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)