一個場景:目前越來越多的業務需要遠程讀寫Redis,而Redis 本身不提供 SSL/TLS 的支持,在需要安全訪問的環境下。
這時候就需要額外的手段進行加密認證,這里有兩種手段:spiped 和 ngx stream proxy
現在服務端起一個監聽在127 的 Redis server
1、使用spiped
[root@ ~]# wget http://www.tarsnap.com/spiped/spiped-1.6.0.tgz
[root@ ~]# tar xf spiped-1.6.0.tgz
[root@ ~]# cd spiped-1.6.0
[root@ ~]# tar xf spiped-1.6.0.tgz
[root@ ~]# cd spiped-1.6.0
[root@ spiped-1.6.0]# make && make install
[root@ spiped-1.6.0]# /usr/local/bin/spiped -h
spiped: illegal option -- -h
usage: spiped {-e | -d} -s <source socket> -t <target socket> -k <key file>
[-DFj] [-f | -g] [-n <max # connections>] [-o <connection timeout>]
[-p <pidfile>] [-r <rtime> | -R]
spiped -v
創建key,並將key分發到代理客戶端
[root@ ~]# dd if=/dev/urandom bs=32 count=1 of=/var/spiped/redis_proxy.key
啟動服務端和客戶端代理:
#服務端:
[root@ ~]# /usr/local/bin/spiped -d -s '[0.0.0.0]:6010' -t '[127.0.0.1]:6379' -k /var/spiped/redis_proxy.key -p /var/spiped/redis_proxy_srv.pid
#客戶端代理:
[root@ ~]# /usr/local/bin/spiped -e -s '[127.0.0.1]:6379' -t '[x.x.x.x]:6010' -k /var/spiped/redis_proxy.key -p /var/spiped/redis_proxy_cli.pid
客戶端測試:
[root@ ~]# /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a redis@passwd ping
PONG
參考:http://www.tarsnap.com/spiped.html
2、使用ngx stream proxy
配置自簽證書,略。
證書和key可以同時給服務端代理和客戶端代理使用
ngx_stream_ssl_module模塊(1.9.0)為流代理服務器提供必要的支持,以使用SSL / TLS協議。 默認情況下不構建此模塊,應使用--with-stream_ssl_module配置參數啟用它。
編譯nginx需要加入--with-stream --with-stream_ssl_module 選項
服務端代理配置:
upstream redis_server{
server 127.0.0.1:6379 max_fails=3 fail_timeout=10s;
}
server{
listen 6010 ssl;
ssl_certificate /data/ssl/stream_proxy/stream_proxy.crt; #服務端證書
ssl_certificate_key /data/ssl/stream_proxy/stream_proxy.key; #服務端key
ssl_verify_client on; #開啟對客戶端的認證
ssl_client_certificate /data/ssl/stream_proxy/cacert.pem; #用於認證客戶端ca證書
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
proxy_connect_timeout 5s;
proxy_timeout 5s;
proxy_pass redis6001_server;
error_log /data/logs/redis_sslproxy_srv.log debug;
}
客戶端代理配置:
upstream redis_server{
server x.x.x.x:6010 max_fails=3 fail_timeout=10s;
}
server{
listen 127.0.0.1:6379;
proxy_ssl_name stream_proxy; #與證書中的hostname一致,覆蓋用於驗證后端的hostname,並通過SNI在與后端建立連接時傳遞,默認proxy_pass地址的host部分會被使用。
proxy_ssl on;
proxy_ssl_certificate /data/ssl/stream_proxy/stream_proxy.crt; #客戶端證書
proxy_ssl_certificate_key /data/ssl/stream_proxy/stream_proxy.key; #客戶端key
proxy_ssl_verify on; #開啟對服務端的認證
proxy_ssl_trusted_certificate /data/ssl/stream_proxy/cacert.pem; #用於認證服務端ca證書
ssl_session_timeout 10m;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
proxy_ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
proxy_connect_timeout 5s;
proxy_timeout 5s;
proxy_pass redis_server;
error_log /data/logs/redis_sslproxy_cli.log debug;
}
如果僅僅是實現ngx stream proxy加密不認證的話,只需要在服務端代理配置好證書,客戶端代理配置proxy_ssl on即可
參考鏈接:
1、https://blog.lyz810.com/article/2016/06/ngx_stream_proxy_module_doc_zh-cn/
2、https://blog.lyz810.com/article/2016/06/ngx_stream_ssl_module_doc_zh-cn/
3、https://my.oschina.net/foreverich/blog/1517128?utm_medium=referral