【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?

简介: 【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?

问题描述

使用6379端口连接Azure Redis服务,连接失败。因为默认情况下Azure Redis的设置没有打开6379的端口。需要使用SSL(6380端口)进行连接,但是遇见了无法连接的问题。

使用非SSL(6379端口)的连接代码

<?php
echo "Azure Redis Connect Test";
echo  "<br/>";
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('xxxxxxxx.redis.cache.chinacloudapi.cn', 6379);
$redis->auth("秘钥");
echo "Connection to server sucessfully";
echo "<br/>";
//设置 redis 字符串数据
$redis->set("test-name", "Redis test value");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("test-name");
?>

如果直接修改端口号 6379 为 6380, 报错无法访问。那么使用6380的正确方式为?

 

问题解答

其实,代码的改动方式非常小。在 connection 方法中添加 TLS及6380端口。

改动前:$redis->connect('xxxxxxxx.redis.cache.chinacloudapi.cn', 6379);

改动后:$redis->connect('tls://xxxxxxxx.redis.cache.chinacloudapi.cn', 6380);

但是一定要注意php redis 扩展,因为扩展版本过低,有可能会引发解析问题。如类似如下的错误消息:

Azure Redis Connect Test<br/>PHP Warning:  
      Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/myweb/redis_tes
t2.php on line 6
PHP Fatal error:  Uncaught RedisException: php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/myweb/redis_test2.php:6
Stack trace:
#0 /data/myweb/redis_test2.php(6): Redis->connect('tls://xxxx-xxxx...', 6380)
#1 {main}
  thrown in /data/myweb/redis_test2.php on line 6

php redis 扩展的版本:

 

 

使用TLS 6380 端口连接Redis的示例代码为:

<?php
echo "Azure Redis Connect Test";
echo  "<br/>";
//连接本地的 Redis 服务
$redis = new Redis();
//$redis->connect('xxxxxxxx.redis.cache.chinacloudapi.cn', 6379);
$redis->connect('tls://xxxxxxxx.redis.cache.chinacloudapi.cn', 6380);
$redis->auth("秘钥");
echo "Connection to server sucessfully";
echo "<br/>";
//设置 redis 字符串数据
$redis->set("test-name", "Redis test value");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("test-name");
?>

 

 

参考资料

php redis example : https://github.com/phpredis/phpredis#example-2

php redis extension : https://pecl.php.net/package/redis

相关文章
|
NoSQL Redis Windows
windows服务器重装系统之后,Redis服务如何恢复?
windows服务器重装系统之后,Redis服务如何恢复?
351 6
|
安全 网络协议 网络安全
【Azure APIM】APIM服务配置网络之后出现3443端口不通,Management Endpoint不健康状态
如果没有关联的网络安全组,则阻止所有网络流量通过子网和网络接口。
303 30
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
810 60
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
837 60
|
网络安全 开发工具 git
解决 Git 访问 GitHub 时的 SSL 错误
通过上述步骤,可以有效解决 Git 访问 GitHub 时的 SSL 错误。推荐优先更新 CA 证书和正确配置 Git 使用 CA 证书,避免禁用 SSL 验证。如果问题持续,可以切换到 SSH 方式访问 GitHub,确保连接的安全性和稳定性。希望这些内容对您的学习和工作有所帮助。
5136 4
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
4637 8
|
安全 网络协议 应用服务中间件
内网ip申请SSL证书实现https访问
内网IP地址虽不能直接申请公网SSL证书,但可通过IP SSL证书保障数据安全。流程包括:确定固定内网IP,选择支持内网IP的CA,注册申请证书,生成CSR,验证IP所有权,下载部署证书至Web服务器,测试HTTPS访问,确保配置正确及证书有效。此方法适用于内网环境,提升数据传输安全性。
内网ip申请SSL证书实现https访问
|
NoSQL Linux 测试技术
redis的安装步骤及前台,后台redis服务启动
这篇文章介绍了Redis的安装步骤,包括在Linux系统中下载、传输、解压、编译、安装Redis,以及Redis服务的前台和后台启动方法。
redis的安装步骤及前台,后台redis服务启动
|
NoSQL Linux Redis
Linux Redis 服务设置开机自启动
【9月更文挑战第2天】在 Linux 系统中,可使用两种方法设置 Redis 开机自启动:一是通过创建 `redis.service` 文件并利用 systemd 进行管理,包括定义服务参数和启动脚本;二是编辑 `/etc/rc.local` 文件,在其中添加启动命令。推荐使用 systemd 方法,因为它更符合现代 Linux 系统的设计理念。设置完成后,可通过 `sudo systemctl status redis.service` 检查服务状态。
2264 4

热门文章

最新文章