{"id":202171,"date":"2025-05-10T14:04:15","date_gmt":"2025-05-10T06:04:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202171\/"},"modified":"2025-05-10T14:04:15","modified_gmt":"2025-05-10T06:04:15","slug":"%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3php-redis502%e9%94%99%e8%af%af%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202171\/","title":{"rendered":"\u600e\u4e48\u89e3\u51b3php redis502\u9519\u8bef\u95ee\u9898"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u600e\u4e48\u89e3\u51b3php redis502\u9519\u8bef\u95ee\u9898<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u600e\u4e48\u89e3\u51b3php redis502\u9519\u8bef\u95ee\u9898\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6570\u636e\u5e93<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u672c\u6587\u64cd\u4f5c\u73af\u5883\uff1aWindows7\u7cfb\u7edf\u3001PHP5.5.30\u7248\u3001DELL G3\u7535\u8111<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3php redis502\u9519\u8bef\u95ee\u9898?<\/strong><\/p>\n<p>php redis set \u65f6 nginx 502 \u89e3\u51b3<\/p>\n<pre>PHP&nbsp;Version&nbsp;5.5.30-1+deb.sury.org~trusty+1\nredis&nbsp;3.0.1\uff0c<\/pre>\n<p>\u901a\u8fc7 \u4e0b\u8f7d pr.tar.gz \u5b89\u88c5\u7684 php-redis\u6269\u5c55\uff0c\u7248\u672c\u4e3a2.10\u3002<\/p>\n<p>\u6267\u884c $redis-&gt;set(&#8216;test&#8217;, &#8216;hello world&#8217;)\uff0c\u62a5nginx 502\u9519\u8bef\uff08nginx 502\u9519\u8bef\u771f\u662f\u4e2a\u5f88\u5934\u75bc\u7684\u95ee\u9898\uff09\u3002<\/p>\n<p>\u95ee\u9898\u5b9a\u4f4d\u5728 &nbsp;$this-&gt;_redis-&gt;setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);<\/p>\n<p>\u539f\u56e0\u662f php-redis\u6269\u5c55\u7248\u672c 2.10\uff0c \u592a\u4f4e\uff0c\u53ef\u4ee5\u4f7f\u7528 sudo apt-get install &nbsp;php5-redis \u6216\u8005 \u5728 gitHub \u4e0a\u4e0b\u8f7d<\/p>\n<pre>https:\/\/github.com\/phpredis\/phpredis<\/pre>\n<p>\u901a\u8fc7\u7f16\u8bd1\u5b89\u88c5\u3002<\/p>\n<p>apt-get install \u5b89\u88c5\u7684\u7248\u672c\u4e3a 2.2.7.<\/p>\n<pre>&nbsp;public&nbsp;function&nbsp;conn()&nbsp;{undefined\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{undefined\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_redis-&gt;pconnect($this-&gt;host,&nbsp;$this-&gt;port,&nbsp;$this-&gt;timeout);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Set&nbsp;client&nbsp;option.&nbsp;must&nbsp;AFTER&nbsp;connected\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/var_dump(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);exit();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_redis-&gt;setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/$this-&gt;_redis-&gt;setOption(0,&nbsp;0);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;_redis-&gt;auth(\"xxxrs\");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u6b64\u5904\u662f\u4e3a&nbsp;redis&nbsp;\u914d\u7f6e\u7684\u9a8c\u8bc1\u5bc6\u7801\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;connected&nbsp;=&nbsp;true;\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(RedisException&nbsp;$e)&nbsp;{undefined\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;CHttpException(500,&nbsp;\"Redis&nbsp;occurs&nbsp;an&nbsp;error:\"&nbsp;.&nbsp;$e-&gt;getMessage());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/pre>\n<p>\u7528\u7ea2\u8272\u7684\u4ee3\u7801\u90a3\u6bb5\uff0c\u5c31\u4f1a\u62a5502\u9519\u8bef\uff0c\u800c\u7528\u7eff\u8272\u4ee3\u7801\u8fd9\u6bb5\u5c31\u6ca1\u95ee\u9898\u3002<\/p>\n<p>setOption \u8fd9\u4e2a\u662f\u4e2a\u795e\u9a6c\u4e1c\u897f\u5462\uff1f\u67e5\u4e86\u4e0bphp-redis api<\/p>\n<pre>$redis-&gt;setOption(Redis::OPT_SERIALIZER,&nbsp;Redis::SERIALIZER_NONE);&nbsp;&nbsp;&nbsp;\/\/&nbsp;don't&nbsp;serialize&nbsp;data&nbsp;&nbsp;\u4e0d\u5e8f\u5217\u5316\u6570\u636e\n$redis-&gt;setOption(Redis::OPT_SERIALIZER,&nbsp;Redis::SERIALIZER_PHP);&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;use&nbsp;built-in&nbsp;serialize\/unserialize&nbsp;&nbsp;\u7528php\u5185\u7f6e\u7684\u5e8f\u5217\u5316\n$redis-&gt;setOption(Redis::OPT_SERIALIZER,&nbsp;Redis::SERIALIZER_IGBINARY);&nbsp;&nbsp;&nbsp;\/\/&nbsp;use&nbsp;igBinary&nbsp;serialize\/unserialize&nbsp;\u7528\u6269\u5c55IGBINARY\u5e8f\u5217\u5316\n$redis-&gt;setOption(Redis::OPT_PREFIX,&nbsp;'myAppName:');&nbsp;\/\/&nbsp;use&nbsp;custom&nbsp;prefix&nbsp;on&nbsp;all&nbsp;keys&nbsp;redis&nbsp;key\u524d\u7f00\n&nbsp;\n$redis-&gt;setOption(Redis::OPT_SERIALIZER,&nbsp;Redis::SERIALIZER_PHP);&nbsp;&nbsp;\u7528\u6b64\u5c31\u62a5502<\/pre>\n<p>\u4e3a\u4ec0\u4e48\u7528php\u5185\u7f6e\u7684\u5e8f\u5217\u5316\u5c31\u4f1a\u51fa\u9519\u5462\uff1f\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<\/p>\n<p>\u540e\u6765\u53c8\u5b89\u88c5\u4e86IGBINARY \u6269\u5c55\uff0c\u8fd8\u662f\u4e0d\u884c\u3002\u6700\u540e\u628aphp-redis\u5b89\u88c5\u4e86\u6700\u65b0\u7684\u7248\u672c2.24\uff0c\u518d\u8fd0\u884c\uff0c\u5c31\u5b8c\u5168\u6ca1\u95ee\u9898\u4e86\u3002<\/p>\n<p>2.24\u7248\u672cphp-redis\u6e90\u7801\u5730\u5740https:\/\/github.com\/nicolasff\/phpredis<\/p>\n<p>======================<\/p>\n<p>Redis::__construct\u6784\u9020\u51fd\u6570<\/p>\n<p>$redis = new Redis();<\/p>\n<p>connect, open \u94fe\u63a5redis\u670d\u52a1<\/p>\n<p>\u53c2\u6570<\/p>\n<p>host: string\uff0c\u670d\u52a1\u5730\u5740<\/p>\n<p>port: int,\u7aef\u53e3\u53f7<\/p>\n<p>timeout: float,\u94fe\u63a5\u65f6\u957f (\u53ef\u9009, \u9ed8\u8ba4\u4e3a 0 \uff0c\u4e0d\u9650\u94fe\u63a5\u65f6\u95f4)<\/p>\n<p>\u6ce8: \u5728redis.conf\u4e2d\u4e5f\u6709\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a300<\/p>\n<p>pconnect, popen \u4e0d\u4f1a\u4e3b\u52a8\u5173\u95ed\u7684\u94fe\u63a5<\/p>\n<p>\u53c2\u8003\u4e0a\u9762<\/p>\n<p>setOption \u8bbe\u7f6eredis\u6a21\u5f0f<\/p>\n<p>getOption \u67e5\u770bredis\u8bbe\u7f6e\u7684\u6a21\u5f0f<\/p>\n<p>ping \u67e5\u770b\u8fde\u63a5\u72b6\u6001<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u600e\u4e48\u89e3\u51b3php redis502&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202171","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202171"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202171\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}