nginx来代理tcp,需要注意以下几点
1:nginx版本必须大于1.9.0
2:编译的时候必须加上这两个参数 –with-stream –with-stream_ssl_module
具体细节配置见:https://www.nginx.com/resources/admin-guide/nginx-tcp-ssl-termination/
workerman的tcp服务见:http://doc3.workerman.net/getting-started/simple-example.html 示例三
为了对比,先示例一般的TCP代码示例
<?php $fp = stream_socket_client("tcp://127.0.0.1:1215", $errno, $errstr, 3); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { echo fgets($fp, 1024); fclose($fp); }
对于服务端启用了ssl的,由于证书是自己生成的,所以需要不检查证书。如果是合法的证书,只需要将tcp协议改成ssl即可
<?php $context = stream_context_create(); //Require verification of peer name stream_context_set_option($context, 'ssl', 'verify_peer_name', false); //是否需要验证 SSL 证书 stream_context_set_option($context, 'ssl', 'verify_peer', false); //tcp不需要,在访问https的可能用的到 stream_context_set_option($context, 'ssl', 'verify_host', false); $fp = stream_socket_client("ssl://127.0.0.1:1215", $errno, $errstr, 3,STREAM_CLIENT_CONNECT, $context); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { echo fgets($fp, 1024); fclose($fp); }
这样我们就可以将正常的tcp服务通过ssl来传输了,如果想验证可以用stream_socket_client的代码来抓包看一下,本人确定已经是加密包了